0
我正在從一個網站加載一個json數據列表到應用程序的列表中。在該列表中,每個項目都有一個與其關聯的圖像。 MediaAdapter適用於每一行,併爲每個圖像調用異步加載。使用AsyncHttpResponseHandler多次調用
這個問題是不正確的。目前列表中有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