(尊重勞動成果,轉載請注明出處,謝謝。http://www.cnblogs.com/clear5/)
其實android-async-http與網絡數據的交互跟與服務器交互都非常簡單和相似。
主要用到兩個類:
com.loopj.android.http.BinaryHttpResponseHandler; //二進制流文件(圖片,音樂,視頻等)處理類
com.loopj.android.http.TextHttpResponseHandler; //文本文件處理類
下面開始貼代碼,相關指示在注釋里標注了。
我這里使用的開發工具為Android Studio。
首先是新建一個project,我這里叫AsyncimageDemo,然后注意導包:android-async-http-1.4.7.jar
先把布局文件搭建好,修改activity_main.xml內容:
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical"> 6 <!-- http://www.cnblogs.com/clear5/ --> 7 <LinearLayout 8 android:id="@+id/btn_line" 9 android:layout_width="match_parent" 10 android:layout_height="wrap_content" 11 android:gravity="center" 12 android:orientation="horizontal"> 13 14 <Button 15 android:id="@+id/img_btn" 16 android:layout_width="wrap_content" 17 android:layout_height="wrap_content" 18 android:text="點擊獲取網絡圖片"/> 19 <Button 20 android:id="@+id/web_btn" 21 android:layout_width="wrap_content" 22 android:layout_height="wrap_content" 23 android:text="點擊獲取網頁數據"/> 24 25 </LinearLayout> 26 27 <ImageView 28 android:id="@+id/img_view" 29 android:adjustViewBounds="true" 30 android:layout_width="match_parent" 31 android:layout_height="wrap_content" /> 32 33 <WebView 34 android:id="@+id/web_view" 35 android:layout_width="match_parent" 36 android:layout_height="wrap_content"/> 37 38 </LinearLayout>
布局顯示圖如下:
修改MainActivity.java文件內容:
1 package com.app.asyncimagedemo; 2 3 import android.app.Activity; 4 import android.graphics.Bitmap; 5 import android.graphics.BitmapFactory; 6 import android.os.Bundle; 7 8 import android.util.Log; 9 import android.view.View; 10 import android.view.Window; 11 import android.webkit.WebView; 12 import android.widget.Button; 13 import android.widget.ImageView; 14 import android.widget.Toast; 15 16 import com.loopj.android.http.AsyncHttpClient; 17 import com.loopj.android.http.BinaryHttpResponseHandler; 18 import com.loopj.android.http.TextHttpResponseHandler; 19 20 import org.apache.http.Header; 21 22 /** 23 * http://www.cnblogs.com/clear5/ 24 */ 25 public class MainActivity extends Activity { 26 27 final static String TAG = MainActivity.class.getCanonicalName(); 28 //創建AsyncHttpClient對象 29 private static AsyncHttpClient client = new AsyncHttpClient(); 30 31 private ImageView imageView; 32 private WebView webView; 33 private Button imgBtn; 34 private Button webBtn; 35 36 @Override 37 protected void onCreate(Bundle savedInstanceState) { 38 super.onCreate(savedInstanceState); 39 requestWindowFeature(Window.FEATURE_NO_TITLE); 40 setContentView(R.layout.activity_main); 41 42 //加載布局控件對象 43 imageView = (ImageView) findViewById(R.id.img_view); 44 imgBtn = (Button) findViewById(R.id.img_btn); 45 46 webView = (WebView) findViewById(R.id.web_view); 47 webBtn = (Button) findViewById(R.id.web_btn); 48 49 //圖片地址 50 final String imgUrl = "http://imgsrc.baidu.com/forum/pic/item/503d269759ee3d6dc4ee119543166d224f4ade40.jpg"; 51 //網頁地址 52 final String webUrl = "http://m.cnblogs.com/?u=clear5"; 53 54 //點擊獲取圖片資源按鈕事件 55 imgBtn.setOnClickListener(new View.OnClickListener() { 56 @Override 57 public void onClick(View view) { 58 59 imageView.setVisibility(View.VISIBLE);//獲取圖片資源時,圖片資源顯示 60 webView.setVisibility(View.GONE);//獲取圖片資源時,網頁資源隱藏 61 62 haveImg(imgUrl); 63 } 64 }); 65 66 //點擊獲取網頁資源按鈕事件 67 webBtn.setOnClickListener(new View.OnClickListener() { 68 @Override 69 public void onClick(View view) { 70 71 imageView.setVisibility(View.GONE);//獲取網頁資源時,圖片資源隱藏 72 webView.setVisibility(View.VISIBLE);//獲取網頁資源時,網頁資源顯示 73 74 haveWeb(webUrl); 75 } 76 }); 77 } 78 //獲取網頁 79 private void haveWeb(String webUrl) { 80 81 client.get(webUrl, new TextHttpResponseHandler() { 82 //獲取失敗 83 @Override 84 public void onFailure(int i, Header[] headers, String s, Throwable throwable) { 85 86 Log.d(TAG,throwable+""); 87 88 Toast.makeText(MainActivity.this,"對不起,讀取失敗……狀態碼為:"+i,Toast.LENGTH_LONG).show(); 89 } 90 //獲取成功 91 @Override 92 public void onSuccess(int i, Header[] headers, String s) { 93 94 webView.getSettings().setJavaScriptEnabled(true);//是否支持動態加載js 95 webView.getSettings().setDefaultTextEncodingName("UTF-8");//編碼方式 96 //以下兩行代碼是使網頁能夠自適屏 97 webView.getSettings().setUseWideViewPort(true); 98 webView.getSettings().setLoadWithOverviewMode(true); 99 100 webView.loadData(s,"text/html;charset=UTF-8",null);//加載網頁 101 } 102 }); 103 } 104 105 //獲取圖片 106 private void haveImg(String imgUrl) { 107 108 client.get(imgUrl, new BinaryHttpResponseHandler() {//BinaryHttpResponseHandler是用來讀取二進制文件(比如圖片)的 109 //成功 110 @Override 111 public void onSuccess(int i, Header[] headers, byte[] bytes) { 112 113 Bitmap b = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);//解析圖片 114 //將圖片資源加載到ImageView控件中 115 imageView.setImageBitmap(b); 116 117 } 118 //失敗 119 @Override 120 public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) { 121 122 Log.d(TAG,throwable+""); 123 124 Toast.makeText(MainActivity.this,"對不起,讀取失敗……狀態碼為:"+i,Toast.LENGTH_LONG).show(); 125 126 } 127 }); 128 } 129 130 131 }
這樣的話,我們就實現了android-async-http框架與網絡交互數據的功能。當然還有一步重要的就是一定要在AndroidManifest.xml中加入獲取網絡權限的代碼:
<uses-permission android:name="android.permission.INTERNET"/>
步驟都完成后,我們就可以運行程序看效果了。
啟動界面:
點擊獲取圖片資源按鈕,獲取圖片加載到屏幕中:
然后再點擊獲取網頁數據按鈕,獲取到網頁加載到屏幕:
這樣就完成了android-async-http與網絡的數據交互,是不是很簡單?