Asynchttpclient開源框架下載圖片和文本,於Volley和Glide開源框架的區別。

AsyncHttpClient是一款比較流行的Android異步網路加載庫,在github上的網址是:https://github.com/loopj/android-async-http
AsyncHttpClient和開源框架 Volley和Glide不一樣的是,不像Volley和Glide內部已經實現好了緩存策略,AsyncHttpClient自身沒有實現緩存策略。android

代碼以下:git

 1 package com.lixu.asynchttpclient;
 2 
 3 import org.apache.http.Header;
 4 import com.loopj.android.http.AsyncHttpClient;
 5 import com.loopj.android.http.AsyncHttpResponseHandler;
 6 import android.app.Activity;
 7 import android.graphics.Bitmap;
 8 import android.graphics.BitmapFactory;
 9 import android.os.Bundle;
10 import android.widget.ImageView;
11 import android.widget.TextView;
12 import android.widget.Toast;
13 
14 public class MainActivity extends Activity {
15     private TextView tv;
16     private ImageView iv;
17 
18     @Override
19     protected void onCreate(Bundle savedInstanceState) {
20         super.onCreate(savedInstanceState);
21         setContentView(R.layout.activity_main);
22 
23         tv = (TextView) findViewById(R.id.tv);
24 
25         iv = (ImageView) findViewById(R.id.iv);
26         // 文本和圖片的網絡地址
27         String url1 = "http://www.baidu.com";
28 
29         String url2 = "https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=1183223528,3058066243&fm=116&gp=0.jpg";
30 
31         loadtxt(url1);
32 
33         loadimage(url2);
34 
35     }
36 
37     private void loadtxt(String url1) {
38         // 建立AsyncHttpClient對象
39         AsyncHttpClient ahc = new AsyncHttpClient();
40         ahc.get(url1, new AsyncHttpResponseHandler() {
41 
42             @Override
43             public void onFailure(int arg0, Header[] arg1, byte[] arg2, Throwable arg3) {
44                 Toast.makeText(getApplicationContext(), "錯誤!!", 0).show();
45             }
46 
47             @Override
48             public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
49                 // 將獲取到的byte[]數組轉換成String字符串
50                 tv.setText(new String(arg2));
51             }
52         });
53     }
54 
55     private void loadimage(String url2) {
56 
57         AsyncHttpClient ahc = new AsyncHttpClient();
58         ahc.get(url2, new AsyncHttpResponseHandler() {
59 
60             @Override
61             public void onFailure(int arg0, Header[] arg1, byte[] arg2, Throwable arg3) {
62 
63                 Toast.makeText(getApplicationContext(), "錯誤!!", 0).show();
64             }
65 
66             @Override
67             public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
68                 // 建立bitmap圖片工廠
69                 BitmapFactory bf = new BitmapFactory();
70                 // 用圖片工廠將字節數組轉換成bitmap圖片
71                 Bitmap bitmap = bf.decodeByteArray(arg2, 0, arg2.length);
72 
73                 iv.setImageBitmap(bitmap);
74 
75             }
76 
77         });
78 
79     }
80 }

xml文件:github

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:id="@+id/LinearLayout1"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:orientation="vertical" >
 7 
 8     <ScrollView
 9         android:layout_width="match_parent"
10         android:layout_height="match_parent"
11         android:layout_weight="1" >
12 
13         <TextView
14             android:id="@+id/tv"
15             android:layout_width="match_parent"
16             android:layout_height="match_parent" />
17     </ScrollView>
18 
19     <ImageView
20         android:id="@+id/iv"
21         android:layout_width="match_parent"
22         android:layout_height="match_parent"
23         android:layout_weight="1" />
24 
25 </LinearLayout>

運行效果:apache

相關文章
相關標籤/搜索