Okhttp在使用凌空时显示泄露的警告

时间:2017-11-16 09:59:17

标签: android memory-leaks android-volley okhttp okhttp3

在开发我的Android应用程序时,我在Logcat中收到此警告

WARNING: A connection to https://... was leaked. Did you forget to close a response body?

我正在使用Okhttps作为网络呼叫凌空的传输层。

class OkHttpStack extends HurlStack {

private final OkUrlFactory okUrlFactory;

    OkHttpStack() {
       this(new OkUrlFactory(new OkHttpClient()));
    }

    private OkHttpStack(OkUrlFactory okUrlFactory) {
        if (okUrlFactory == null) {
            throw new NullPointerException("Client must not be null.");
        }
        this.okUrlFactory = okUrlFactory;
    }

    @Override
    protected HttpURLConnection createConnection(URL url) throws 
    IOException {
        return okUrlFactory.open(url);
    }

}

用于创建requestQueue

synchronized RequestQueue getRequestQueue() {
    if (mRequestQueue == null) {
        mRequestQueue = Volley.newRequestQueue(mContext,new OkHttpStack());
    }
    return mRequestQueue;
}
<T> void addToRequestQueue(Request<T> req) {
    getRequestQueue().add(req);
}

我正在调用这样的网址

public void postCall(final String Url, JSONObject Data, final ResponseCallback responseCallback){

    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(mUrl+Url, Data,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    Log.d(TAG,response.toString());
                    responseCallback.onGettingResponse(response);
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.d(TAG,error.toString());
                    responseCallback.onError(error);
                }
            });
    jsonObjectRequest.setRetryPolicy(new DefaultRetryPolicy(
            TIMEOUT_IN_SECONDS,
            0,
            0));

    mRestApiCall.getInstance(mContext).addToRequestQueue(jsonObjectRequest.setTag(mRequestTag));

}

从Okhttp问题跟踪器,我读到我们必须每次关闭响应体以避免泄漏但我不知道如何使用volley和Okhttp这样做。

依赖关系:

compile 'com.android.volley:volley:1.0.0'
compile 'com.squareup.okhttp3:okhttp:3.9.0'
compile 'com.squareup.okhttp:okhttp-urlconnection:2.4.0'

2 个答案:

答案 0 :(得分:2)

在 Kotlin 中我们可以这样使用:

try {
                val req: Request = Request.Builder().url(url).get().build()
                val client = OkHttpClient()
                val resp: Response = client.newCall(req).execute()
                val code: Int = resp.code() // can be any value
                body= resp.body()
                if (code == 200) {
                    body?.close() // I close it explicitly
                }

答案 1 :(得分:0)

你猜这里没有显示完整的代码,这条消息是新呼叫未被关闭的结果。

private String get(final String url) {
String res = null;
ResponseBody body;
try {
    final Request req = new Request.Builder().url(url).get().build();
    final Response resp = ok.newCall(req).execute();
    final int code = resp.code();
    if (code == 200) {
        body = resp.body();
        res = body.string();
    }
}
catch (final Throwable th) {
    System.out.println(th.getMessage());
  }
finally() {
    body.close(); // this would be missing somewhere in your code where you are receiving your response
}
return res;