2017-10-08 53 views
1

我正在製作一個程序,將數組列表發佈到服務器上,然後解析數組列表並將其存儲在數據庫中,但是iam無法讀取響應 有人可以幫我解決問題,因爲iam無法完成靈魂活動如何在android中使用asynchttp從php中讀取json響應?

下面是Android 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) 

我怎麼能讀這個respons e在我的java程序中?

回答

0

在用於HTTP狀態代碼200的onSuccess校驗然後解析響應字節數組這樣

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

之後解析JSON斯汀使用GSON任何POJO。 爲成功和失敗做同樣的事情。