必須知道的Android網路框架大全!值得一看良心文章!
導語
大通常在 Android 中進行網路連線一般使用 Scoket 和 HTTP兩種方式。而 HTTP 請求方式比 Scoket 多得多,HTTP 請求一般採用原生的 HttpClient 和 HttpUrlConnection 的兩種網路訪問方式。可是在 Android 5.0 的時候 Google 就不推薦使用 HttpClient 了,到了 Android 6.0 (api 23) SDK,不再提供 org.apache.http.* (只保留幾個類), 如果使用了 httpClient 相關類的庫專案,如 android-async-http 等等,會出現有一些類找不到的錯誤。而從Android4.4開始HttpURLConnection的底層實現採用的是okHttp。
okHttp、volley、android-async-http 對比如下。
- volley是一個簡單的非同步http庫,僅此而已。缺點是不支援同步,這點會限制開發模式;不能post大資料,不適合用來上傳檔案。
- android-async-http。與volley一樣是非同步網路庫,但volley是封裝的httpUrlConnection,它是封裝的httpClient,而android平臺不推薦用HttpClient了,所以這個庫已經不適合android平臺了。
- okhttp是高效能的http庫,和volley一樣實現了http協議的快取,支援同步、非同步,而且實現了spdy、http2、websocket協議,Api簡潔易用。
所以,還是okHttp 才是目前最值得推薦的一款網路通訊框架,簡潔易用,功能強大。但是,為了普及知識,還是要把曾經為Android效力過的網路請求方式都寫出來。
整合所有請求網路的方式及流行框架總結練習:該demo包含原生的HttpClient、HttpUrlConntection的網路請求方式。以及流行的框架如 :Volley、OkHttp、RxJava的練習。
正文
HttpUrlConnection
最早學Android的時候用的網路請求是HttpUrlConnection,在android 2.2及以下版本中HttpUrlConnection存在著一些bug,所以建議在android 2.3以後使用HttpUrlConnection,之前使用HttpClient。在Android 2.2版本之前,HttpClient擁有較少的bug,因此使用它是最好的選擇。而在Android 2.3版本及以後,HttpURLConnection則是最佳的選擇。它的API簡單,體積較小,因而非常適用於Android專案。壓縮和快取機制可以有效地減少網路訪問的流量,在提升速度和省電方面也起到了較大的作用。對於新的應用程式應該更加偏向於使用HttpURLConnection,因為在以後的工作當中我們也會將更多的時間放在優化HttpURLConnection上面。
特點:
- 比較輕便,靈活,易於擴充套件。
- 在3.0後以及4.0中都進行了改善,如對HTTPS的支援。
- 在4.0中,還增加了對快取的支援。
使用:
- 首先我們需要獲取到一個HttpURLConnection例項,一般需要new出一個URL物件,並傳入目標網路地址,通過呼叫openConnection()方法獲得HttpURLConnection例項。
- 得到該例項後。我們需要設定一下http請求的的方法,這裡我們主要研究get和post,預設是使用get方法。get一般用於從伺服器獲取資料,post一般用於向伺服器提交資料,設定請求方法使用函式setRequestMethod(“POST”)進行設定。
- 此外可以進行一些請求的限制,比如連線超時的時間等,可以通過setConnectTimeout設定超時時間。
- 獲取伺服器返回的輸入流,使用getInputStream方法獲取。
- 讀取內容並處理。
- 關閉連線,通過呼叫disconnect方法關閉當前的連線。
- 關鍵程式碼如下 。
- 使用過程中不要忘記新增許可權。
<uses-permission android:name="android.permission.INTERNET"
- GET
public String get(String urlPath) { HttpURLConnection connection = null; InputStream is = null; try { URL url = new URL(urlPath); //獲得URL物件 connection = (HttpURLConnection) url.openConnection(); //獲得HttpURLConnection物件 connection.setRequestMethod("GET"); // 預設為GET connection.setUseCaches(false); //不使用快取 connection.setConnectTimeout(10000); //設定超時時間 connection.setReadTimeout(10000); //設定讀取超時時間 connection.setDoInput(true); //設定是否從httpUrlConnection讀入,預設情況下是true; if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) { //相應碼是否為200 is = connection.getInputStream(); //獲得輸入流 BufferedReader reader = new BufferedReader(new InputStreamReader(is)); //包裝位元組流為字元流 StringBuilder response = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { response.append(line); } return response.toString(); } } catch (Exception e) { e.printStackTrace(); } finally { if (connection != null) { connection.disconnect(); connection = null; } if (is != null) { try { is.close(); is = null; } catch (IOException e) { e.printStackTrace(); } } } return null; }
- POST
private String post(String urlPath, Map<String, String> params) { if (params == null || params.size() == 0) { return get(urlPath); } OutputStream os = null; InputStream is = null; HttpURLConnection connection = null; StringBuffer body = getParamString(params); byte[] data = body.toString().getBytes(); try { URL url = new URL(urlPath); //獲得URL物件 connection = (HttpURLConnection) url.openConnection(); //獲得HttpURLConnection物件 connection.setRequestMethod("POST"); // 設定請求方法為post connection.setUseCaches(false); //不使用快取 connection.setConnectTimeout(10000); //設定超時時間 connection.setReadTimeout(10000); //設定讀取超時時間 connection.setDoInput(true); //設定是否從httpUrlConnection讀入,預設情況下是true; connection.setDoOutput(true); //設定為true後才能寫入引數 connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); connection.setRequestProperty("Content-Length", String.valueOf(data.length)); os = connection.getOutputStream(); os.write(data); //寫入引數 if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) { //相應碼是否為200 is = connection.getInputStream(); //獲得輸入流 BufferedReader reader = new BufferedReader(new InputStreamReader(is)); //包裝位元組流為字元流 StringBuilder response = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { response.append(line); } return response.toString(); } } catch (Exception e) { e.printStackTrace(); } finally { //關閉 if (os != null) { try { os.close(); } catch (IOException e) { e.printStackTrace(); } } if (is != null) { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } if (connection != null) { connection.disconnect(); connection = null; } } return null; } private StringBuffer getParamString(Map<String, String> params) { StringBuffer result = new StringBuffer(); Iterator<Map.Entry<String, String>> iterator = params.entrySet().iterator(); while (iterator.hasNext()) { Map.Entry<String, String> param = iterator.next(); String key = param.getKey(); String value = param.getValue(); result.append(key).append('=').append(value); if (iterator.hasNext()) { result.append('&'); } } return result; }
HttpClient
簡述
高效穩定,但是維護成本高昂,故開發團隊不願意維護該庫而是轉投更為輕便的HttpUrlConnection。
用法
- HttpClient是一個介面,因此無法直接建立它的例項,一般都是建立一個DefaultHttpClient例項。
- 如果要發起Get請求,需要建立一個HttpGet物件,並傳入請求地址。
- 如果要發起Post請求,需要建立一個HttpPost物件。並傳入請求地址,通過setEntity函式設定請求引數。
- 呼叫execute方法,傳入HttpGet或者HttpPost例項,執行後返回HttpResponse物件,判斷響應狀態碼。
解析響應結果,通過呼叫getEntity函式獲得一個HttpEntity物件,之後可以通過EntityUtils.toString方法將其轉換為字串。
由於在android2.3之後就被HttpUrlConnection取代了,這裡也不過多介紹了,不過當初學習它的時候還沒接觸到其他庫。下面是使用GET方法:
privateString get(String url){
HttpClient client=null;
HttpGet request=null;try{
client=newDefaultHttpClient();
request=newHttpGet(url);
HttpResponse response=client.execute(request);
if(response.getStatusLine().getStatusCode()==HttpStatus.SC_OK){
String result=EntityUtils.toString(response.getEntity(),"UTF-8");returnresult;
}
}catch(IOException e) {
e.printStackTrace();
}returnnull;
}
特點
- 所以請求在子執行緒中完成,請求回撥在呼叫該請求的執行緒中完成;
- 使用執行緒池;
- 使用RequestParams類封裝請求引數;
- 支援檔案上傳;
- 持久化cookie到SharedPreferences,個人感覺這一點也是這個庫的重要特點,可以很方便的完成一些模擬登入;
- 支援json;
- 支援HTTP Basic Auth;
用法
編寫一個靜態的HttpClientpackage cn.edu.zafu.http;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
/**
* Created by lizhangqu on 2015/5/7.
*/
public class TestClient {
private static final String BASE_URL = "http://121.41.119.107/";
private static AsyncHttpClient client = new AsyncHttpClient();
public static void get(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
client.get(getAbsoluteUrl(url), params, responseHandler);
}
public static void post(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
client.post(getAbsoluteUrl(url), params, responseHandler);
}
private static String getAbsoluteUrl(String relativeUrl) {
return BASE_URL + relativeUrl;
}
}
- 呼叫get或者post方法
引數通過RequestParams傳遞,沒有引數則傳遞null
RequestParams params = new RequestParams();
params.put("","");
- 如果要儲存cookie,在發起請求之前呼叫以下程式碼
PersistentCookieStore myCookieStore = new PersistentCookieStore(this);
client.setCookieStore(myCookieStore);
之後請求所得到的cookie都會自動持久化;如果要自己新增cookie,則呼叫以下程式碼
BasicClientCookie newCookie = new BasicClientCookie("cookiesare", "awesome");
newCookie.setVersion(1);
newCookie.setDomain("mydomain.com");
newCookie.setPath("/");
myCookieStore.addCookie(newCookie);
- 在回撥函式中處理返回結果
private void get(){
TestClient.get("test/index.php", null, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
}
});
}
private void post(){
RequestParams params = new RequestParams();
params.put("user","asas");
params.put("pass","12121");
params.put("time","1212121");
TestClient.post("test/login.php", params, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
}
});
}
Volley
既然在android2.2之後不建議使用Http Client,那麼有沒有一個庫是android2.2及以下版本使用Http Client,而android2.3及以上版本使用HttpUrlConnection的呢,答案是肯定的,就是Volley,它是android開發團隊在2013年Google I/O大會上推出了一個新的網路通訊框架。 Volley可以說是把AsyncHttpClient和Universal-Image-Loader的優點集於了一身,既可以像AsyncHttpClient一樣非常簡單地進行HTTP通訊,也可以像Universal-Image-Loader一樣輕鬆載入網路上的圖片。除了簡單易用之外,Volley在效能方面也進行了大幅度的調整,它的設計目標就是非常適合去進行資料量不大,但通訊頻繁的網路操作,而對於大資料量的網路操作,比如說下載檔案等,Volley的表現就會非常糟糕。
特點
- Volley的優勢在於處理小檔案的http請求;
- 在Volley中也是可以使用Okhttp作為傳輸層
- Volley在處理高解析度的影象壓縮上有很好的支援;
- NetworkImageView在GC的使用模式上更加保守,在請求清理上也更加積極,networkimageview僅僅依賴於強大的記憶體引用,並當一個新請求是來自ImageView或ImageView離開螢幕時 會清理掉所有的請求資料。
用法
- 建立一個RequestQueue物件。
- 建立一個Request物件。
- 將Request物件新增到RequestQueue裡面。
- 下面一步一步來學習其用法
GET
private void get(){
RequestQueue queue= Volley.newRequestQueue(getApplicationContext());
String url="http://121.41.119.107/test/index.php";
StringRequest request=new StringRequest(url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d("TAG",response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
queue.add(request);
}
POST
通過指定請求方法為Request.Method.POST使其成為post請求,然後重新getParams方法設定請求引數。當發出POST請求的時候,Volley會嘗試呼叫StringRequest的父類——Request中的getParams()方法來獲取POST引數
private void post() {
RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
String url = "http://121.41.119.107/test/login.php";
StringRequest request = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d("TAG", response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}) {
//重寫getParams方法設定引數
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("user", "asas");
params.put("pass", "12121");
params.put("time", "1212121");
return params;
}
};
queue.add(request);
}
載入影象的方法和前面類似,只不過不在是StringRequest而是ImageRequest。
private void getImage() {
RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
String url = "https://www.baidu.com/img/bdlogo.png";
//第三第四個引數分別用於指定允許圖片最大的寬度和高度,如果指定的網路圖片的寬度或高度大於這裡的最大值,則會對圖片進行壓縮,指定成0的話就表示不管圖片有多大,都不會進行壓縮。
//第五個引數就是ImageView裡中的屬性ScaleType
//第六個引數用於指定圖片的顏色屬性
ImageRequest request = new ImageRequest(url, new Response.Listener<Bitmap>() {
@Override
public void onResponse(Bitmap response) {
ImageView iv= (ImageView) findViewById(R.id.iv);
iv.setImageBitmap(response);
}
}, 0, 0, ImageView.ScaleType.CENTER, Bitmap.Config.ARGB_8888, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
queue.add(request);
}
其實載入圖片的功能還遠遠不止這些,使用ImageLoader可以實現對圖片的快取,還可以過濾重複連結,避免傳送重複的請求
ImageLoader的使用方法概括為以下幾步
1. 建立一個RequestQueue物件。
2. 建立一個ImageLoader物件。
3. 獲取一個ImageListener物件。
4. 呼叫ImageLoader的get()方法載入網路上的圖片。
//繼承ImageCache,使用LruCache實現快取
public class BitmapCache implements ImageLoader.ImageCache {
private LruCache<String, Bitmap> mCache;
public BitmapCache() {
int maxSize = 10 * 1024 * 1024;
mCache = new LruCache<String, Bitmap>(maxSize) {
@Override
protected int sizeOf(String key, Bitmap bitmap) {
return bitmap.getRowBytes() * bitmap.getHeight();
}
};
}
@Override
public Bitmap getBitmap(String url) {
return mCache.get(url);
}
@Override
public void putBitmap(String url, Bitmap bitmap) {
mCache.put(url, bitmap);
}
}
private void getImageByImageLoader() {
ImageView iv= (ImageView) findViewById(R.id.iv);
RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
String url = "https://www.baidu.com/img/bdlogo.png";
ImageLoader loader=new ImageLoader(queue,new BitmapCache() );
// 第一個引數指定用於顯示圖片的ImageView控制元件
// 第二個引數指定載入圖片的過程中顯示的圖片
// 第三個引數指定載入圖片失敗的情況下顯示的圖片
ImageLoader.ImageListener listener=ImageLoader.getImageListener(iv,R.mipmap.ic_launcher,R.mipmap.ic_launcher);
// 呼叫ImageLoader的get()方法來載入圖片
// 第一個引數就是圖片的URL地址
// 第二個引數則是剛剛獲取到的ImageListener物件
// 如果想對圖片的大小進行限制,也可以使用get()方法的過載,指定圖片允許的最大寬度和高度,即通過第三第四個引數指定
loader.get(url,listener);
}
OkHttp
簡述
是一個 Java 的 HTTP+SPDY 客戶端開發包,同時也支援 Android。需要Android 2.3以上。
特點
- OKHttp是Android版Http客戶端。非常高效,支援SPDY、連線池、GZIP和 HTTP 快取。
- 預設情況下,OKHttp會自動處理常見的網路問題,像二次連線、SSL的握手問題。
- 如果你的應用程式中集成了OKHttp,Retrofit預設會使用OKHttp處理其他網路層請求。
- 從Android4.4開始HttpURLConnection的底層實現採用的是okHttp.
- 新建一個OkHttpClient物件
- 通過Request.Builder物件新建一個Request物件
返回執行結果
- GET
private String get(String url) { OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder() .url(url) .build(); Response response = null; try { response = client.newCall(request).execute(); return response.body().string(); } catch (IOException e) { e.printStackTrace(); } return null; }
- POST
POST需要使用RequestBody物件,之後再構建Request物件時呼叫post函式將其傳入即可
private String post(String url) {
OkHttpClient client = new OkHttpClient();
RequestBody formBody = new FormEncodingBuilder()
.add("user", "Jurassic Park")
.add("pass", "asasa")
.add("time", "12132")
.build();
Request request = new Request.Builder()
.url(url)
.post(formBody)
.build();
Response response = null;
try {
response = client.newCall(request).execute();
return response.body().string();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
此外,post的使用方法還支援檔案等操作,具體使用方法有興趣的可以自行查閱
- 對Gson的支援
private Person gson(String url){ OkHttpClient client = new OkHttpClient(); Gson gson = new Gson(); Request request = new Request.Builder() .url(url) .build(); Response response = null; try { response = client.newCall(request).execute(); Person person = gson.fromJson(response.body().charStream(), Person.class); return person; } catch (IOException e) { e.printStackTrace(); } return null; }
- 非同步操作
以上的兩個例子必須在子執行緒中完成,同時okHttp還提供了非同步的方法呼叫,通過使用回撥來進行非同步呼叫,然後okHttp的回撥依然不在主執行緒中,因此該回調中不能操作UI
private void getAsync(String url) {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(url)
.build();
Response response = null;
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {
}
@Override
public void onResponse(Response response) throws IOException {
String result = response.body().string();
Toast.makeText(getApplicationContext(),result,Toast.LENGTH_SHORT).show();
//不能操作ui,回撥依然在子執行緒
Log.d("TAG", result);
}
});
}
okHttp的使用還有很多內容,這裡也不過多介紹,更多內容,參考官方網址
系統文章推薦:
Retrofit
特點
- 效能最好,處理最快
- 使用REST API時非常方便;
- 傳輸層預設就使用OkHttp;
- 支援NIO;
- 擁有出色的API文件和社群支援
- 速度上比volley更快;
- 如果你的應用程式中集成了OKHttp,Retrofit預設會使用OKHttp處理其他網路層請求。
- 預設使用Gson
使用
Retrofit支援同步和非同步兩種方式,在使用時,需要將請求地址轉換為介面,通過註解來指定請求方法,請求引數,請求頭,返回值等資訊。還是使用之前的person的那段json值,get請求到伺服器後從資料庫查詢資料,返回值為查詢到的資料,post請求向伺服器提交一條資料,返回值為提交的資料。
首先,完成請求所用的service,是一個interface,完全通過註解完成配置
package cn.edu.zafu.http;
import retrofit.Callback;
import retrofit.http.Field;
import retrofit.http.FormUrlEncoded;
import retrofit.http.GET;
import retrofit.http.Headers;
import retrofit.http.POST;
import retrofit.http.Path;
import retrofit.http.Query;
/**
* Created by lizhangqu on 2015/5/11.
*/
public interface PersonService {
@Headers({
"Cache-Control: max-age=640000",
"User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko"
})
//通過註解設定請求頭
@GET("/{test}/rest.php")
//設定請求方法為get,相對路徑為註解內內容,其中{test}會被@Path註解指定內容替換
Person getPerson(@Path("test") String dir,@Query("name") String name);
//@Query用於指定引數
@FormUrlEncoded
//urlencode
@POST("/test/rest1.php")
//post提交
Person updatePerson(@Field("name") String name,@Field("age") int age);
//@Field提交的域
@POST("/test/rest1.php")
void updatePerson(@Field("name") String name,@Field("age") int age, Callback<Person> callback);
//非同步回撥,不能指定返回值
}
- GET
使用時,通過RestAdapter的例項獲得一個介面的例項,其本質是動態代理,注意含有返回值的方法是同步的,不能UI執行緒中呼叫,應該在子執行緒中完成RestAdapter restAdapter = new RestAdapter.Builder() .setEndpoint("http://121.41.119.107") .build(); PersonService personService=restAdapter.create(PersonService.class); Person person=personService.getPerson("test","zhangsan"); Log.d("TAG",person.toString());
- POST
POST的呼叫同Get,獲得adapter後獲得一個代理物件,然後通過這個代理物件進行網路請求
Person person1=personService.updatePerson("lizhangqu", 12);
Log.d("TAG",person1.toString());
- 非同步請求
如果要使用非同步請求,需要將介面中的方法返回值修改會void,再加入回撥引數Callback,就如PersonService中第三個方法一樣,請求完成後會回撥該callback物件的success或者fail方法。
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint("http://121.41.119.107")
.build();
PersonService personService=restAdapter.create(PersonService.class);
personService.updatePerson("lizhangqu",23, new Callback<Person>() {
@Override
public void success(Person person, Response response) {
Log.d("TAG", person.toString());
}
@Override
public void failure(RetrofitError error) {
}
});
Retrofit的使用還有很多內容,剩下的就留給各位讀者自行去發現了,而其官網頁提供了及其詳細的說明。retrofit官網示例
這個庫裡面有很多精華的內容,建議各位仔細的閱讀下官方的文件。
系列推薦