【问题标题】:why i am getting org.json.JSONException: Value property of type java.lang.String cannot be converted to JSONObject?为什么我得到 org.json.JSONException:java.lang.String 类型的值属性无法转换为 JSONObject?
【发布时间】:2017-01-09 11:29:32
【问题描述】:

通过使用这个,我得到了响应代码并试图将 String 转换为 JSONObject,但得到了异常。

public JSONObject getJSONFromUrl(String url,List<NameValuePair> nameValuePairsList) {
        // Making HTTP request
        try {
            HttpParams param = new BasicHttpParams();
            HttpConnectionParams.setConnectionTimeout(param, 20000);
            HttpConnectionParams.setSoTimeout(param, 20000);
            DefaultHttpClient httpClient = new DefaultHttpClient(param);
            HttpPost httpPost = new HttpPost(url);
            httpPost.setHeader(HTTP.CONTENT_TYPE, "application/x-www-form-urlencoded");
            try {
                httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairsList));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            PropertyLogger.debug("URL Request: ", url.toString());
            PropertyLogger.debug("Encoded Params: ", nameValuePairsList.toString());
            HttpResponse httpResponse = httpClient.execute(httpPost);
            int code = httpResponse.getStatusLine().getStatusCode();
            if (code != 200) {
                PropertyLogger.debug("HTTP response code is:", Integer.toString(code));
                return null;
            } else {
                HttpEntity httpEntity = httpResponse.getEntity();
                is = httpEntity.getContent();
            }
        } catch (ConnectTimeoutException e) {
            // TODO: handle exception
            PropertyLogger.error("Timeout Exception", e.toString());
            return null;
        } catch (SocketTimeoutException e) {
            // TODO: handle exception
            PropertyLogger.error("Socket Time out", e.toString());
            return null;
        } catch (UnsupportedEncodingException e) {
            PropertyLogger.error("UnSupported Exception", e.toString());
            e.printStackTrace();
            return null;
        } catch (ClientProtocolException e) {
            e.printStackTrace();
            return null;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
        String TAG = "PropertyJsonParser";
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null)
            {
                sb.append(line + "\n");
            }
            is.close();
            jsonResp = sb.toString();
            PropertyLogger.debug("Content: ", sb.toString());
        } catch (Exception e) {
            PropertyLogger.error("Buffer Error", "Error converting Response " + e.toString());
            return null;
        }
        // try parse the string to a JSON object
        try {
            jObj = new JSONObject(jsonResp);
        } catch (JSONException e) {
            PropertyLogger.error("JSON Parser", "Error parsing data :" + e.toString());
            return null;
        }

        return jObj;
    }

【问题讨论】:

  • 记录 jsonResp 并告诉我们它是什么。
  • 你没有得到有效的 json
  • 在转换为 json 对象之前记录您的 jsonResp 并检查它是否为 json 格式?

标签: java android json string


【解决方案1】:

您收到此错误是因为您没有收到正确的 JSON 响应。

检查您的 api 响应,并可能还提供您的 api 响应。

【讨论】:

    【解决方案2】:

    因为您的变量 jsonResp 包含字符串值而不是 JSONObject 所以请先打印并确定它是否是字符串类型值。

    【讨论】:

    • jsonResp 不是String 怎么可能是什么?
    • 因为你的错误表明这个错误只有在你将字符串值放入 JSONObject 时才会发生。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多