2017-01-09 4 views
0

これを使用してレスポンスコードを取得し、StringをJSONObjectに変換しようとしていますが、例外が発生しています。あなたの変数jsonRespためなぜ私は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

ので、最初の印刷をしてください、それが文字列型の値かどうかであることを確認してくださいJSONObjectない文字列値が含まれています。

+1

'jsonResp'は' String'以外の何かになりますか? – darrengorman

+1

このエラーはJSONObjectに文字列値を入力した場合にのみ発生することを示すエラーです。 – bharat7777

関連する問題