AsyncHttpClient是一款比较流行的Android异步网路加载库,在github上的网址是:
https://github.com/loopj/android-async-http
但是AsyncHttpClient和另外比较著名的Android异步网络库 Volley和Glide不同的是,不像Volley和Glide内部已经实现好了缓存策略,AsyncHttpClient自身没有实现缓存策略。
实验可以表明这点。
实验方法:我写一个简单的Android小程序,仅仅加载一张从网络获取的图片(我的CSDN博客头像),在联网时候启动这个程序,然后关闭手机设备的所以网络连接,此时再次启动该程序。
先给出测试的程序所有代码:
但是AsyncHttpClient和另外比较著名的Android异步网络库 Volley和Glide不同的是,不像Volley和Glide内部已经实现好了缓存策略,AsyncHttpClient自身没有实现缓存策略。
实验可以表明这点。
实验方法:我写一个简单的Android小程序,仅仅加载一张从网络获取的图片(我的CSDN博客头像),在联网时候启动这个程序,然后关闭手机设备的所以网络连接,此时再次启动该程序。
先给出测试的程序所有代码:
测试的主Activity:MainActivity.java
package com.zzw.testasynchttpclient;
import org.apache.http.Header;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView imageView = (ImageView) findViewById(R.id.imageView);
TextView textView = (TextView) findViewById(R.id.textView);
// 设置下拉
textView.setMovementMethod(ScrollingMovementMethod.getInstance());
useAsyncHttpClientGetString("http://www.cnblogs.com/zzw1994", textView);
useAsyncHttpClientGetImage("http://pic.cnblogs.com/avatar/822717/20151120000857.png", imageView);
}
private void useAsyncHttpClientGetString(String url, TextView textView) {
final TextView mTextView = textView;
// 第一步,得到AsyncHttpClient对象
AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
// 第二步,通过url获取数据
asyncHttpClient.get(url, new AsyncHttpResponseHandler() {
// 成功后返回的数据处理
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] response) {
mTextView.setText(new String(response));
}
// 失败后返回的数据处理
@Override
public void onFailure(int statusCode, Header[] headers, byte[] response, Throwable error) {
Toast.makeText(getApplicationContext(), "请求失败", 1).show();
}
});
}
private void useAsyncHttpClientGetImage(String url, ImageView imageView) {
final ImageView mImageView = imageView;
// 第一步,得到AsyncHttpClient对象
AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
// 第二步,通过url获取数据
asyncHttpClient.get(url, new AsyncHttpResponseHandler() {
// 成功后返回的数据处理
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] response) {
BitmapFactory bitmapFactor = new BitmapFactory();
Bitmap bitmap = bitmapFactor.decodeByteArray(response, 0, response.length);
mImageView.setImageBitmap(bitmap);
}
// 失败后返回的数据处理
@Override
public void onFailure(int statusCode, Header[] headers, byte[] response, Throwable error) {
Toast.makeText(getApplicationContext(), "加载失败", 1).show();
}
});
}
}
activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:scrollbars="vertical"
android:singleLine="false" />
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
activity_main.xml
最后不要忘记加权限:
1 <uses-permission android:name="android.permission.INTERNET" />