如何在Android中使用AsyncHTTP在Android中读取PHP的JSON响应



我正在制作一个程序将数组列表发布到服务器,然后解析数组列表并将其存储在数据库中,但我无法读取响应有人可以帮助我,因为我无法鳞翅目

这是安卓java代码:-

 public void confirmOrder(final String name, final String price, final String quantity, final String cost, final String sellerid)
     {
            AsyncHttpClient httpClient = new AsyncHttpClient();
            RequestParams params = new RequestParams();
            ArrayList<HashMap<String,String>> productList= dbHelper.getAllProducts();
            if(productList.size()!=0)
            {
                pDialog.show();
                params.put("OrderSummary",dbHelper.getAllproducts());
                httpClient.post(APIURL.API_URL, params, new AsyncHttpResponseHandler() {
                    @Override
                    public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                    }
                    @Override
                    public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                    }
                });
            }
            else
            {
                Toast.makeText(getApplicationContext(),"List Empty",Toast.LENGTH_LONG).show();
            }
     }

这是PHP代码:-

 $json = $_POST["usersJSON"];
    //Remove Slashes
    if (get_magic_quotes_gpc()){
    $json = stripslashes($json);
    }
    //Decode JSON into an Array
12
    $data = json_decode($json);
    //Util arrays to create response JSON
    $a=array();
    $b=array();
    //Loop through an Array and insert data read from JSON into MySQL DB
    for($i=0; $i<count($data) ; $i++)
    {
      //inserting data in to the database
        i++;
      }
    if(i<0)
     {
          $response['error']=true;
          $response['message']="data inserted";
     }
    else
     {
         $response['error']=false;
         $response['message']="error occured";
         }
    echo json_encode ($response)

如何在我的 Java 程序中读取此响应?

> 在 onSuccess 中检查 http 状态代码 200,然后像这样解析响应字节数组

  String str = IOUtils.toString(responseBody, "UTF-8");

之后使用 Gson 将 json Sting 解析到任何 POJO。对成功和失败都做同样的事情。

最新更新