加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

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

发布时间:2020-12-13 21:47:50 所属栏目:PHP教程 来源:网络整理
导读:我有一个连接到数据库并获取数据的php文件,现在我希望将这些数据发送到我的 java代码并存储为数组,用于连接到php并且我正在使用AsyncHttpClient查找结果 现在在AsyncHttpClient中,它们是一个函数onSucess,它接受一个字符串值作为参数,所以来自php的任何东西
我有一个连接到数据库并获取数据的php文件,现在我希望将这些数据发送到我的 java代码并存储为数组,用于连接到php并且我正在使用AsyncHttpClient查找结果
现在在AsyncHttpClient中,它们是一个函数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());
        }               
    });
}

和我有一个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);
 ?>

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

解决方法

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

PHP方面

创建简单的PHP数组,更多细节click here

<?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;
?>

Android Side

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

String String_Response = ""; // this is your web response

创建一个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);
               }

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

Android + PHP Communication Example

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读