凌空,ImageLoader.ImageListener中的onResponse调用了多少次 [英] Volley, How many times the onResponse in ImageLoader.ImageListener called
问题描述
我使用Volley
进行互联网请求.我认为onResponse
方法在收到响应时应该只调用一次,但是我发现它被调用了两次.
I‘ using Volley
to do internet requests. I think the onResponse
method should be called only once when receive a response, but I found it called twice.
这是我的代码:
YVolley.getInstance(context).getImageLoader().get(category.child.get(i).icon, new ImageLoader.ImageListener() {
@Override
public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate) {
Drawable drawable = new BitmapDrawable(context.getResources(), response.getBitmap());
drawable.setBounds(0, 0, GeneralUtil.dip2px(context, 45), GeneralUtil.dip2px(context, 45));
button.setCompoundDrawables(null, drawable, null, null);
Log.i("swifter", "get icon ... success == "+url);
}
@Override
public void onErrorResponse(VolleyError error) {
Log.i("swifter", "get drawable icon error...");
}
});
成功"日志打印了两次.
The "success" Log printed twice.
我的代码有问题吗?还是应该像这样?
Is there something wrong with my code or it should be like this ?
推荐答案
I found the answer to this in the documentation for ImageLoader.java
. The documentation states:
呼叫流程是这样:
The call flow is this:
-
在附加到请求后,将调用
onResponse(response, true)
以反映所有已可用的缓存数据.如果数据可用,则response.getBitmap()
将为非空.
Upon being attached to a request,
onResponse(response, true)
will be invoked to reflect any cached data that was already available. If the data was available,response.getBitmap()
will be non-null.
网络响应返回后,只会发生以下情况之一:
After a network response returns, only one of the following cases will happen:
-
如果图像已加载,将调用
-
onResponse(response, false)
或
如果加载图像时出错,将调用 -
onErrorResponse
.
onResponse(response, false)
will be called if the image was loaded, oronErrorResponse
will be called if there was an error loading the image.
基于此,可能的响应共有三种模式.我已经在示例应用程序中测试并确认了这些.
Based on this there are three patterns of possible responses. I have tested and confirmed these in a sample application.
在这种情况下,会有一个呼叫:
In this case there will be one call:
-
onRespsonse(response, true)
将被调用,您可以从response.getBitmap()
获取图像.
onRespsonse(response, true)
will be called, and you can get the image fromresponse.getBitmap()
.
在这种情况下,将有两个呼叫:
In this case there will be two calls:
-
首先,将调用
onRespsonse(response, true)
,而response.getBitmap()
将是null
.
然后,将调用onRespsonse(response, false)
,您可以从response.getBitmap()
获取图像.
Then, onRespsonse(response, false)
will be called, and you can get the image from response.getBitmap()
.
在这种情况下,将有两个呼叫:
In this case there will be two calls:
-
首先,将调用
onRespsonse(response, true)
,而response.getBitmap()
将是null
.
然后,将调用onErrorResponse(error)
,并且可以从error
(这是VolleyError
的实例)中找到错误的详细信息.
Then, onErrorResponse(error)
will be called, and details of the error can be found from error
(which will be an instance of VolleyError
).
对于您而言,以下代码段将帮助您跳过第一个空"响应:
In your case, the following code snippet will help you to skip the first "empty" response:
@Override
public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate) {
// skip cache failure
if (isImmediate && response.getBitmap() == null) return;
// ...
}
希望这有助于弄清为什么您可能会收到两个针对您请求的回复.
Hopefully this helps to clear up why you may be getting two responses to your request.
这篇关于凌空,ImageLoader.ImageListener中的onResponse调用了多少次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!