Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Volley Request Identity onErrorResponse Section

public void getTestDats(String unique_id) {
    final String tag = "testList";
    String url = Constants.BASE_URL + "test_module.php";
    Map<String, String> params = new HashMap<String, String>();
    params.put("user_id", SharedPreferenceUtil.getString(Constants.PrefKeys.PREF_USER_ID, "1"));
    params.put("unique_id", unique_id);//1,2,3,4,5
    DataRequest loginRequest = new DataRequest(Method.POST, url, params, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            switch (response.optInt("unique_id")) {
                case 1:
                    //task 1
                    break;
                case 2:
                    //task 2
                    break;
                default:
                    //nothing
            }
        }
    }, new ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
                //I want to know which unique_id request is failed 
        }
    });
    loginRequest.setRetryPolicy(new DefaultRetryPolicy(20000, 0, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
    AppController.getInstance().addToRequestQueue(loginRequest, tag);
} 

I'm trying to identity which request is failed with having unique_id.

I'm calling getTestDats("1") function with unique_id. And function called 10 times and all the api call in addToRequestQueue.

When API go into Success part its working as per code. But when API go into Error part I didn't identity the request. Is there any way to know my request param so I can retry with particular unique_id request.

like image 699
comeback4you Avatar asked Jul 25 '16 11:07

comeback4you


2 Answers

set a field in loginRequest and in onErrorResponse access the field like loginRequest.getUniqueId()

Alternatively, create a seperate class that implements Response.Listener and ErrorListener

Response Listener class:

public class MyReponseListener implements Response.Listener<JSONOBject>{
    private long uniqId;
    public MyResponseListener(long uniqId){
        this.uniqId = uniqId;
    }

    @Override
    public void onResponse(JSONObject response) {
        System.out.println("response for uniqId " + uniqId);
        // do your other chit chat
    }
}

ErrorListener class:

public class MyErrorListener implements ErrorListener{
        private long uniqId;
        public MyErrorListener(long uniqId){
            this.uniqId = uniqId;
        }

        @Override
        public void onErrorResponse(VolleyError error) {
             System.out.println("Error for uniqId : " + uniqId);
        }
}

Now call it like:

DataRequest loginRequest = new DataRequest(Method.POST, url, params, new MyResponeListener(uniqId), new MyErrorListener(uniqId));

Now if you want some code of the calling class to be accessible in the ErrorListener class then do the following: 1. In calling class put the codes you want to access in methods 2. Create an interface with those method 3. The calling class will implement that interface 4. Pass the interface to constructor of the MyErrorListener or MyResponseListener

for example an activity calls the volley request, on error you want to show a message. put that show error codes in a method:

public void showMessage(int errorCode){
    //message according to code
}

now create an interface

public interface errorMessageInterface{
    void showMessage(int errorCode);
}

the activity will implement errorMessageInterface and pass this to the constructor of MyErrorListener and save it in a field.

Inside onErrorResponse, you will call

field.showMessage()
like image 171
dumb_terminal Avatar answered Sep 27 '22 23:09

dumb_terminal


You can parse error response in the same way as you parse success response. I use similar solution in my projects.

public class VolleyErrorParser {
    private VolleyError mError;
    private String mBody;
    private int mUniqueId = -1;
    public VolleyErrorParser(VolleyError e){
        mError = e;
        parseAnswer();
        parseBody();
    }

    private void parseBody() {
        if (mBody==null)
            return;
        try{
            JSONObject response = new JSONObject(mBody);
            mUniqueId = response.getOptInt("unique_id");

        }catch (JSONException e){
            e.printStackTrace();
        }
    }

    private void parseAnswer() {
        if (mError!=null&&mError.networkResponse!=null&&mError.networkResponse.data!=null){
            mBody = new String(mError.networkResponse.data);
        }
    }
    public String getBody(){
        return mBody;
    }
    public int getUniqueId(){
        return mUniqueId;
    }
}

Use:

...
, new ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            int id = new VolleyErrorParse(error).getUniqueId();
            switch (id) {
                case -1:
                     //unique id not found in the answer
                     break;
                case 1:
                    //task 1
                    break;
                case 2:
                    //task 2
                    break;
                default:
                    //nothing
            }        
        }
    }
...
like image 25
Alexey Rogovoy Avatar answered Sep 27 '22 23:09

Alexey Rogovoy