私は多くのImageViewアイテムを持つGridViewを持っています。アイテムのそれぞれに対して、HttpClientを使用してWebServiceからサムネイル画像を読み込みます。私はダウンロードしたすべてのイメージに対して新しいHttpClientを作成します。サムネイルのサイズは2〜4 KBです。私はダウンロードが遅いことに気付きました、画像は1ずつロードされ、それぞれが1秒でダウンロードされます。プロセスをスピードアップすることは可能ですか?android HttpClient多くの小さなサムネイル画像を読み込む
public Bitmap downloadPhoto(String url) {
try {
HttpParams params = new BasicHttpParams();
params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
HttpClient client = new DefaultHttpClient(params);
HttpUriRequest request = new HttpGet(url);
if (this.authToken != null) {
request.setHeader(AUTH_TOKEN_NAME, authToken);
}
request.setHeader(USER_AGENT_PROPERTY, AGENT_NAME);
HttpResponse response = client.execute(request);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
// read the content
long contentLenght = response.getEntity().getContentLength();
BufferedHttpEntity bufferedHttpEntity = new BufferedHttpEntity(response.getEntity());
Bitmap image = BitmapFactory.decodeStream(bufferedHttpEntity.getContent());
Log.e(TAG, "Bitmap != null " + (image != null));
return image;
} else {
Log.e(TAG, "HTTP ERROR while executing method: downloadImage: " + response.getStatusLine().getStatusCode());
}
} catch (Exception e) {
Log.e(TAG, "Exception while executing method: downloadImage: " + e.getMessage());
return null;
}
return null;
}
これにより、単一のIPに複数の接続が確立されます。これは、 "2つの接続制限ルール"に違反します。 – cubesoft
ここでは、ThreadSafeHttpClientConnManagerが最適です。次のリンクはそれに関するさらなる質問に答えています。http://stackoverflow.com/questions/1281219/best-practice-to-use-httpclient-in-multithreaded-environment – phanij