When developing my Android application, I get this warning in my Logcat
WARNING: A connection to https:
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'
source
share