【问题标题】:at response of type org.json.JSONObject cannot be converted to JSONArrayorg.json.JSONObject 类型的响应无法转换为 JSONArray
【发布时间】:2017-12-21 01:36:46
【问题描述】:

我正在尝试使用HttpPost 上传文件并将其发送到php,在那里我会在JsonArray 中得到回复。但是我遇到的问题是使用JsonArray 或对象在android 应用程序中使用。然后将数组中的信息放入应用程序中的SQLite db。我无法从数组中提取数据并出现以下错误

" org.json.JSONObject 类型的响应无法转换为 JSONArray" on 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)

【问题讨论】:

  • 类型不匹配错误。

标签: java php android json http-post


【解决方案1】:

您的 php 文件生成了一个 json 对象,而不是一个 json 数组。

那么,试试JSONObject arr =object.getJSONObject("response");

对了,你的 logcat 已经提醒你把它当作一个 JSONObject 来对待了。

【讨论】:

  • 感谢您的回复。我知道它是一个对象,但是 JSON 对象中的 JSONArray 怎么能
  • 例如:{ "animals": { "dogs": [ { "name": "Snoopy", "color": "white", "age": "1" } ], "猫”:[{“名称”:“小猫”,“颜色”:“白色”,“年龄”:“1”},{“文本”:“加菲猫”,“颜色”:“橙色”,“年龄” : "2" } ] } } animals 是 JSONObject。它包含两个JSONArrays。 cats 和 dogs 是 JSONArrays。 cats 或 dogs 中的内容是 JSONObjects。
猜你喜欢
  • 1970-01-01
  • 2021-07-12
  • 2013-08-24
  • 1970-01-01
  • 1970-01-01
  • 2023-04-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多