Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/239.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/214.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Android中如何将http响应放入数组_Php_Android - Fatal编程技术网

Php Android中如何将http响应放入数组

Php Android中如何将http响应放入数组,php,android,Php,Android,我试图使用Http响应从PHP服务器获取数据,但这里的棘手之处是,我将响应作为一个字符串获取。我想将响应放入数组中。响应最初包含许多我从MySQL检索到的查询。非常感谢您的帮助。试试这个……它将帮助您将响应存储在数组中 try { URL url = new URL("http:/xx.xxx.xxx.x/sample.php"); HttpURLConnection urlConn

我试图使用Http响应从PHP服务器获取数据,但这里的棘手之处是,我将响应作为一个字符串获取。我想将响应放入数组中。响应最初包含许多我从MySQL检索到的查询。非常感谢您的帮助。

试试这个……它将帮助您将响应存储在数组中

           try
             {

                URL url = new URL("http:/xx.xxx.xxx.x/sample.php");
                HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                 InputStream in = new BufferedInputStream(urlConnection.getInputStream());
                 BufferedReader r = new BufferedReader(new InputStreamReader(in));
                 String x = "";
                 String total = "";
                 int i=0;
                 ArrayList<String> content = new ArrayList();
                 while((x = r.readLine()) != null)
                 {
                             content.add(x);

                 }
                 in.close();
                 r.close();
             }
             catch(Exception e)
             {
                 e.printStackTrace();
                 Toast.makeText(this, e.toString(), Toast.LENGTH_SHORT).show();
             }

您应该在服务器端使用数据交换格式(如
XML
JSON
)对响应进行编码

然后您可以在客户端轻松地解析它

Android对两者都有很好的支持,不过JSON可能更容易一些

如果您的数据结构非常简单(例如单词列表),则可以使用
CSV
(逗号分隔值)和
String.split()
获取数组:

String[] words = response.split(",");

JSON示例(字符串数组)

[
“敏捷的棕色狐狸跳过懒惰的狗”,
“寒鸦喜欢我的石英大狮身人面像”,
“Lorem ipsum door sit amet,一位杰出的领导者,他是一位临时的领导者”
]
JSONArray数组=新的JSONArray(响应);
String[]句子=新字符串[array.length()];
对于(int i=0,i
更好的方法是让php Web服务以JSON格式发送数据。然后将其作为一个函数接收,并解析JSON响应以获取所需的数据。我推荐JSON,因为它比xml更轻,这将提高性能,减少带宽消耗

尝试创建返回JSON数据的php脚本这是检索数据并将其放入数组的示例

              String ar[]= content.toArray(new String[content.size()]);
<?php
 $response = array();

 require_once __DIR__ . '/db_connect.php';

$db = new DB_CONNECT();  
$result = mysql_query("SELECT * FROM tbl_products") or die(mysql_error());

if (mysql_num_rows($result) > 0) {

$response["product"] = array();

while ($row = mysql_fetch_array($result)) {

    $product            = array();
    $product["products_id"]     = $row["table_id"];
    $product["products_price"]  = $row["transaction_no"];
$product['products_name']   = $row["table_total_price"];

    array_push($response["product"], $product);
}
$response["success"] = 1;

echo json_encode($response);

} else {
$response["success"] = 0;
$response["message"] = "No products found";

echo json_encode($response);
}
?>

这一款是针对android的:

JSONObject json = jParser.getJSONFromUrl(NAME_OF_URL);

        Log.d("All Product List: ", json.toString());

        try {

            int success = json.getInt("success");

            if (success == 1) {

                products = json.getJSONArray("product");

                for (int i = 0; i < products.length(); i++) {

                    JSONObject c = products.getJSONObject(i);

                    String id  =c.getString("products_id");
                    String price =c.getString("products_price");
                    String name = c.getString("products_name");

                }

            } else {

            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
jsonobjectjson=jParser.getJSONFromUrl(URL的名称);
Log.d(“所有产品列表:”,json.toString());
试一试{
int success=json.getInt(“success”);
如果(成功==1){
products=json.getJSONArray(“产品”);
对于(int i=0;i
但是如果我的回答是一组句子呢?我已经包含了一个JSON示例。
JSONObject json = jParser.getJSONFromUrl(NAME_OF_URL);

        Log.d("All Product List: ", json.toString());

        try {

            int success = json.getInt("success");

            if (success == 1) {

                products = json.getJSONArray("product");

                for (int i = 0; i < products.length(); i++) {

                    JSONObject c = products.getJSONObject(i);

                    String id  =c.getString("products_id");
                    String price =c.getString("products_price");
                    String name = c.getString("products_name");

                }

            } else {

            }

        } catch (JSONException e) {
            e.printStackTrace();
        }