Search code examples
androidandroid-volleynetwork-connection

Getting response from server after reconnect the internet volley


While i am hitting URL to get response from server using volley ,during the fetching the net was disconnected .

my question is how to fetch data from server after reconnect the internet connection ,without hitting url once again

 private void PostRequest(String Url) {

mRequest = new ServiceRequest(HomeActivity.this);
mRequest.makeServiceRequest(Url, Request.Method.GET, null, new ServiceRequest.ServiceListener() {
    @Override
    public void onCompleteListener(String response) {

        Log.d("reponse", response);

            JSONObject object = new JSONObject(response);

    }


    @Override
    public void onErrorListener() {
        indicator.setVisibility(View.GONE);
    }
});



 public void makeServiceRequest(final String url, int method, final 
 HashMap<String, String> param,ServiceListener listener) {

this.mServiceListener=listener;

stringRequest = new StringRequest(method, url, new Response.Listener<String> 
 () {
    @Override
    public void onResponse(String response) {
        try {
            mServiceListener.onCompleteListener(response);

        } catch (Exception e) {
        }
    }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        try {
            if (error instanceof TimeoutError || error instanceof 
  NoConnectionError) {
            } else if (error instanceof AuthFailureError) {
            } else if (error instanceof ServerError) {
            } else if (error instanceof NetworkError) {
            } else if (error instanceof ParseError) {
            }
        } catch (Exception e) {
        }

         mServiceListener.onErrorListener();
    }
     }) {
    @Override
    protected Map<String, String> getParams() throws AuthFailureError {
        return param;
    }

    @Override
    public Map<String, String> getHeaders() throws AuthFailureError {
        Map<String, String> headers = new HashMap<String, String>();
        return headers;
    }
};

     //to avoid repeat request Multiple Time
DefaultRetryPolicy retryPolicy = new DefaultRetryPolicy(0, -1, 
 DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
stringRequest.setRetryPolicy(retryPolicy);
stringRequest.setRetryPolicy(new DefaultRetryPolicy(30000,
        DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
        DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
stringRequest.setShouldCache(false);
AppController.getInstance().addToRequestQueue(stringRequest);
 }
 }

please give suggetion,before hitting url i was checked internet connection


Solution

  • You can do one thing, create the collection of your failed requests. Register the broadcast receiver of CONNECTIVITY_CHANGE and WIFI_STATE_CHANGED to get notified when the internet connection is available. Once your device connects to the internet, you will get notified in registered broadcast receiver and then send retry all the request which are there in your collection. Once request is successful remove the request from the collection.