I am unable to make a network call using Volley

前端 未结 4 1530
甜味超标
甜味超标 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:24

    Change your DefaultRetryPolicy with this:

    DefaultRetryPolicy policy = new DefaultRetryPolicy(0,
                        DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                        DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
    jsonObject.setRetryPolicy(policy);
    
    0 讨论(0)
  • 2021-01-24 09:37

    Another Solution using StringRequest to get Json Response

      private void callApi() {
        RequestQueue mrequestQueue;
        StringRequest stringRequest;
        mrequestQueue = Volley.newRequestQueue(this);
        stringRequest = new StringRequest(Request.Method.GET, DATA_URL, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
    
                try {
    
                    JSONObject jsonObject = new JSONObject(response);
                    System.out.println("json"+jsonObject);
    
    
                } catch (JSONException e) {
    
                    e.printStackTrace();
                }
    
    
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                //Log.i(TAG, "Response: " + error.toString());
    
            }
        });
        mrequestQueue.add(stringRequest);
    
    
    }
    

    0 讨论(0)
  • 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<JSONObject>() {
                        @Override
                        public void onResponse(JSONObject response) {
    
    
                        }
                    }, new VolleyErrorListener(this, AuthController.LOGIN_ERROR)) {
                @Override
                public Map<String, String> 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<String, Bitmap>
                                cache = new LruCache<String, Bitmap>(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;
        }
    

    }

    0 讨论(0)
  • 2021-01-24 09:41

    Try this using String Request

    public class TestActivity extends AppCompatActivity {
            RequestQueue requestQueue;
            String DATA_URL = "http://ec2-54-147-238-136.compute-1.amazonaws.com/hmc/api/getnewsfeeds?order=asc";
        
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
                requestQueue = Volley.newRequestQueue(this);
                callApi();
            }
        
            private void callApi() {
                StringRequest stringRequest = new StringRequest(Request.Method.GET, DATA_URL, new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Log.e("res", response);
                    }
        
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Log.e("onErrorResponse: ", error.getLocalizedMessage());
                    }
        
                });
        
                requestQueue.add(stringRequest);
            }
    

    0 讨论(0)
提交回复
热议问题