一,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是同步的請求,所以必須放在子線程中。使用示例如下:
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();
- 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
get請求的使用方法如上。如果需要傳遞參數,則直接把參數拼接到url後面,其他完全相同,如下:
String url = "https://www.baidu.com/?userName=zhangsan&password=123456";
- 1
注意點:
1,url與參數之間用?隔開。
2,鍵值對中鍵與值用=連接。
3,兩個鍵值對之間用&連接。
分析:
1, 使用connection.setRequestMethod(“GET”);設置請求方式。
2, 使用connection.connect();連接網絡。請求行,請求頭的設置必須放在網絡連接前。
3, connection.getInputStream()只是得到一個流對象,並不是數據,不過我們可以從流中讀出數據,從流中讀取數據的操作必須放在子線程。
4, connection.getInputStream()得到一個流對象,從這個流對象中只能讀取一次數據,第二次讀取時將會得到空數據。
三,post請求的使用方法
1,post的基本用法如下:
使用示例如下:
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();
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
注:post請求與get請求有很多相似,只是在連接之前多了一些設置,兩者可以對比學習使用。
2,使用post請求傳遞鍵值對參數
使用示例如下:
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
- 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
分析:
1,post方式傳遞參數的本質是:從連接中得到一個輸出流,通過輸出流把數據寫到服務器。
2,數據的拼接採用鍵值對格式,鍵與值之間用=連接。每個鍵值對之間用&連接。
3,使用post請求傳遞json格式參數
post請求也可以傳遞json格式的參數,使用示例如下:
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();
- 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
傳遞json格式的參數與傳遞鍵值對參數不同點有兩個:
1,傳遞json格式數據時需要在請求頭中設置參數類型是json格式。
2,body是json格式的字符串。
四,設置請求頭
Get請求與post請求都可以設置請求頭,設置請求頭的方式也是相同的。爲了節約篇幅,重複的代碼不再列出,核心代碼如下:
connection.setRequestMethod("POST");
connection.setRequestProperty("version", "1.2.3");//設置請求頭
connection.setRequestProperty("token", token);//設置請求頭
connection.connect();
- 1
- 2
- 3
- 4
- 5
注意:
1,請求頭必須在connection.connect();代碼前設置。
2,可以設置多個請求頭參數。
五,上傳文件
在post請求傳遞參數時知道,可以從連接中得到一個輸出流,輸出流可以像服務器寫數據。同理,可以使用這個輸出流將文件寫到服務器。代碼如下:
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
- 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
注:
1,上傳文件使用的是post請求方式。
2,使用的原理類似於post請求中上傳參數。
六,同時上傳參數和文件
在實際應用時,上傳文件的同時也常常需要上傳鍵值對參數。比如在微信中發朋友圈時,不僅有圖片,還有有文字。此時就需要同時上傳參數和文件。
在httpURLconnection中並沒有提供直接上傳參數和文件的API,需要我們自己去探索。我們知道在Web頁面上傳參數和文件很簡單,只需要在form標籤寫上contentype=”multipart/form-data”即可,剩餘工作便都交給瀏覽器去完成數據收集併發送Http請求。但是如果沒有頁面的話要怎麼上傳文件呢?
由於脫離了瀏覽器的環境,我們就要自己去完成數據的封裝併發送。首先我們來看web頁面上傳參數和文件是什麼樣子的?
我們寫一個web表單,上傳兩個鍵值對參數和一個文件。使用抓包工具抓取的數據結果如下:
經過分析可知,上傳到服務器的數據除了鍵值對數據和文件數據外,還有其他字符串,使用這些這些字符串來拼接一定的格式。
那麼我們只要模擬這個數據,並寫入到Http請求中便能實現同時傳遞參數和文件。
代碼如下:
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();
}
- 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
- 97
注:http最早出現時就是爲了瀏覽器與服務器之間的數據傳輸,所以有固定的協議,協議規範了一定的數據格式,所以在瀏覽器中傳遞數據時會自動按照一定的格式封裝。在android中不能自動封裝,所以這些操作需要手動操作。
七,下載文件
從服務器下載文件是比較簡單的操作,只要得到輸入流,就可以從流中讀出數據。使用示例如下:
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();
}
- 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
八,對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集合文件
* 文件下載(包含進度)
* 圖片下載(實現了圖片的壓縮)
請大家多多支持,多多提出寶貴意見,謝謝!