簡體   English   中英

類型為org.json.JSONObject的響應無法轉換為JSONArray

[英]at response of type org.json.JSONObject cannot be converted to JSONArray

我正在嘗試使用HttpPost上傳文件並將其發送到php ,在JsonArray中將得到響應。 但是我遇到的問題是在Android應用程序中使用JsonArray或對象。 然后,來自數組的信息將與應用程序一起放入SQLite db中。 我無法從數組中提取數據並得到以下錯誤

JSONArray上的“響應類型為org.json.JSONObject的響應無法轉換為JSONArray” arr = object.getJSONArray(“ response”);

我對HttpPost不太熟悉,希望得到我提供的任何詳細幫助。 我從網上嘗試了幾件事,但沒有成功。

Array 
  ( 
     [response] => Array 
       ( 
           [Filepathserver] => webaddress 
           [email] => xyz@email.com
           [syncstatus] => yes 
       ) 

)  

Java文件

   String url = "uploadphpfile.php";
    File file = new File(filepath);
    try {

        HttpClient httpclient = new DefaultHttpClient();

        HttpPost httppost = new HttpPost(url);

         httppost.setEntity(mpEntity);
        Log.d("enitity",mpEntity.toString());

        HttpResponse response = httpclient.execute(httppost);
        HttpEntity r_entity = response.getEntity();

       String result = EntityUtils.toString(r_entity);
        JSONObject object = new JSONObject(result);

        JSONArray arr =object.getJSONArray("response");

        for (int i = 0; i < arr.length(); i++) {
            JSONObject obj = arr.getJSONObject(i);
            Log.d("filepathserver",obj.get("Filepathserver").toString());
        }
        //Do something with response...
        int statusCode = response.getStatusLine().getStatusCode();
        if (statusCode == 200) {
            // Server response
          String  responseString = EntityUtils.toString(r_entity);
        } else {
            String responseString = "Error occurred! Http Status Code: "
                    + statusCode;
        }
     } catch (Exception e) {
          Log.e(DBHelperReports.class.getName(), e.getLocalizedMessage(), 
  e);
     }
    }

php文件

   $response = array();
  $response["Filepathserver"] = $target_path;
  $response["email"] = $target_path;
  $response["syncstatus"] = 'yes';
  echo json_encode(array("response"=>$response));

logcat的

  Value 
 {"Filepathserver":"webaddress","email":"xyz@email.com","syncstatus":"yes"} 
 at response of type org.json.JSONObject cannot be converted to JSONArray

 org.json.JSONException: Value 
 {"Filepathserver":"webaddress","email":"xyz@email.com","syncstatus":"yes"} 
 at response of type org.json.JSONObject cannot be converted to JSONArray

 at org.json.JSON.typeMismatch(JSON.java:100)

 at org.json.JSONObject.getJSONArray(JSONObject.java:588)

 at com.multiinspectionelite.DBHelperReports$1.run(DBHelperReports.java:383)

 at java.lang.Thread.run(Thread.java:762)

您的php文件產生了一個json對象,而不是json數組。

因此,請嘗試JSONObject arr =object.getJSONObject("response");

順便說一句,您的logcat已經提醒您將其視為JSONObject。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM