Android 用HttpURLConnection訪問網(wǎng)絡(luò)的方法
一、 HttpURLConnection以GET方式訪問網(wǎng)絡(luò):
HttpURLConnection connection = null;
try {
URL url = new URL("https://www.xxx.com/");
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");//設(shè)置訪問方式為“GET”
connection.setConnectTimeout(8000);//設(shè)置連接服務(wù)器超時(shí)時(shí)間為8秒
connection.setReadTimeout(8000);//設(shè)置讀取服務(wù)器數(shù)據(jù)超時(shí)時(shí)間為8秒
if (HttpURLConnection.HTTP_OK == connection.getResponseCode()) {
//從服務(wù)器獲取響應(yīng)并把響應(yīng)數(shù)據(jù)轉(zhuǎn)為字符串打印
InputStream in = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder response = new StringBuilder();
String line;
while (null != (line = reader.readLine())) {
response.append(line);
}
Log.d(TAG, response.toString());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null!= connection) {
connection.disconnect();
}
}
二、 HttpURLConnection以POST方式訪問網(wǎng)絡(luò):
HttpURLConnection connection = null;
try{
URL url = new URL("https://www.xxx.com/");
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setConnectTimeout(8000);
connection.setReadTimeout(8000);
connection.setDoOutput(true);// 使用 URL 連接進(jìn)行輸出
connection.setDoInput(true);// 使用 URL 連接進(jìn)行輸入
connection.setUseCaches(false);// 忽略緩存
// 建立輸出流,并寫入數(shù)據(jù)
OutputStream outputStream = connection.getOutputStream();
DataOutputStream dataOutputStream = new DataOutputStream(outputStream);
dataOutputStream.writeBytes("username=admin&password=888888");
dataOutputStream.close();
if (HttpURLConnection.HTTP_OK == connection.getResponseCode()) {
// 當(dāng)正確響應(yīng)時(shí)處理數(shù)據(jù)
StringBuffer response = new StringBuffer();
String line;
BufferedReader responseReader =
new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"));
// 處理響應(yīng)流,必須與服務(wù)器響應(yīng)流輸出的編碼一致
while (null != (line = responseReader.readLine())) {
response.append(line);
}
responseReader.close();
Log.d(TAG, response.toString());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null!= connection) {
connection.disconnect();
}
}
注意:
1. HTTP訪問是不允許在主線程進(jìn)行的,否則會(huì)報(bào)錯(cuò)。因此上面的操作應(yīng)該在新線程中進(jìn)行。
2. 一般要用HttpURLConnection.getResponseCode() == 200來判斷是否正常響應(yīng)。為true則正常響應(yīng)。
3. 在Android 2.2及以下版本,使用的是HttpClient,Android 2.3及以上版本,使用的是HttpURLConnection,而Android5.1之后廢棄了HttpClient的相關(guān)Api。因此HttpClient用法不再進(jìn)行研究。
4. 以POST方式提交數(shù)據(jù)時(shí),每條數(shù)據(jù)要以鍵值對(duì)的方式提交,各條數(shù)據(jù)之間以&隔開。比如上面的代碼中dataOutputStream.writeBytes(“username=admin&password=888888”);
5. 上面用到了StringBuilder和StringBuffer,沒有什么特別用意,只是順便用下。StringBuilder在單線程下比StringBuffer更高效,但不是線程安全的。
以上這篇Android 用HttpURLConnection訪問網(wǎng)絡(luò)的方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- Android HttpURLConnection下載網(wǎng)絡(luò)圖片設(shè)置系統(tǒng)壁紙
- Android基于HttpUrlConnection類的文件下載實(shí)例代碼
- Android網(wǎng)絡(luò)技術(shù)HttpURLConnection詳解
- Android程序開發(fā)通過HttpURLConnection上傳文件到服務(wù)器
- Android 中HttpURLConnection與HttpClient使用的簡(jiǎn)單實(shí)例
- Android HttpURLConnection.getResponseCode()錯(cuò)誤解決方法
- Android使用HttpURLConnection實(shí)現(xiàn)網(wǎng)絡(luò)訪問流程
相關(guān)文章
Android各國(guó)語言縮寫及簡(jiǎn)稱詳細(xì)介紹
android資源文件夾的寫法規(guī)則: 語言縮寫-國(guó)家地區(qū)縮寫,本文將詳細(xì)介紹Android 各國(guó)語言縮寫及簡(jiǎn)稱,需要的朋友可以參考下2012-12-12
Android開發(fā)之背景動(dòng)畫簡(jiǎn)單實(shí)現(xiàn)方法
這篇文章主要介紹了Android開發(fā)之背景動(dòng)畫簡(jiǎn)單實(shí)現(xiàn)方法,涉及Android背景動(dòng)畫簡(jiǎn)單設(shè)置與使用技巧,需要的朋友可以參考下2017-10-10
Android程序開發(fā)之ListView實(shí)現(xiàn)橫向滾動(dòng)(帶表頭與固定列)
這篇文章主要介紹了Android程序開發(fā)之ListView實(shí)現(xiàn)橫向滾動(dòng)(帶表頭與固定列)的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-07-07
Android開發(fā)使用ProgressBar實(shí)現(xiàn)進(jìn)度條功能示例
這篇文章主要介紹了Android開發(fā)使用ProgressBar實(shí)現(xiàn)進(jìn)度條功能,結(jié)合實(shí)例形式分析了Android進(jìn)度條ProgressBar的具體樣式、布局與功能實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-03-03
Android 限制顯示小數(shù)點(diǎn)后兩位的實(shí)現(xiàn)方法
下面小編就為大家分享一篇Android 限制顯示小數(shù)點(diǎn)后兩位的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-01-01
Android Studio導(dǎo)入項(xiàng)目不支持的兩種解決方式
這篇文章主要介紹了Android Studio導(dǎo)入項(xiàng)目不支持的兩種解決方式,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11
Android 加載大圖及多圖避免程序出現(xiàn)OOM(OutOfMemory)異常
這篇文章主要介紹了Android 加載大圖及多圖避免程序出現(xiàn)OOM(OutOfMemory)異常的相關(guān)資料,需要的朋友可以參考下2017-03-03
android 6.0 寫入SD卡的權(quán)限申請(qǐng)實(shí)例講解
今天小編就為大家分享一篇android 6.0 寫入SD卡的權(quán)限申請(qǐng)實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-08-08
Android自定義鍵盤的實(shí)現(xiàn)(數(shù)字鍵盤和字母鍵盤)
本篇文章主要介紹了Android自定義鍵盤的實(shí)現(xiàn)(數(shù)字鍵盤和字母鍵盤),具有一定的參考價(jià)值,有興趣的可以了解一下2017-08-08

