I am unable to make a network call using Volley

前端 未结 4 1529
甜味超标
甜味超标 2021-01-24 09:13

When I am making a network call using jsonObject request. I am not receiving any response. I always receive HTTP response for request=<[ ] before my URL. I have tried these l

4条回答
  •  情歌与酒
    2021-01-24 09:41

    if your app is getting completed before response then it is causing connection timeout. you must add connection timeout in volley and retry policy also.Here is an example

            JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST,
                    url,
                    request,
                    new Response.Listener() {
                        @Override
                        public void onResponse(JSONObject response) {
    
    
                        }
                    }, new VolleyErrorListener(this, AuthController.LOGIN_ERROR)) {
                @Override
                public Map getHeaders() throws AuthFailureError {
                    return new VolleyHeader().getHeaders();
                }
            };
            jsonObjectRequest.setRetryPolicy(new DefaultRetryPolicy(
                    50000,
                    0,
                    DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
            NetworkRequestQueue.getInstance(mContext).getRequestQueue().add(jsonObjectRequest);
    

    NetworkRequestQueue class is as follows:

    public class NetworkRequestQueue {
    
        private static NetworkRequestQueue mInstance;
        private static Context mCtx;
        private RequestQueue mRequestQueue;
        private ImageLoader mImageLoader;
    
    
        private NetworkRequestQueue(Context context) {
            mCtx = context;
            mRequestQueue = getRequestQueue();
    
            mImageLoader = new ImageLoader(mRequestQueue,
                    new ImageLoader.ImageCache() {
                        private final LruCache
                                cache = new LruCache(20);
    
                        @Override
                        public Bitmap getBitmap(String url) {
                            return cache.get(url);
                        }
    
                        @Override
                        public void putBitmap(String url, Bitmap bitmap) {
                            cache.put(url, bitmap);
                        }
                    });
        }
    
        public static synchronized NetworkRequestQueue getInstance(Context context) {
            if (mInstance == null) {
                mInstance = new NetworkRequestQueue(context);
            }
            return mInstance;
        }
    
        public RequestQueue getRequestQueue() {
            if (mRequestQueue == null) {
                Cache cache = new DiskBasedCache(mCtx.getCacheDir(), 10 * 1024 * 1024);
                Network network = new BasicNetwork(new HurlStack());
                mRequestQueue = new RequestQueue(cache, network);
                // Don't forget to start the volley request queue
                mRequestQueue.start();
            }
            return mRequestQueue;
        }
    
        public ImageLoader getImageLoader() {
            return mImageLoader;
        }
    

    }

提交回复
热议问题