简体   繁体   中英

how to read json response from php in android using asynchttp in android?

I'm making an program to post an array list to server which then parse the array list and store it in the data base but iam unable to read the response could somebody help me out as iam unable to fin the soultion

Here is the android java code :-

 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();
            }

     }

and here is the php code :-

 $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)

how can i read this response in my java program ?

In onSuccess check for http status code 200 then parse the response byte array like this

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

after that parse json Sting to any POJO using Gson. Do same for both Success and failure.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM