简体   繁体   English

类型为org.json.JSONObject的响应无法转换为JSONArray

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

I am trying to upload a file using HttpPost and send it to php where I would get a response in JsonArray . 我正在尝试使用HttpPost上传文件并将其发送到php ,在JsonArray中将得到响应。 However the problem I am having is to use the JsonArray or object to use in android app. 但是我遇到的问题是在Android应用程序中使用JsonArray或对象。 The information from the array will then be put in to SQLite db with in the app. 然后,来自数组的信息将与应用程序一起放入SQLite db中。 I am unable to extract the data from array and getting the following error 我无法从数组中提取数据并得到以下错误

"at response of type org.json.JSONObject cannot be converted to JSONArray" on JSONArray arr =object.getJSONArray("response"); JSONArray上的“响应类型为org.json.JSONObject的响应无法转换为JSONArray” arr = object.getJSONArray(“ response”);

I am not well verse with HttpPost and would appreciate any detail help I can get. 我对HttpPost不太熟悉,希望得到我提供的任何详细帮助。 I have tried few things from net but was not successful. 我从网上尝试了几件事,但没有成功。

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

)  

java file 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 file php文件

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

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

your php file produced a json object, not a json array. 您的php文件产生了一个json对象,而不是json数组。

so, try JSONObject arr =object.getJSONObject("response"); 因此,请尝试JSONObject arr =object.getJSONObject("response");

By the way, your logcat had already reminded you to treat it as a JSONObject. 顺便说一句,您的logcat已经提醒您将其视为JSONObject。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Android中解析JSON响应“无法将org.json.JSONObject类型的响应转换为JSONArray” - Parse JSON response in android “response of type org.json.JSONObject cannot be converted to JSONArray” 无法将org.json.JSONObject类型的数据转换为JSONArray - Data of type org.json.JSONObject cannot be converted to JSONArray at 类型 org.json.JSONObject 的数据无法转换为 JSONArray - at data of type org.json.JSONObject cannot be converted to JSONArray 值类型 org.json.JSONObject 无法转换为 JSONArray - Value type org.json.JSONObject cannot be converted to JSONArray org.json.JSONObject无法转换为JSONArray - org.json.JSONObject cannot be converted to JSONArray 错误org.json.JSONObject无法转换为JSONArray - error org.json.JSONObject cannot be converted to JSONArray 解析JSON数组时,获取异常“ org.json.JSONObject类型的答案无法转换为JSONArray” - Getting exception “ at answer of type org.json.JSONObject cannot be converted to JSONArray” while parsing JSON array 我有一个错误:类型 org.json.JSONObject 无法转换为 JSONArray - I have an error : type org.json.JSONObject cannot be converted to JSONArray org.json.JSONException:值 {“storeid0”:[“1535”],“storeid1”:[“1862”]} 类型为 org.json.JSONObject 的 idddsss 无法转换为 JSONArray - org.json.JSONException: Value {“storeid0”:[“1535”],“storeid1”:[“1862”]} at idddsss of type org.json.JSONObject cannot be converted to JSONArray 无法将类型为org.json.JSONObject $ 1的值null转换为JSONObject错误 - Value null of type org.json.JSONObject$1 cannot be converted to JSONObject error found
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM