2016-05-20 10 views
3

OKHttphttps://github.com/hongyangAndroid/okhttp-utilsを使用してログイン機能を実装しようとしていますが、JsonObjectレスポンスをコールバックで使用する方法がわかりません。okhttp new JsonObjectからレスポンス

OkHttpUtils.post() 
      .url(url) 
      .addParams("phone", "18681873411") 
      .addParams("password", "18681873411") 
      .build() 
      .execute(new Callback() { 
       @Override 
       public Object parseNetworkResponse(Response response) throws Exception { 
        Log.i("ws","---->>parseNetworkResponse" + response.body().string()); 


        JSONObject jsonObj = null; 
        try { 
         jsonObj = new JSONObject(response.body().string()); 
        } catch (JSONException e) { 
         Log.i("ws","---->>JSONException"); 
         e.printStackTrace(); 
        } catch (IOException e) { 
         Log.i("ws","---->>IOException"); 
         e.printStackTrace(); 
        } 
        Log.i("ws","---->>111 :"); 
        String opCode = jsonObj.getString("OpCode"); 
        String message = jsonObj.getString("Message"); 
        Log.i("ws","---->>111 opCode:" + opCode); 
        Log.i("ws","---->>111 Message:" + message); 
        JSONObject result = new JSONObject(); 
        result.put("qrcode",opCode); 
        result.put("message", message); 
        return result; 

       } 

       @Override 
       public void onError(Call call, Exception e) { 
        Log.i("ws","---->>onError" + e); 
       } 

       @Override 
       public void onResponse(Object response) { 
        Log.i("ws","---->>onResponse" + response.toString()); 
       } 
      }); 

とエラー情報:

parseNetworkResponse{"OpCode":0,"Message":"登陆成功","Other":{"id":1,"name":null,"phone":"18681873411","QQuid":null,"weixinuid":null,"gender":null,"age":null,"credits":null,"address":null,"signature":null,"imageurl":null,"password":"18681873411","created_at":"2016-05-19T17:05:02.000Z","updated_at":"2016-05-19T17:05:02.000Z"}} 
05-20 11:29:01.953 13496-13621/com.smartspace.magicmirror I/ws: ---->>IOException 
05-20 11:29:01.954 13496-13621/com.smartspace.magicmirror I/ws: ---->>111 : 
05-20 11:29:01.955 13496-13496/com.smartspace.magicmirror I/ws: ---->>onErrorjava.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String org.json.JSONObject.getString(java.lang.String)' on a null object reference 
+0

は、あなたの 'jsonObj'が正しく解析された値が含まれていることを確認していますか?コード 'String opCode = jsonObj.getString(" OpCode ")を試してみてください。 String message = jsonObj.getString( "Message"); 'あなたの' try'文の中にあります。 'jsonObj =新しいJSONObject(response.body()。string());'の下にあります。 –

+0

私の答えを見て.. .. !! –

答えて

4

コールresponse.body().string()は、それを閉じて戻りストリームからコンテンツを読むので、あなたはそれを呼び出す二度目ますので。それは何も返さないだろう:

良い方法:\

if (!response.isSuccessful()) throw new IOException("Unexpected code " + response); 
    String responseData = response.body().string(); 
    Log.e(TAG, "onResponse: " + responseData); 
    try { 
     JSONObject jsonObject = new JSONObject(responseData); 
     // Do something here 
     } catch (JSONException e) { 
...} 
+2

それは、ありがとう、働く。 –

関連する問題