2017-01-09 54 views
0

通过使用这个我得到的响应代码,并试图将字符串转换为JSONObject但得到异常。为什么我得到org.json.JSONException:类型java.lang.String的Value属性不能转换为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; 
    } 
+0

登录jsonResp并告诉我们它是什么。 –

+0

你没有得到有效的JSON –

+0

登录您的jsonResp之前转换成JSON对象,并检查它是JSON格式? – LeoMobDev

回答

2

你,因为你没有接受适当的JSON响应得到这个错误。

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

0

因为你的变量jsonResp包含字符串值不的JSONObject因此请首先打印,并确保它是字符串类型值与否。

+1

'jsonResp'会不会是'String'? – darrengorman

+1

因为您的错误表明只有在将字符串值放入JSONObject时才会出现此错误。 – bharat7777

相关问题