繁体   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