Okhttp showing warning when used with volleyball

When developing my Android application, I get this warning in my Logcat

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

I use Okhttps as a transport layer for volleyball for a network call.

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);
    }

}

To create requestQueue

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

I am calling url like this

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));

}

From tracking issues from Okhttp, I read that we must close the body of the response every time to avoid leakage, but I don’t understand how to do this when using volleyball and Okhttp.

Dependencies:

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'
+4
source share
1 answer

you are not showing the full code here, I think this message is the result of the new call not closing.

private String get(final String url) {
String res = null;
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) {
        final ResponseBody 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;
0
source

Source: https://habr.com/ru/post/1689366/


All Articles