android - okhttp 响应中的新 JsonObject

标签 android okhttp androidhttpclient

我正在尝试使用OKHttphttps://github.com/hongyangAndroid/okhttp-utils来实现登录功能,但我不知道如何实现创建新的 JsonObject 在回调中使用 Response。

  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

最佳答案

由于调用 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) {
...}

关于android - okhttp 响应中的新 JsonObject,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37337516/

相关文章:

android - 如何在亚马逊API中恢复购买?

java - 在 android 中使用 Json 对象文件创建数组列表

java - 在android中使用HttpUrlConnection出现意外的状态行异常?

android - 在 Android 中发送 "PUT"请求到 rest api

android-emulator - 如何在 Android 模拟器中连接 localhost?

android - 解析安装未获取 DeviceToken

java - Android Studio运行java类时出错

java - 响应http响应体的编码

java - 如何使用 OKHTTP 进行并发网络请求?

java - AndroidHttpClient线程安全吗