我正在开发一个应用程序,它将来自网站的json数据列表加载到列表中。在该列表中,每个项目都有一个与之关联的图像。 MediaAdapter适用于每一行,并为每个图像调用异步加载。
问题来得不正常。目前列表中有3个项目(和3个图像)。有时我会收到1张图片,其他时间2.我很少收到所有3张图片。我已经设置了日志记录,并且在没有破解开放android-async-http的情况下跟踪了我的内容。
在下面的日志记录中,请注意为joe_thumb__804587.jpg调用onStart,但我从未收到过该请求的异常或任何进一步更新。
这是一些代码和一些日志记录。
媒体适配器(注意:MediaLoader.fetchBinary传递给BinaryNetworkClass):
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null){
convertView = ((Activity)this.getContext()).getLayoutInflater()
.inflate(R.layout.media_list_item, null);
}
TextView title = (TextView)convertView.findViewById(R.id.title);
TextView keywords = (TextView) convertView.findViewById(R.id.keywords);
ImageView thumb = (ImageView) convertView.findViewById(R.id.photo);
title.setText(getItem(position).getTitle());
keywords.setText(getItem(position).getKeywords());
fetchThumbnailsInView(thumb, getItem(position).getDefaultThumbnailUrl());
return convertView;
}
private void fetchThumbnailsInView(final ImageView imageView, final String url) {
String[] split = url.split("/");
final String fileName = split[split.length - 1];
MediaLoader.fetchBinary(url, new NetworkClientListener() {
@Override
public void success(Object data) {
if (data instanceof byte[]) {
byte[] imageData = (byte[]) data;
Bitmap b = BitmapFactory.decodeStream(new ByteArrayInputStream(imageData));
imageView.setImageBitmap(b);
Log.d(TAG, "Setting fileName: " + fileName + " for imageView: " + imageView.toString());
}
else{
Log.e(TAG, "Didn't receive a byte array for url: " + url);
}
}
@Override
public void failure(Throwable throwable) {
Log.e(TAG, Log.getStackTraceString(throwable));
}
});
}
BinaryNetworkClass:
public class BinaryNetworkClient {
private static final AsyncHttpClient mHttpClient = new AsyncHttpClient();
private static final String TAG = "BinaryNetworkClient";
public static void getData(final String url, final NetworkClientListener<byte[]> listener){
String[] split = url.split("/");
final String fileName = split[split.length - 1];
Log.d(TAG, "Requesting BINARY data for fileName: " + fileName);
mHttpClient.get(url, new AsyncHttpResponseHandler(){
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
if (listener != null) {
Log.d(TAG, "Received BINARY data for fileName: " + fileName);
listener.success(responseBody);
}
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
if (listener != null) {
Log.e(TAG, "Requesting BINARY FAILED for fileName: " + fileName);
listener.failure(error);
}
}
@Override
public void onRetry() {
super.onRetry();
Log.i(TAG, "Retry called for fileName: " + fileName);
}
@Override
public void onStart() {
super.onStart();
Log.i(TAG, "Start called for fileName: " + fileName);
}
@Override
public void onFinish() {
super.onFinish();
Log.i(TAG, "Finish called for fileName: " + fileName);
}
@Override
public void onProgress(int bytesWritten, int totalSize) {
super.onProgress(bytesWritten, totalSize);
Log.i(TAG, "OnProgress called for fileName: " + fileName + " (" + bytesWritten + " / " + totalSize + ") ");
}
});
}
}
这是一些记录:
D/MARK﹕ Requesting BINARY data for fileName: joe_thumb__188454.jpg
D/MARK﹕ Requesting BINARY data for fileName: joe_thumb.jpg
D/MARK﹕ Requesting BINARY data for fileName: joe_thumb__804587.jpg
I/BinaryNetworkClient﹕ Start called for fileName: joe_thumb__188454.jpg
I/BinaryNetworkClient﹕ Start called for fileName: joe_thumb.jpg
I/BinaryNetworkClient﹕ OnProgress called for fileName: joe_thumb__188454.jpg (2470 / 31687)
I/BinaryNetworkClient﹕ OnProgress called for fileName: joe_thumb__188454.jpg (6566 / 31687)
I/BinaryNetworkClient﹕ OnProgress called for fileName: joe_thumb__188454.jpg (10662 / 31687)
I/BinaryNetworkClient﹕ OnProgress called for fileName: joe_thumb__188454.jpg (14758 / 31687)
I/BinaryNetworkClient﹕ OnProgress called for fileName: joe_thumb__188454.jpg (18854 / 31687)
I/BinaryNetworkClient﹕ OnProgress called for fileName: joe_thumb__188454.jpg (22950 / 31687)
I/BinaryNetworkClient﹕ OnProgress called for fileName: joe_thumb__188454.jpg (27046 / 31687)
I/BinaryNetworkClient﹕ OnProgress called for fileName: joe_thumb__188454.jpg (31142 / 31687)
I/BinaryNetworkClient﹕ OnProgress called for fileName: joe_thumb__188454.jpg (31687 / 31687)
D/BinaryNetworkClient﹕ Received BINARY data for fileName: joe_thumb__188454.jpg
D/MARK﹕ Setting fileName: joe_thumb__188454.jpg for imageView: android.widget.ImageView{52a39304 V.ED.... ......ID 0,33-345,228 #7f07003f app:id/photo}
I/BinaryNetworkClient﹕ Finish called for fileName: joe_thumb__188454.jpg
I/BinaryNetworkClient﹕ OnProgress called for fileName: joe_thumb.jpg (2470 / 31687)
I/BinaryNetworkClient﹕ OnProgress called for fileName: joe_thumb.jpg (6566 / 31687)
I/BinaryNetworkClient﹕ OnProgress called for fileName: joe_thumb.jpg (8150 / 31687)
I/BinaryNetworkClient﹕ OnProgress called for fileName: joe_thumb.jpg (12246 / 31687)
I/BinaryNetworkClient﹕ OnProgress called for fileName: joe_thumb.jpg (16342 / 31687)
I/BinaryNetworkClient﹕ OnProgress called for fileName: joe_thumb.jpg (18886 / 31687)
I/BinaryNetworkClient﹕ Start called for fileName: joe_thumb__804587.jpg
I/BinaryNetworkClient﹕ OnProgress called for fileName: joe_thumb.jpg (22982 / 31687)
I/BinaryNetworkClient﹕ OnProgress called for fileName: joe_thumb.jpg (27078 / 31687)
I/BinaryNetworkClient﹕ OnProgress called for fileName: joe_thumb.jpg (31174 / 31687)
I/BinaryNetworkClient﹕ OnProgress called for fileName: joe_thumb.jpg (31687 / 31687)
D/BinaryNetworkClient﹕ Received BINARY data for fileName: joe_thumb.jpg
D/dalvikvm﹕ GC_FOR_ALLOC freed 541K, 8% free 7376K/7996K, paused 3ms, total 4ms
D/MARK﹕ Setting fileName: joe_thumb.jpg for imageView: android.widget.ImageView{52a3b720 V.ED.... ......ID 0,33-345,228 #7f07003f app:id/photo}
I/BinaryNetworkClient﹕ Finish called for fileName: joe_thumb.jpg
答案 0 :(得分:0)
找到答案。
https://github.com/loopj/android-async-http/issues/536
需要更新1.4.5-SNAPSHOT库(我使用的是1.4.4)。
更新后,一切都按预期工作。