How to call manually Response.Listener in Android?

53
June 16, 2022, at 11:00 PM

I have the following method that runs some web services commands in Android.

    Response.Listener Success = new Response.Listener<JSONObject >() {@Override
                public void onResponse(JSONObject  response) {
                    JSONObject  S = response;
                    // do something with the response
    }};
    
    Response.ErrorListener Fail = new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            if (error.networkResponse.statusCode == 400) {
                Success.notify();  // <<<<< According to some technical custom logic, this should be considered as a successful REST command. Hence run Success Response Listener
            }
            else {
                // operation failed, do something
            }
        }
    };
    JSONObject J = new JSONObject();        
    J.put("param", some parameters);
    RequestQueue queue = Volley.newRequestQueue(Act);
    JsonObjectRequest JsonRequestObject = new JsonObjectRequest(Method, URL, J, Success, Fail);        
    queue.add(JsonRequestObject);

According to some custom technical logic, the requests that get 400 as a response should be considered as successful and hence run the success calback.

I tried this code but it crashes at the line :

                Success.notify();  // <<<<<

Does anyone know how can I execute the Success call back manually please ?

Thanks Cheers,

Answer 1

Instead of notify() you should call onResponse() like Success.onResponse() with null argument or with some JSONObject mapped from error.networkResponse.

More generalized way - create custom Response Listener (to listen to the success/error events in one place):

public abstract class ResponseListener<T> implements Response.Listener<T>,
        Response.ErrorListener {
    @Override
    public final void onErrorResponse(VolleyError error) {
        if (error.networkResponse.statusCode == 400) {
            // redirect to onResponse()
            onResponse(null /* or some response T object mapped from [error.networkResponse] */);
        } else {
            onError(error);
        }
    }
    public abstract void onError(VolleyError error);
}

...

 final ResponseListener<JSONObject> responseListener =
            new ResponseListener<JSONObject>() {
                @Override
                public void onResponse(@Nullable JSONObject response) {
                    // do something with the response
                }
                @Override
                public void onError(VolleyError error) {
                    // operation failed, do something
                }
            };
    //
    final JsonObjectRequest JsonRequestObject =
            new JsonObjectRequest(Method, URL, J, responseListener, responseListener);
    //
Rent Charter Buses Company
READ ALSO
JavaScript Error Element is null: while interacting ShadownDom element

JavaScript Error Element is null: while interacting ShadownDom element

I am writing code in Javascript to automate the website https://wwwfashionette

84
ViewModel datas don&#39;t update

ViewModel datas don't update

I have an UpdateFragment which display cards group by tags using a RecyclerView and ViewModelIn UpdateFragment I can also modify the name of the tags but I don't know why the change isn't detected by UpdateFragment until I recreate the fragment so UI don't update

74
Spring batch job step up defenition

Spring batch job step up defenition

Im developing a Spring Batch application, technology which by the way I'm new to

69
Docker Image java library information

Docker Image java library information

I have a custom docker imageI need to check what all java dependency that image using

73