0

在 Andriod 上,我正在尝试将框架从 AsyncHttpClient 更改为 Volley。在AsyncHttpClient中有一个回调方法onStart可以帮助我在收到服务器的响应之前做一些事情。(比如通知用户等待。)

我想通过 Volley 来实现这个功能,但我没有找到类似的回调来实现。有人可以帮我吗?谢谢!

4

1 回答 1

0

Volley中有两种类型的回调:

/** 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);
}

没有onStart等效的 AsyncHttpClient 回调。

例子:

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
于 2015-04-24T19:29:37.053 回答