2016-08-01 54 views
0

我使用改造圖書館,使HttpClient的POST請求,但 我無法通過響應身體的JSONObject構造, 當我嘗試使用通過響應的JSONObject:unhandeled例外org.json.JSONObject

org.json.JSONObject Jobject = new org.json.JSONObject(jsonData); 

我有以下異常:

unhandeled例外org.json.JSONObject

我的代碼:

call.enqueue(new Callback<ResponseBody>() { 
       @Override 
       public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) { 
        if(response.isSuccessful()){ 

        String jsonData = response.body().toString(); 

        JSONObject Jobject = new JSONObject(jsonData); 

       } 

      } 

      @Override 
      public void onFailure(Call<ResponseBody> call, Throwable t) { 
       display_message.setText(t.toString()); 
      } 
     }); 

從下面的語句預期的返回值:

String jsonData = response.body().source().toString(); 

是: {"is_new_user":true,"messing_user_data["mobile_number","country_code","password","pin"]}

但具體返回的結果是:

[size=90 text={"is_new_user":true,"messing_user_data":["mobile_number","countr…] 

任何想法幫助我。

+0

加上'的JSONObject Jobject = new JSONObject(jsonData);'在'try'塊內,並在catch塊中捕獲'JSONException'。 –

+0

你可以寫日誌jsonData嗎?請檢查它是格式json文件嗎? JsonObject或JsonArray? – sonnv1368

+0

請參閱上面其他更新 –

回答

0

您應環繞你

JSONObject Jobject = new JSONObject(jsonData); 

用try-catch塊

call.enqueue(new Callback<ResponseBody>() { 
     @Override 
     public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) { 
      if(response.isSuccessful()){ 

      String jsonData = response.body().toString(); 

      try { 
       JSONObject Jobject = new JSONObject(jsonData); 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 

     } 

    } 

    @Override 
    public void onFailure(Call<ResponseBody> call, Throwable t) { 
     display_message.setText(t.toString()); 
    } 
}); 

並嘗試檢查您的JSON字符串,如果有正確的格式,因爲你需要

+0

將try-catch包含JSONObject之後我有以下例外: org.json.JSONException:值[email protected] java.lang.String類型不能轉換爲JSONObject –

+0

請參閱上面的內容額外的更新 –

+0

好吧,很明顯,因爲你的字符串不是有效的JSON對象。請再次檢查您的字符串{「is_new_user」:true,「messing_user_data [」mobile_number「,」country_code「,」password「,」pin「]} – HendraWD