0

On Andriod, I'm trying to change the framework from AsyncHttpClient to Volley. There's a call back method onStart in AsyncHttpClient which helps me do something before I receive the response from the server. (Such as notifying the user to wait.)

I'd like to implement this functionality by Volley, but I didn't find a similar call back to implement. Can someone help me out? Thanks!

53iScott
  • 827
  • 1
  • 13
  • 18

1 Answers1

0

In Volley there are two types of callbacks:

/** Callback interface for delivering parsed responses. */
public interface Listener<T> {
    /** Called when a response is received. */
    public void onResponse(T response);
}

/** Callback interface for delivering error responses. */
public interface ErrorListener {
    /**
     * Callback method that an error has been occurred with the
     * provided error code and optional user-readable message.
     */
    public void onErrorResponse(VolleyError error);
}

There is no AsyncHttpClient onStart callback equivalent.

Example:

String url = "http://stackoverflow.com";
StringRequest request = new StringRequest(url, new Listener<String>() {
    @Override
    public void onResponse(String response) {
        // handle response
    }
}, new ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        // handle error
    }
});

RequestQueue requestQueue = Volley.newRequestQueue(context);

request.setTag(TAG); // you can add tag to request
requestQueue.add(request); // add request to queue
requestQueue.cancelAll(TAG); // you can cancel request using tag
Ziem
  • 6,579
  • 8
  • 53
  • 86
  • Yes, I'm using these two Listeners. It seems Volley is not good at handling the moment during sending a request and receiving the response, does it? I'd like to notify the user it's processing during this moment. – 53iScott Apr 28 '15 at 07:40