凌空ImageLoader.ImageListener的onResponse多少次叫 [英] Volley How many time the onResponse in ImageLoader.ImageListener called
问题描述
我用排球做一些意向性的要求。我认为,当得到响应onResponse方法应该调用一次,但我发现它调用两次。
这是我的code:
YVolley.getInstance(上下文).getImageLoader()获得(category.child.get(我).icon,新ImageLoader.ImageListener(){
@覆盖
公共无效onResponse(ImageLoader.ImageContainer响应,布尔isImmediate){
可绘制可绘制=新BitmapDrawable(context.getResources(),response.getBitmap());
drawable.setBounds(0,0,GeneralUtil.dip2px(上下文,45),GeneralUtil.dip2px(上下文,45));
button.setCompoundDrawables(NULL,绘制,NULL,NULL);
Log.i(更快,弄图标...成功==+网址);
} @覆盖
公共无效onErrorResponse(VolleyError错误){
Log.i(更快,得可绘制图标错误...);
}
});
成功打印日志两次。
是不是有什么毛病我code,或者它应该是这个样子?
我发现<一个这个问题的答案href=\"https://android.googlesource.com/platform/frameworks/volley/+/master/src/main/java/com/android/volley/toolbox/ImageLoader.java#118\"相对=nofollow称号=ImageLoader.java,行118>为 ImageLoader.java
的文档。该文件指出:
呼叫流程是这样的:
在被连接到一个请求,
onResponse(响应,真)
将被调用,以反映已经可用的任何缓存数据。如果数据可用,response.getBitmap()
将非空。
网络响应返回后,只有下列情况之一会发生:
onResponse(响应,FALSE)
将在拍摄时加载调用,或者
onErrorResponse
如果有加载图像错误就会被调用。
块引用>在此基础上有可能的响应三种模式。我已经测试,并在一个示例应用程序证实了这些。
图像的是的从缓存加载
在这种情况下,将有一个呼叫:
onRespsonse(响应,真)
将被调用,你可以从response.getBitmap图像()
。的图像的不的从缓存中加载,而是的从网络上加载
在这种情况下,将有两个呼叫:
首先,
onRespsonse(响应,真)
将被调用,而response.getBitmap()
将空
。然后,
onRespsonse(响应,FALSE)
将被调用,你可以从response.getBitmap图像()
。图像的不的从缓存中加载,的不的从网络上加载
在这种情况下,将有两个呼叫:
首先,
onRespsonse(响应,真)
将被调用,而response.getBitmap()
将空
。然后,
onErrorResponse(错误)
将被调用,并且错误的详细信息可从发现错误
(这将是一个实例VolleyError
)。在你的情况,以下code段将帮助你跳过第一个空的回应:
@覆盖
公共无效onResponse(ImageLoader.ImageContainer响应,布尔isImmediate){
//跳过缓存失败
如果(isImmediate&安培;&安培; response.getBitmap()== NULL)回报; // ...
}希望这有助于澄清为什么你可能会得到两个响应您的要求。
I use Volley to do some intent request. I think the onResponse method should be called only one time when get response, but I found it called twice.
here is my code:
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 print 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:
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)
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.
The image is loaded from the cache
In this case there will be one call:
onRespsonse(response, true)
will be called, and you can get the image fromresponse.getBitmap()
.The image is not loaded from cache, and is loaded from the network
In this case there will be two calls:
First,
onRespsonse(response, true)
will be called, andresponse.getBitmap()
will benull
.Then,
onRespsonse(response, false)
will be called, and you can get the image fromresponse.getBitmap()
.The image is not loaded from cache, and is not loaded from the network
In this case there will be two calls:
First,
onRespsonse(response, true)
will be called, andresponse.getBitmap()
will benull
.Then,
onErrorResponse(error)
will be called, and details of the error can be found fromerror
(which will be an instance ofVolleyError
).
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屋!