自己學習android也有一段時間了,在實際開發中,頻繁的接觸網絡請求,而網絡請求的方式很多,最常見的那么幾個也就那么幾個。本篇文章對常見的網絡請求庫進行一個總結。
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方法關閉當前的連接。
關鍵代碼如下
使用過程中不要忘記添加權限
1
|
<uses-permission android:name="android.permission.INTERNET">
|
- GET
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
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;
}
|
以上代碼參考了部分LessCode項目
HttpClient
特點
- 高效穩定,但是維護成本高昂,故android 開發團隊不願意在維護該庫而是轉投更為輕便的HttpUrlConnection
用法
- HttpClient是一個接口,因此無法直接創建它的實例,一般都是創建一個DefaultHttpClient實例
- 如果要發起Get請求,需要創建一個HttpGet對象,並傳入請求地址
- 如果要發起Post請求,需要創建一個HttpPost對象。並傳入請求地址,通過setEntity函數設置請求參數
- 調用execute方法,傳入HttpGet或者HttpPost實例,執行后返回HttpResponse對象,判斷響應狀態碼
- 解析響應結果,通過調用getEntity函數獲得一個HttpEntity對象,之后可以通過EntityUtils.toString方法將其轉換為字符串
由於在android2.3之后就被HttpUrlConnection取代了,這里也不過多介紹了,不過當初學習它的時候還沒接觸到其他庫,就感覺它好方便,下面簡單貼出使用方法
- GET
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
private String get(String url){
HttpClient client=null;
HttpGet request=null;
try {
client=new DefaultHttpClient();
request=new HttpGet(url);
HttpResponse response=client.execute(request);
if(response.getStatusLine().getStatusCode()== HttpStatus.SC_OK){
String result=EntityUtils.toString(response.getEntity(),"UTF-8");
return result;
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
|
- POST
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
private String post(String url,List<NameValuePair> params){
HttpClient client=null;
HttpPost request=null;
try {
client=new DefaultHttpClient();
request=new HttpPost(url);
request.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
HttpResponse response=client.execute(request);
if(response.getStatusLine().getStatusCode()== HttpStatus.SC_OK){
String result=EntityUtils.toString(response.getEntity(),"UTF-8");
return result;
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
|
以上代碼參考了郭霖《第一行代碼》——HttpClient部分
Android Asynchronous Http Client
Android Asynchronous Http Client一看名字就知道它是基於Http Client的,但是呢在安卓中Http Client已經廢棄了,所以也不建議使用這個庫了。然后仍然有一些可取的內容值得學習,所以這里也介紹一下。
特點
- 所以請求在子線程中完成,請求回調在調用該請求的線程中完成
- 使用線程池
- 使用RequestParams類封裝請求參數
- 支持文件上傳
- 持久化cookie到SharedPreferences,個人感覺這一點也是這個庫的重要特點,可以很方便的完成一些模擬登錄
- 支持json
- 支持HTTP Basic Auth
用法
- 編寫一個靜態的HttpClient
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
package 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
1
2
|
RequestParams params = new RequestParams();
params.put("","");
|
- 如果要保存cookie,在發起請求之前調用以下代碼
1
2
|
PersistentCookieStore myCookieStore = new PersistentCookieStore(this);
client.setCookieStore(myCookieStore);
|
之后請求所得到的cookie都會自動持久化
如果要自己添加cookie,則調用以下代碼
1
2
3
4
5
|
BasicClientCookie newCookie = new BasicClientCookie("cookiesare", "awesome");
newCookie.setVersion(1);
newCookie.setDomain("mydomain.com");
newCookie.setPath("/");
myCookieStore.addCookie(newCookie);
|
- 使用
在回調函數中處理返回結果
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
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參數
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
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。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
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()方法加載網絡上的圖片。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
//繼承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);
}
|
最后,Volley提供了一種自定義ImageView來加載圖片,其使用方法可概括為
1. 創建一個RequestQueue對象。
2. 創建一個ImageLoader對象。
3. 在布局文件中添加一個NetworkImageView控件。
4. 在代碼中獲取該控件的實例。
5. 設置要加載的圖片地址。
我們在布局中申明該控件
1
2
3
4
5
6
|
<com.android.volley.toolbox.NetworkImageView
android:id="@+id/network_image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
/>
|
在程序中實現加載
1
2
3
4
5
6
7
8
|
public void networkImageView(){
RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
ImageLoader loader=new ImageLoader(queue,new BitmapCache() );
NetworkImageView niv= (NetworkImageView) findViewById(R.id.network_image_view);
niv.setDefaultImageResId(R.mipmap.ic_launcher);//設置加載中顯示的圖片
niv.setErrorImageResId(R.mipmap.ic_launcher);//設置加載失敗時顯示的圖片
niv.setImageUrl("https://www.baidu.com/img/bdlogo.png", loader);//設置目標圖片的URL地址
}
|
- 自定義Request
在實際應用中,往往需要將http請求與json進行集成,而Volley正恰恰支持這樣的方式,不過需要我們自己自定義Request,這里我們使用google的Gson庫進行集成。
1. 繼承Request類
2. 重寫parseNetworkResponse,實現json與實體類轉換,由於實體類未定,所以采用泛型
下文用到的json字符串如下
1
|
{"name":"lizhangqu","age":16}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
package cn.edu.zafu.http;
import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.toolbox.HttpHeaderParser;
import com.google.gson.Gson;
import java.io.UnsupportedEncodingException;
/**
* Created by lizhangqu on 2015/5/7.
*/
public class GsonRequest<T> extends Request<T> {
private final Response.Listener<T> mListener;
private Gson mGson;
private Class<T> mClass;
public GsonRequest(int method, String url, Class<T> clazz, Response.Listener<T> listener,
Response.ErrorListener errorListener) {
super(method, url, errorListener);
mGson = new Gson();
mClass = clazz;
mListener = listener;
}
public GsonRequest(String url, Class<T> clazz, Response.Listener<T> listener,
Response.ErrorListener errorListener) {
this(Method.GET, url, clazz, listener, errorListener);
}
@Override
protected Response<T> parseNetworkResponse(NetworkResponse response) {
try {
String jsonString = new String(response.data,
HttpHeaderParser.parseCharset(response.headers));
return Response.success(mGson.fromJson(jsonString, mClass),
HttpHeaderParser.parseCacheHeaders(response));
} catch (UnsupportedEncodingException e) {
return Response.error(new ParseError(e));
}
}
@Override
protected void deliverResponse(T response) {
mListener.onResponse(response);
}
}
|
編寫測試實體類,兩個字段一個name一個age
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
package cn.edu.zafu.http;
/**
* Created by lizhangqu on 2015/5/7.
*/
public class Person {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
|
調用方法和StringRequest是一樣的。如下所示
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
private void json(){
RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
String url = "http://121.41.119.107/test/index.php";
GsonRequest<Person> request=new GsonRequest<Person>(url, Person.class, new Response.Listener<Person>() {
@Override
public void onResponse(Person response) {
Log.d("TAG",response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
queue.add(request);
}
|
以上代碼參考了郭霖三篇Volley博客文章,分別為
Android Volley完全解析(一),初識Volley的基本用法
Android Volley完全解析(二),使用Volley加載網絡圖片
Android Volley完全解析(三),定制自己的Request
okHttp
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
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函數將其傳入即可
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
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的支持
okHttp還自帶了對Gson的支持
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
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,完全通過注解完成配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
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線程中調用,應該在子線程中完成
1
2
3
4
5
6
|
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后獲得一個代理對象,然后通過這個代理對象進行網絡請求
1
2
|
Person person1=personService.updatePerson("lizhangqu", 12);
Log.d("TAG",person1.toString());
|
- 異步請求
如果要使用異步請求,需要將接口中的方法返回值修改會void,再加入回調參數Callback,就如PersonService中第三個方法一樣,請求完成后會回調該callback對象的success或者fail方法。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
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的使用還有很多內容,剩下的就留給各位讀者自行去發現了,而其官網頁提供了及其詳細的說明。下面提供官方網址
這個庫里面有很多精華的內容,建議各位仔細的閱讀下官方的文檔。
RoboSpice
見之前寫的一篇博文
總結
網絡請求庫多種多樣,最終其本質思想是一致的,要學會融匯貫通,還是要fucking the source code。由於本篇文章已經過長,所以圖片的網絡加載准備另開一篇博客進行整理。