从后台线程上的OkHttp3 API调用获取空指针

时间:2018-02-11 03:35:49

标签: java android nullpointerexception okhttp3

我正在开发一款使用网络服务的移动应用。我正在使用MVP模式。

有时,我在调试时得到响应,有时候我得到一个空指针,指示后台线程在我尝试从调用中访问结果之前没有完成处理调用。

我该如何解决这个问题?调用API的代码运行良好,但看起来时间问题就在这里。

  

登录交互者类代码

public class LoginInteractorImpl implements LoginInteractor {

    private static final String TAG = LoginInteractorImpl.class.getName();
    private Handler mHandler;
    private HttpResponseResult httpResponseResult = null;
    private Gson mGson;
    private OkHttpRequestUtil okHttpRequestUtil = null;

    @Override
    public void login(final String username, final String password, final OnLoginFinishedListener listener, final LoginView mLoginView) {

        mGson = new Gson();
        mHandler = new Handler(Looper.getMainLooper());

        mHandler.post(new Runnable() {
            @Override
            public void run() {
                if (TextUtils.isEmpty(username)) {
                    listener.onUsernameError();
                    return;
                }
                if (TextUtils.isEmpty(password)) {
                    listener.onPasswordError();
                    return;
                }


            }
        });



        String postBody = mGson.toJson(new LoginRequestDTO(username, password));

        okHttpRequestUtil = new OkHttpRequestUtil();
        String url = Configuration.BASE_URL.concat(Configuration.FEED_LOGIN_URL);
        MediaType jsonMediaType = Configuration.JSON_MEDIA_TYPE;


        httpResponseResult = okHttpRequestUtil.DoPost(null, postBody, (Activity) mLoginView, listener, url, jsonMediaType);

        try {
            TransformJsonResponseToPojo(httpResponseResult, (Activity)mLoginView);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
  

OkHttpRequestUtil类的DoPost方法。

public class OkHttpRequestUtil {
    public static final String TAG = OkHttpRequestUtil.class.getName();

    OkHttpClient mClient = null;
    HttpResponseResult httpResponseResult = null;
    private Handler mHandler;

    public HttpResponseResult DoPost(final Map<String, String> headers, String postBody, Context context, final LoginInteractor.OnLoginFinishedListener listener, String url, MediaType jsonMediaType) {

        HttpUrl.Builder urlBuilder = HttpUrl.parse(url).newBuilder();
        Request request = null;
        String processedUrl = urlBuilder.build().toString();
        RequestBody body = RequestBody.create(jsonMediaType, postBody);

        mHandler = new Handler(Looper.getMainLooper());


        if (headers != null) {
            Headers headerBuild = Headers.of(headers);

            request = new Request.Builder()
                    .headers(headerBuild)
                    .url(processedUrl)
                    .post(body)
                    .build();

        }
        else
        {
            request = new Request.Builder()
                    .url(processedUrl)
                    .post(body)
                    .build();
        }


        try {
            mClient = new OkHttpClient.Builder()
                    .connectTimeout(30, TimeUnit.SECONDS)
                    .readTimeout(30, TimeUnit.SECONDS)
                    .writeTimeout(30, TimeUnit.SECONDS)
                    .sslSocketFactory(CustomTrust.getPinnedCertSslSocketFactory(context), (X509TrustManager) CustomTrust.getTrustManagerFactory(context).getTrustManagers()[0])
                    .build();
        } catch (KeyStoreException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (CertificateException e) {
            e.printStackTrace();
        }

        mClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                call.cancel();
                e.printStackTrace();
            }

            @Override
            public void onResponse(Call call, final Response response) throws IOException {
                // ... check for failure using `isSuccessful` before proceeding
                if (!response.isSuccessful()) {
                    Log.i(TAG, response.toString());
                    mHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            listener.onPasswordError();
                            try {
                                throw new IOException("Unexpected code " + response);
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    });



                }
                // Populate the HttpResponseResult

                httpResponseResult = new HttpResponseResult();
                httpResponseResult.setCode(String.valueOf(response.code()));
                httpResponseResult.setMessage(response.message());
                httpResponseResult.setBodyString(response.body().string());


                Headers okHttpResponseHeaders = response.headers();
                Map<String, String> responseHeadersTemp = new HashMap<String, String>();

                for(int i = 0; i < okHttpResponseHeaders.size(); i++)
                {
                    responseHeadersTemp.put(okHttpResponseHeaders.name(i), okHttpResponseHeaders.value(i));
                }

                httpResponseResult.setHeaders(responseHeadersTemp);

                listener.onSuccess();

            }
        });

        return httpResponseResult;
    }
}

1 个答案:

答案 0 :(得分:0)

基本上,你的代码就像你的返回语句在这个enqueue Callback结束之前发生一样

mClient.newCall(request).enqueue

使HTTP响应为null,是的。

解决方案是让DoPost不返回任何内容(并且不要将您的方法名称大写)。方法类型为void

将结果从onResponse通过参数传递回listener.onSuccess(responseData);

您可以立即解析响应,无需使用标头和所有

复制整个HTTP响应