试图从PHP获取数组并将其存储在java,android中

yug*_*yug 3 php arrays string android

香港专业教育学院得到了连接到数据库并获取数据的php文件,现在我想这个数据被发送到我的Java代码,并存储为一个数组,用于连接到PHP和retriving我使用结果AsyncHttpClient 现在AsyncHttpClient他们是FUNC onSucess采用字符串值作为参数,所以来自php的任何内容都存储为字符串.我希望它是数组..请建议我一种方式,要么得到一个数组而不是字符串以下是我的代码.

 public void func3(View view)throws Exception
{
    AsyncHttpClient client = new AsyncHttpClient();
    RequestParams rp = new RequestParams();
    rp.put("pLat", "select * from iwmp_state");
    client.post("http://10.0.2.2/conc2.php", rp, newAsyncHttpResponseHandler() {
        public final void onSuccess(Array response) {
            // handle your response here
            //tx.setText(response.toString());

        }

        @Override
        public void onFailure(Throwable e, String response) {
            // something went wrong
            tx.setText(response.toString());
        }               
    });
}
Run Code Online (Sandbox Code Playgroud)

和我有一个PHP文件回声数组$行

<?php
 // attempt a connection
 $dbh = pg_connect("host=10.22.35.11 dbname=iwmp_dev2 user=postgres "); 
 if (!$dbh) {
     die("Error in connection: " . pg_last_error());
 }       

 // execute query
 $sql = $_POST['pLat'];
 $result = pg_query($dbh, $sql);
 if (!$result) {
     die("Error in SQL query: " . pg_last_error());
 }       
$array = array();
 // iterate over result set
 // print each row
 while ($row = pg_fetch_array($result)) {
    $i++;
echo $row[0];
 }       

 // free memory
 pg_free_result($result);       

 // close connection
 pg_close($dbh);
 ?>  
Run Code Online (Sandbox Code Playgroud)

php echos是什么数组,onSuccess的参数是字符串.该怎么办!

Chi*_*iya 5

在这里,我只是展示简单的demo.You必须根据您的要求更新.

PHP方面

创建简单PHP Array,有关详细信息,请单击此处

<?php
$cars=array("Volvo","BMW","Toyota");
$arrlength=count($cars);

for($x=0;$x<$arrlength;$x++)
  {
  echo $cars[$x];
  echo "<br>";
  }
 echo json_encode($cars); 
 exit;
?>
Run Code Online (Sandbox Code Playgroud)

Android Side

现在如何在android中读取PHP数组?

String String_Response = ""; // this is your web response
Run Code Online (Sandbox Code Playgroud)

创建一个ArrayList.

    ArrayList<String> User_List = new ArrayList<String>();


                  try
              {
            JSONArray jArray = new JSONArray(String_Response);
            for (int i = 0; i < jArray.length(); i++)
            {
                       JSONObject json_data = jArray.getJSONObject(i);                                                        
               User_List.add(json_data.getString("your_json_obj"));
            }

               } 
                  catch (Exception e)
              {
            Log.e(TAG, "" + e);
               }
Run Code Online (Sandbox Code Playgroud)

另请查看以下链接,您将了解如何从android发送和接收数据到php.

Android + PHP通信示例