轉 關於HttpClient,HttpURLConnection,OkHttp的用法


轉自:https://www.cnblogs.com/zp-uestc/p/10371012.html

1 HttpClient入門實例

1.1發送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
/**
  * HttpClient發送get請求
  * @param url 請求地址
  * @return
  * @throws IOException
  */
public  static  String httpGet(String url)  throws  IOException {
     String result =  "" ;
 
     //創建HttpClient對象
     HttpClient httpClient = HttpClients.createDefault();
     HttpGet httpGet =  new  HttpGet(url);
     //發送get請求
     HttpResponse response = httpClient.execute(httpGet);
 
     /**請求發送成功,並得到響應**/
     if  (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
         /**讀取服務器返回過來的json字符串數據**/
         result = EntityUtils.toString(response.getEntity());
         System.out.println(result);
         return  result;
     }
     return  result;
}

  

1.2 發送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
/**
  * HttpClient發送post請求
  * @param url 請求地址
  * @param jsonParam 請求參數(json串或者xml串)
  * @param type 參數類型
  * @return
  * @throws IOException
  */
public  static  String httpPost(String url, String jsonParam, String type)  throws  IOException {
     String result =  "" ;
 
     //創建HttpClient對象
     HttpClient httpClient = HttpClients.createDefault();
     HttpPost httpPost =  new  HttpPost(url);
     if  (jsonParam !=  null ) {
         //解決中文亂碼問題
         StringEntity entity =  new  StringEntity(jsonParam,  "utf-8" );
         entity.setContentEncoding( "UTF-8" );
         if  (type.equals( "json" )) {
             entity.setContentType( "application/json" );
         }
         if  (type.equals( "xml" )) {
             entity.setContentType( "application/xml" );
         }
 
         httpPost.setEntity(entity);
     }
     //發送post請求
     HttpResponse response = httpClient.execute(httpPost);
     /**請求發送成功,並得到響應**/
     if  (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
         /**讀取服務器返回過來的json字符串數據**/
         result = EntityUtils.toString(response.getEntity());
         System.out.print(result);
         return  new  String(result.getBytes( "ISO-8859-1" ),  "UTF-8" );
     }
     return  result;
}

  

2 HttpURLConnection入門實例

一,HttpURLconnection的介紹
在Android開發中網絡請求是最常用的操作之一, Android SDK中對HTTP(超文本傳輸協議)也提供了很好的支持,這里包括兩種接口: 
1、標准Java接口(java.NET) —-HttpURLConnection,可以實現簡單的基於URL請求、響應功能; 
2、Apache接口(org.appache.http)—-HttpClient,使用起來更方面更強大。

但在android API23的SDK中Google將HttpClient移除了。Google建議使用httpURLconnection進行網絡訪問操作。

HttpURLconnection是基於http協議的,支持get,post,put,delete等各種請求方式,最常用的就是get和post,下面針對這兩種請求方式進行講解。

二,get請求的使用方法
HttpURLconnection是同步的請求,所以必須放在子線程中。使用示例如下

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
new  Thread( new  Runnable() {
     @Override
     public  void  run() {
         try  {
             String url =  "https://www.baidu.com/" ;
             URL url =  new  URL(url);
             //得到connection對象。
             HttpURLConnection connection = (HttpURLConnection) url.openConnection();
             //設置請求方式
             connection.setRequestMethod( "GET" );
             //連接
             connection.connect();
             //得到響應碼
             int  responseCode = connection.getResponseCode();
             if (responseCode == HttpURLConnection.HTTP_OK){
                 //得到響應流
                 InputStream inputStream = connection.getInputStream();
                 //將響應流轉換成字符串
                 String result = is2String(inputStream); //將流轉換為字符串。
                 Log.d( "kwwl" , "result=============" +result);
             }
 
         catch  (Exception e) {
             e.printStackTrace();
         }
     }
}).start();<br><br>

其中文章提到的is2String(inputStream)讀取響應結果,可參考如下寫法

public String is2String(InputStream inputStream){

// 得到響應流
InputStream is = connection.getInputStream();
// 將響應流轉換成字符串
byte[] buffer = new byte[1024];
ByteArrayOutputStream baos = new ByteArrayOutputStream();
for (int len = 0; (len = is.read(buffer)) > 0;) {
baos.write(buffer, 0, len);
}
String result = new String(baos.toByteArray(), "utf-8");
System.out.println(result);

return result ;
}

  get請求的使用方法如上。如果需要傳遞參數,則直接把參數拼接到url后面,其他完全相同,如下:

1
String url =  "https://www.baidu.com/?userName=zhangsan&password=123456" ;

  

注意點: 
1,url與參數之間用?隔開。 
2,鍵值對中鍵與值用=連接。 
3,兩個鍵值對之間用&連接。

分析: 
1, 使用connection.setRequestMethod(“GET”);設置請求方式。 
2, 使用connection.connect();連接網絡。請求行,請求頭的設置必須放在網絡連接前。 
3, connection.getInputStream()只是得到一個流對象,並不是數據,不過我們可以從流中讀出數據,從流中讀取數據的操作必須放在子線程。 
4, connection.getInputStream()得到一個流對象,從這個流對象中只能讀取一次數據,第二次讀取時將會得到空數據。

三,post請求的使用方法

1,post的基本用法如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
new  Thread( new  Runnable() {
     @Override
     public  void  run() {
         try  {
             URL url =  new  URL(getUrl);
             HttpURLConnection connection = (HttpURLConnection) url.openConnection();
             connection.setRequestMethod( "POST" ); //設置請求方式為POST
             connection.setDoOutput( true ); //允許寫出
             connection.setDoInput( true ); //允許讀入
             connection.setUseCaches( false ); //不使用緩存
             connection.connect(); //連接
             int  responseCode = connection.getResponseCode();
             if (responseCode == HttpURLConnection.HTTP_OK){
                 InputStream inputStream = connection.getInputStream();
                 String result = is2String(inputStream); //將流轉換為字符串。
                 Log.d( "kwwl" , "result=============" +result);
             }
 
         catch  (Exception e) {
             e.printStackTrace();
         }
     }
}).start();

  注:post請求與get請求有很多相似,只是在連接之前多了一些設置,兩者可以對比學習使用

2,使用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
new  Thread( new  Runnable() {
     @Override
     public  void  run() {
         try  {
             URL url =  new  URL(getUrl);
             HttpURLConnection connection = (HttpURLConnection) url.openConnection();
             connection.setRequestMethod( "POST" );
             connection.setDoOutput( true );
             connection.setDoInput( true );
             connection.setUseCaches( false );
             connection.connect();
 
             String body =  "userName=zhangsan&password=123456" ;
             BufferedWriter writer =  new  BufferedWriter( new  OutputStreamWriter(connection.getOutputStream(),  "UTF-8" ));
             writer.write(body);
             writer.close();
 
             int  responseCode = connection.getResponseCode();
             if (responseCode == HttpURLConnection.HTTP_OK){
                 InputStream inputStream = connection.getInputStream();
                 String result = is2String(inputStream); //將流轉換為字符串。
                 Log.d( "kwwl" , "result=============" +result);
             }
 
         catch  (Exception e) {
             e.printStackTrace();
         }
     }
}).start();

  分析: 
1,post方式傳遞參數的本質是:從連接中得到一個輸出流,通過輸出流把數據寫到服務器。 
2,數據的拼接采用鍵值對格式,鍵與值之間用=連接。每個鍵值對之間用&連接。

3,使用post請求傳遞json格式參數

post請求也可以傳遞json格式的參數,使用示例如下:

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
new  Thread( new  Runnable() {
     @Override
     public  void  run() {
         try  {
             URL url =  new  URL(getUrl);
             HttpURLConnection connection = (HttpURLConnection) url.openConnection();
             connection.setRequestMethod( "POST" );
             connection.setDoOutput( true );
             connection.setDoInput( true );
             connection.setUseCaches( false );
             connection.setRequestProperty( "Content-Type" "application/json;charset=utf-8" ); //設置參數類型是json格式
             connection.connect();
 
             String body =  "{userName:zhangsan,password:123456}" ;
             BufferedWriter writer =  new  BufferedWriter( new  OutputStreamWriter(connection.getOutputStream(),  "UTF-8" ));
             writer.write(body);
             writer.close();
 
             int  responseCode = connection.getResponseCode();
             if (responseCode == HttpURLConnection.HTTP_OK){
                 InputStream inputStream = connection.getInputStream();
                 String result = is2String(inputStream); //將流轉換為字符串。
                 Log.d( "kwwl" , "result=============" +result);
             }
 
         catch  (Exception e) {
             e.printStackTrace();
         }
     }
}).start();

  傳遞json格式的參數與傳遞鍵值對參數不同點有兩個: 
1,傳遞json格式數據時需要在請求頭中設置參數類型是json格式。 
2,body是json格式的字符串。

四,設置請求頭

Get請求與post請求都可以設置請求頭,設置請求頭的方式也是相同的。為了節約篇幅,重復的代碼不再列出,核心代碼如下:

1
2
3
4
connection.setRequestMethod( "POST" );
connection.setRequestProperty( "version" "1.2.3" ); //設置請求頭
connection.setRequestProperty( "token" , token); //設置請求頭
connection.connect();

  注意: 
1,請求頭必須在connection.connect();代碼前設置。 
2,可以設置多個請求頭參數。

五,上傳文件

在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
try  {
     URL url =  new  URL(getUrl);
     HttpURLConnection connection = (HttpURLConnection) url.openConnection();
     connection.setRequestMethod( "POST" );
     connection.setDoOutput( true );
     connection.setDoInput( true );
     connection.setUseCaches( false );
     connection.setRequestProperty( "Content-Type" "file/*" ); //設置數據類型
     connection.connect();
 
     OutputStream outputStream = connection.getOutputStream();
     FileInputStream fileInputStream =  new  FileInputStream( "file" ); //把文件封裝成一個流
     int  length = - 1 ;
     byte [] bytes =  new  byte [ 1024 ];
     while  ((length = fileInputStream.read(bytes)) != - 1 ){
         outputStream.write(bytes, 0 ,length); //寫的具體操作
     }
     fileInputStream.close();
     outputStream.close();
 
     int  responseCode = connection.getResponseCode();
     if (responseCode == HttpURLConnection.HTTP_OK){
         InputStream inputStream = connection.getInputStream();
         String result = is2String(inputStream); //將流轉換為字符串。
         Log.d( "kwwl" , "result=============" +result);
     }
 
catch  (Exception e) {
     e.printStackTrace();
}

  注: 
1,上傳文件使用的是post請求方式。 
2,使用的原理類似於post請求中上傳參數。

六,同時上傳參數和文件

在實際應用時,上傳文件的同時也常常需要上傳鍵值對參數。比如在微信中發朋友圈時,不僅有圖片,還有有文字。此時就需要同時上傳參數和文件。

在httpURLconnection中並沒有提供直接上傳參數和文件的API,需要我們自己去探索。我們知道在Web頁面上傳參數和文件很簡單,只需要在form標簽寫上contentype=”multipart/form-data”即可,剩余工作便都交給瀏覽器去完成數據收集並發送Http請求。但是如果沒有頁面的話要怎么上傳文件呢?

由於脫離了瀏覽器的環境,我們就要自己去完成數據的封裝並發送。首先我們來看web頁面上傳參數和文件是什么樣子的?

我們寫一個web表單,上傳兩個鍵值對參數和一個文件。使用抓包工具抓取的數據結果如下:

 


經過分析可知,上傳到服務器的數據除了鍵值對數據和文件數據外,還有其他字符串,使用這些這些字符串來拼接一定的格式。

那么我們只要模擬這個數據,並寫入到Http請求中便能實現同時傳遞參數和文件。

代碼如下:

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
85
86
87
88
89
90
91
92
93
94
95
96
try  {
 
     String BOUNDARY = java.util.UUID.randomUUID().toString();
     String TWO_HYPHENS =  "--" ;
     String LINE_END =  "\r\n" ;
 
     URL url =  new  URL(URLContant.CHAT_ROOM_SUBJECT_IMAGE);
     HttpURLConnection connection = (HttpURLConnection) url.openConnection();
     connection.setRequestMethod( "POST" );
     connection.setDoOutput( true );
     connection.setDoInput( true );
     connection.setUseCaches( false );
 
     //設置請求頭
     connection.setRequestProperty( "Connection" "Keep-Alive" );
     connection.setRequestProperty( "Charset" "UTF-8" );
     connection.setRequestProperty( "Content-Type" , "multipart/form-data; BOUNDARY="  + BOUNDARY);
     connection.setRequestProperty( "Authorization" , "Bearer " +UserInfoConfigure.authToken);
     connection.connect();
 
     DataOutputStream outputStream =  new  DataOutputStream(connection.getOutputStream());
     StringBuffer strBufparam =  new  StringBuffer();
     //封裝鍵值對數據一
     strBufparam.append(TWO_HYPHENS);
     strBufparam.append(BOUNDARY);
     strBufparam.append(LINE_END);
     strBufparam.append( "Content-Disposition: form-data; name=\""  "groupId"  "\"" );
     strBufparam.append(LINE_END);
     strBufparam.append( "Content-Type: "  "text/plain"  );
     strBufparam.append(LINE_END);
     strBufparam.append( "Content-Lenght: " +( "" +groupId).length());
     strBufparam.append(LINE_END);
     strBufparam.append(LINE_END);
     strBufparam.append( "" +groupId);
     strBufparam.append(LINE_END);
 
     //封裝鍵值對數據二
     strBufparam.append(TWO_HYPHENS);
     strBufparam.append(BOUNDARY);
     strBufparam.append(LINE_END);
     strBufparam.append( "Content-Disposition: form-data; name=\""  "title"  "\"" );
     strBufparam.append(LINE_END);
     strBufparam.append( "Content-Type: "  "text/plain"  );
     strBufparam.append(LINE_END);
     strBufparam.append( "Content-Lenght: " + "kwwl" .length());
     strBufparam.append(LINE_END);
     strBufparam.append(LINE_END);
     strBufparam.append( "kwwl" );
     strBufparam.append(LINE_END);
 
     //拼接完成后,一塊寫入
     outputStream.write(strBufparam.toString().getBytes());
 
 
     //拼接文件的參數
     StringBuffer strBufFile =  new  StringBuffer();
     strBufFile.append(LINE_END);
     strBufFile.append(TWO_HYPHENS);
     strBufFile.append(BOUNDARY);
     strBufFile.append(LINE_END);
     strBufFile.append( "Content-Disposition: form-data; name=\""  "image"  "\"; filename=\""  + file.getName() +  "\"" );
     strBufFile.append(LINE_END);
     strBufFile.append( "Content-Type: "  "image/*"  );
     strBufFile.append(LINE_END);
     strBufFile.append( "Content-Lenght: " +file.length());
     strBufFile.append(LINE_END);
     strBufFile.append(LINE_END);
 
     outputStream.write(strBufFile.toString().getBytes());
 
     //寫入文件
     FileInputStream fileInputStream =  new  FileInputStream(file);
     byte [] buffer =  new  byte [ 1024 * 2 ];
     int  length = - 1 ;
     while  ((length = fileInputStream.read(buffer)) != - 1 ){
         outputStream.write(buffer, 0 ,length);
     }
     outputStream.flush();
     fileInputStream.close();
 
     //寫入標記結束位
     byte [] endData = (LINE_END + TWO_HYPHENS + BOUNDARY + TWO_HYPHENS + LINE_END).getBytes(); //寫結束標記位
     outputStream.write(endData);
     outputStream.flush();
 
     //得到響應
     int  responseCode = connection.getResponseCode();
     if (responseCode == HttpURLConnection.HTTP_OK){
         InputStream inputStream = connection.getInputStream();
         String result = is2String(inputStream); //將流轉換為字符串。
         Log.d( "kwwl" , "result=============" +result);
     }
 
catch  (Exception e) {
     e.printStackTrace();
}

  注:http最早出現時就是為了瀏覽器與服務器之間的數據傳輸,所以有固定的協議,協議規范了一定的數據格式,所以在瀏覽器中傳遞數據時會自動按照一定的格式封裝。在android中不能自動封裝,所以這些操作需要手動操作。

七,下載文件

從服務器下載文件是比較簡單的操作,只要得到輸入流,就可以從流中讀出數據。使用示例如下:

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
try  {
      String urlPath =  "https://www.baidu.com/" ;
       URL url =  new  URL(urlPath);
       HttpURLConnection connection = (HttpURLConnection) url.openConnection();
       connection.setRequestMethod( "GET" );
       connection.connect();
       int  responseCode = connection.getResponseCode();
       if (responseCode == HttpURLConnection.HTTP_OK){
           InputStream inputStream = connection.getInputStream();
           File dir =  new  File( "fileDir" );
           if  (!dir.exists()){
               dir.mkdirs();
           }
           File file =  new  File(dir,  "fileName" ); //根據目錄和文件名得到file對象
           FileOutputStream fos =  new  FileOutputStream(file);
           byte [] buf =  new  byte [ 1024 * 8 ];
           int  len = - 1 ;
           while  ((len = inputStream.read(buf)) != - 1 ){
               fos.write(buf,  0 , len);
           }
           fos.flush();
       }
 
   catch  (Exception e) {
       e.printStackTrace();
   }

  

八,對httpURLconnection的封裝
經過上面的學習可知,httpURLconnection的使用非常麻煩,每次訪問網絡都需要寫大量的代碼,尤其在同時上傳參數和文件時更加繁瑣,一不小心就容易出現錯誤。而且httpURLconnection請求是同步的,使用時必須開啟子線程,修改UI時需要跳轉到UI線程。等等導致不得不對httpURLconnection封裝后再使用。Google也提供了網絡請求封裝類volley,熟悉volley的小伙伴都知道,volley在操作文件時性能並不好,而且沒有提供同時上傳參數和文件的方法。所以我們必須自己封裝一套httpURLconnection的工具類。

我個人研究httpURLconnection的用法后封裝了一套httpURLconnection的工具類,叫UrlHttpUtils。這套UrlHttpUtils最大的優點是簡單和便於使用,這是我項目中實際用的網絡請求工具類,完全可以說拿來即用。而且代碼簡單,可供學習使用。

UrlHttpUtils在github上的地址是:https://github.com/guozhengXia/UrlHttpUtils

封裝的功能有: 

* 一般的get請求 
* 一般的post請求 
* 上傳單個文件(包含進度) 
* 上傳list集合文件 
* 上傳map集合文件 
* 文件下載(包含進度) 
* 圖片下載(實現了圖片的壓縮)

 

3 .OkHttpClient入門實例

OkHttp3的基本用法

這篇文章說下OkHttp的基本用法,是最新的OkHttp3,如果你曾經在網上搜索OkHttp怎么使用發現有些類沒有了可能是因為人家說的是OkHttp2。

首先說下OkHttp3是Java和Android都能用,Android還有一個著名網絡庫叫Volley,那個只有Android能用。

3.1導入包

自己到入jar包,別漏了okio:

1
2
okhttp- 3.3 . 0 .jar
okio- 1.8 . 0 .jar

 maven方式:

1
2
3
4
5
<dependency>
   <groupId>com.squareup.okhttp3</groupId>
   <artifactId>okhttp</artifactId>
   <version> 3.3 . 0 </version>
</dependency>

  

gradle方式:

1
compile  'com.squareup.okhttp3:okhttp:3.3.0'

  

3.2 Get請求

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
String url =  "https://www.baidu.com/" ;
OkHttpClient okHttpClient =  new  OkHttpClient();
Request request =  new  Request.Builder()
     .url(url)
     .build();
Call call = okHttpClient.newCall(request);
try  {
     Response response = call.execute();
     System.out.println(response.body().string());
catch  (IOException e) {
     e.printStackTrace();
}
如果你需要在request的的header添加參數。例如Cookie,User-Agent什么的,就是
 
Request request =  new  Request.Builder()
     .url(url)
     .header( "鍵" "值" )
     .header( "鍵" "值" )
     ...
     .build();

response的body有很多種輸出方法,string()只是其中之一,注意是string()不是toString()。如果是下載文件就是response.body().bytes()。
另外可以根據response.code()獲取返回的狀態碼。

3.3 Post請求

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
String url =  "https://www.baidu.com/" ;
OkHttpClient okHttpClient =  new  OkHttpClient();
 
RequestBody body =  new  FormBody.Builder()
     .add( "鍵" "值" )
     .add( "鍵" "值" )
     ...
     .build();
 
Request request =  new  Request.Builder()
     .url(url)
     .post(body)
     .build();
 
Call call = okHttpClient.newCall(request);
try  {
     Response response = call.execute();
     System.out.println(response.body().string());
catch  (IOException e) {
     e.printStackTrace();
}

post請求創建request和get是一樣的,只是post請求需要提交一個表單,就是RequestBody。表單的格式有好多種,普通的表單是:

1
2
3
4
5
RequestBody body =  new  FormBody.Builder()
     .add( "鍵" "值" )
     .add( "鍵" "值" )
     ...
     .build();<em id= "__mceDel"  style= " font-family: "PingFang SC", "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 14px" > </em>

RequestBody的數據格式都要指定Content-Type,常見的有三種:

  • 1 application/x-www-form-urlencoded 數據是個普通表單
    2 multipart/form-data 數據里有文件
    3 application/json 數據是個json

但是好像以上的普通表單並沒有指定Content-Type,這是因為FormBody繼承了RequestBody,它已經指定了數據類型為application/x-www-form-urlencoded。

private static final MediaType CONTENT_TYPE = MediaType.parse("application/x-www-form-urlencoded");

再看看數據為其它類型的RequestBody的創建方式。

如果表單是個json:

MediaType JSON = MediaType.parse("application/json; charset=utf-8");
RequestBody body = RequestBody.create(JSON, "你的json");

如果數據包含文件:

RequestBody requestBody = new MultipartBody.Builder()
    .setType(MultipartBody.FORM)
    .addFormDataPart("file", file.getName(), RequestBody.create(MediaType.parse("image/png"), file))
    .build();

上面的MultipartBody也是繼承了RequestBody,看下源碼可知它適用於這五種Content-Type:

public static final MediaType MIXED = MediaType.parse("multipart/mixed");
public static final MediaType ALTERNATIVE = MediaType.parse("multipart/alternative");
public static final MediaType DIGEST = MediaType.parse("multipart/digest");
public static final MediaType PARALLEL = MediaType.parse("multipart/parallel");
public static final MediaType FORM = MediaType.parse("multipart/form-data");

另外如果你上傳一個文件不是一張圖片,但是MediaType.parse("image/png")里的"image/png"不知道該填什么,可以參考下這個頁面

3.4 同步與異步

從上文已經能知道call.execute()就是在執行http請求了,但是這是個同步操作,是在主線程運行的。如果你在android的UI線程直接執行這句話就出異常了。
OkHttp也幫我們實現了異步,寫法是:

復制代碼
String url = "https://www.baidu.com/";
OkHttpClient okHttpClient = new OkHttpClient();
Request request = new Request.Builder()
        .url(url)
        .build();
Call call = okHttpClient.newCall(request);
call.enqueue(new Callback() {
    @Override
    public void onFailure(Call call, IOException e) {
        e.printStackTrace();
    }

    @Override
    public void onResponse(Call call, Response response) throws IOException {
        System.out.println("我是異步線程,線程Id為:" + Thread.currentThread().getId());
    }
});
for (int i = 0; i < 10; i++) {
    System.out.println("我是主線程,線程Id為:" + Thread.currentThread().getId());
    try {
        Thread.currentThread().sleep(100);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}
復制代碼

 

執行結果是:

復制代碼
我是主線程,線程Id為:1
我是主線程,線程Id為:1
我是主線程,線程Id為:1
我是異步線程,線程Id為:11
我是主線程,線程Id為:1
我是主線程,線程Id為:1
我是主線程,線程Id為:1
我是主線程,線程Id為:1
我是主線程,線程Id為:1
我是主線程,線程Id為:1
我是主線程,線程Id為:1
復制代碼

顯然onFailure()和onResponse()分別是在請求失敗和成功時會調用的方法。這里有個要注意的地方,onFailure()和onResponse()是在異步線程里執行的,所以如果你在Android把更新UI的操作寫在這兩個方法里面是會報錯的,這個時候可以用runOnUiThread這個方法。

自動管理Cookie

Request經常都要攜帶Cookie,上面說過request創建時可以通過header設置參數,Cookie也是參數之一。就像下面這樣:

Request request = new Request.Builder()
    .url(url)
    .header("Cookie", "xxx")
    .build();

然后可以從返回的response里得到新的Cookie,你可能得想辦法把Cookie保存起來。
但是OkHttp可以不用我們管理Cookie,自動攜帶,保存和更新Cookie。
方法是在創建OkHttpClient設置管理Cookie的CookieJar:

復制代碼
private final HashMap<String, List<Cookie>> cookieStore = new HashMap<>();
OkHttpClient okHttpClient = new OkHttpClient.Builder()
    .cookieJar(new CookieJar() {
        @Override
        public void saveFromResponse(HttpUrl httpUrl, List<Cookie> list) {
            cookieStore.put(httpUrl.host(), list);
        }

        @Override
        public List<Cookie> loadForRequest(HttpUrl httpUrl) {
            List<Cookie> cookies = cookieStore.get(httpUrl.host());
            return cookies != null ? cookies : new ArrayList<Cookie>();
        }
    })
    .build();
復制代碼

這樣以后發送Request都不用管Cookie這個參數也不用去response獲取新Cookie什么的了。還能通過cookieStore獲取當前保存的Cookie。
最后,new OkHttpClient()只是一種快速創建OkHttpClient的方式,更標准的是使用OkHttpClient.Builder()。后者可以設置一堆參數,例如超時時間什么的。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM