AsyncHttpClient 极其好用,采用回调的方式并且是异步的,UI线程已经封装好。 https://github.com/loopj/android-async-http
这个例子较为简单。
先看运行结果
点击按钮 从网络上获取图片和baidu首页显示出来.
项目是直接用的源码,为了研究源码使用的。
public class HTTPHelper {
public HTTPHelper() {
}
public void get(String url,ResponseHandlerInterface callback){
AsyncHttpClient client = new AsyncHttpClient();
client.get(url,callback);
}
}
public class MyActivity extends Activity {
final static String TAG = MyActivity.class.getCanonicalName();
private final static String URL = "http://www.baidu.com";
private final static String IMAGE_URL = "http://preview.quanjing.com/glow_foto001/gcr211129740.jpg";//图片资源
private WebView webView;
private ImageView imageView;
private HTTPHelper helper;
public MyActivity() {
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
webView = (WebView) findViewById(R.id.webview);
imageView = (ImageView) findViewById(R.id.img);
helper = new HTTPHelper();
findViewById(R.id.btn_http_get).setOnClickListener(
new OnClickListener() {
public void onClick(View view) {
helper.get(URL, textCallback);//获取文本
}
});
findViewById(R.id.btn_http_get_img).setOnClickListener(
new OnClickListener() {
public void onClick(View view) {
helper.get(IMAGE_URL, binaryCallback);//获取图片
}
});
}
// 获取图片流
BinaryHttpResponseHandler binaryCallback = new BinaryHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers,
byte[] binaryData) {
Log.d(TAG, "onSuccess:" + statusCode + " binaryData:"
+ binaryData.length);
MyActivity.this.updateImage(binaryData);
}
@Override
public void onFailure(int statusCode, Header[] headers,
byte[] binaryData, Throwable error) {
Log.e(TAG, "onFailure:" + error + " statusCode:" + statusCode);
}
};
// 获取文本流
TextHttpResponseHandler textCallback = new TextHttpResponseHandler() {
@Override
public void onFailure(int statusCode, Header[] headers,
String responseString, Throwable throwable) {
Log.e(TAG, "onFailure:" + throwable + " statusCode:" + statusCode);
}
@Override
public void onSuccess(int statusCode, Header[] headers,
String responseString) {
Log.d(TAG, "onSuccess:" + statusCode + " responseString:"
+ responseString.length());
MyActivity.this.updateWebView(responseString);
}
};
public void updateImage(byte[] data) {
Bitmap b = BitmapFactory.decodeByteArray(data, 0, data.length);
imageView.setImageBitmap(b);
}
public void updateWebView(String data) {
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setDefaultTextEncodingName("UTF -8");
webView.loadData(data, "text/html; charset=UTF-8", null);
}
}
BinaryHttpResponseHandler 是Binary的请求回调,用在获取图片比较合适
TextHttpResponseHandler 是字符串文本类的请求回调,它的子类还有json获取的回调接口
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/btn_http_get"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="http get text" />
<Button
android:id="@+id/btn_http_get_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="http get image" />
<WebView
android:layout_marginTop="5dp"
android:layout_width="fill_parent"
android:layout_height="240dp"
android:id="@+id/webview"
/>
<ImageView
android:id="@+id/img"
android:layout_marginTop="5dp"
android:layout_width="200dp"
android:layout_height="150dp"
android:scaleType="fitCenter" />
</LinearLayout>
That's all right~