I just started porting from my existing network library to Android's Volley. So far I have successfully implemented Volleys ImageLoader where applicable. Now, I am trying to get my first http call up and running, but am finding this error.
Note: I intentionally started with a PATCH request because I will be using them frequently. Also, my version of Volley DOES support patch: https://android.googlesource.com/platform/frameworks/volley/+/master/src/com/android/volley/Request.java https://android.googlesource.com/platform/frameworks/volley/+/master/src/com/android/volley/toolbox/HurlStack.java
Stacktrace:
E/InputDialogFragment(27940): VolleyError: java.net.ProtocolException: Connection already established
D/Volley (27940): [1] MarkerLog.finish: (67 ms) [ ] https://mobile.example.com/m/api/v1/user/ 0xb33a3c8d NORMAL 2
D/Volley (27940): [1] MarkerLog.finish: (+0 ) [ 1] add-to-queue
D/Volley (27940): [1] MarkerLog.finish: (+0 ) [544] cache-queue-take
D/Volley (27940): [1] MarkerLog.finish: (+0 ) [544] cache-miss
D/Volley (27940): [1] MarkerLog.finish: (+0 ) [545] network-queue-take
D/Volley (27940): [1] MarkerLog.finish: (+14 ) [545] post-error
D/Volley (27940): [1] MarkerLog.finish: (+53 ) [ 1] done
PATCH Request
HashMap<String, Object> values = new HashMap<String, Object>();
values.put(mParam, val);
JsonObjectRequest request = new JsonObjectRequest(Request.Method.PATCH, APIConstants.URL_USER, new JSONObject(values),
new Response.Listener<JSONObject>(){
@Override
public void onResponse(JSONObject response){
// Blah do stuff here
mProgressDialog.dismiss();
}
},
new Response.ErrorListener(){
@Override
public void onErrorResponse(VolleyError error){
Log.e(TAG, "VolleyError: " + error.getMessage());
}
}){
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
VolleySingleton.getInstance().addCookie(headers);
return headers;
}
};
VolleySingleton.getInstance().getRequestQueue().add(request);
Yes, I plan to eventually build out classes for StringRequest, JsonObjectRequest, etc... but currently I just want to get one up and running.
Also, if you are wondering about addCookie, for now I prefer to save my cookie in preferences as I am not as familiar with CookieManager.
VolleySingleton
public class VolleySingleton {
private static final String COOKIE_KEY = "Cookie";
private static VolleySingleton mInstance = null;
private RequestQueue mRequestQueue;
private ImageLoader mImageLoader;
private SharedPreferences mPreferences;
private VolleySingleton(){
mRequestQueue = Volley.newRequestQueue(MyApplication.getAppContext());
mImageLoader = new ImageLoader(this.mRequestQueue, new ImageLoader.ImageCache() {
private final LruCache<String, Bitmap> mCache = new LruCache<String, Bitmap>(10);
public void putBitmap(String url, Bitmap bitmap) {
mCache.put(url, bitmap);
}
public Bitmap getBitmap(String url) {
return mCache.get(url);
}
});
mPreferences = MyApplication.getAppContext().getSharedPreferences(PrefConstants.PREFERENCES, 0);
}
public static VolleySingleton getInstance(){
if(mInstance == null){
mInstance = new VolleySingleton();
}
return mInstance;
}
public RequestQueue getRequestQueue(){
return this.mRequestQueue;
}
public ImageLoader getImageLoader(){
return this.mImageLoader;
}
public final void addCookie(Map<String, String> headers) {
String cookie = mPreferences.getString(PrefConstants.PREF_COOKIE, null);
if(cookie != null){
headers.put(COOKIE_KEY, cookie);
}
}
}