代码之家  ›  专栏  ›  技术社区  ›  Ahsan

at响应类型为org。json。JSONObject无法转换为JSONArray

  •  0
  • Ahsan  · 技术社区  · 8 年前

    我正在尝试使用上载文件 HttpPost 并将其发送到 php 在那里我可以得到回应 JsonArray . 然而,我的问题是使用 JsonArray公司 或在android应用程序中使用的对象。然后将来自阵列的信息输入到 SQLite 应用程序中的db。我无法从数组中提取数据,并出现以下错误

    “无法将类型为org.json.JSONObject的at响应转换为 JSONArray arr=object.getJSONArray(“响应”);

    我不太喜欢诗歌 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)
    
    1 回复  |  直到 8 年前
        1
  •  3
  •   Relish Wang    8 年前

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

    所以,试试看 JSONObject arr =object.getJSONObject("response");

    顺便说一下,您的logcat已经提醒您将其视为JSONObject。

    推荐文章