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ù)器超時時間為8秒
connection.setReadTimeout(8000);//設(shè)置讀取服務(wù)器數(shù)據(jù)超時時間為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 連接進行輸出
connection.setDoInput(true);// 使用 URL 連接進行輸入
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ù)據(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訪問是不允許在主線程進行的,否則會報錯。因此上面的操作應(yīng)該在新線程中進行。
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用法不再進行研究。
4. 以POST方式提交數(shù)據(jù)時,每條數(shù)據(jù)要以鍵值對的方式提交,各條數(shù)據(jù)之間以&隔開。比如上面的代碼中dataOutputStream.writeBytes(“username=admin&password=888888”);
5. 上面用到了StringBuilder和StringBuffer,沒有什么特別用意,只是順便用下。StringBuilder在單線程下比StringBuffer更高效,但不是線程安全的。
以上這篇Android 用HttpURLConnection訪問網(wǎng)絡(luò)的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
- Android HttpURLConnection下載網(wǎng)絡(luò)圖片設(shè)置系統(tǒng)壁紙
- Android基于HttpUrlConnection類的文件下載實例代碼
- Android網(wǎng)絡(luò)技術(shù)HttpURLConnection詳解
- Android程序開發(fā)通過HttpURLConnection上傳文件到服務(wù)器
- Android 中HttpURLConnection與HttpClient使用的簡單實例
- Android HttpURLConnection.getResponseCode()錯誤解決方法
- Android使用HttpURLConnection實現(xiàn)網(wǎng)絡(luò)訪問流程
相關(guān)文章
Android中使用TextView實現(xiàn)圖文混排的方法
向TextView或EditText中添加圖像比直接添加文本復(fù)雜一點點,需要用到<img>標(biāo)簽。接下來通過本文給大家介紹Android中使用TextView實現(xiàn)圖文混排的方法,希望對大家有所幫助2016-02-02
Android編程中出現(xiàn)The connection to adb is down問題的解決方法
這篇文章主要介紹了Android編程中出現(xiàn)The connection to adb is down問題的解決方法,涉及Android進程與服務(wù)的相關(guān)操作技巧,需要的朋友可以參考下2015-12-12
解決Android啟動APP的一瞬間系統(tǒng)欄會變成藍色問題
這篇文章主要介紹了解決Android啟動APP的一瞬間系統(tǒng)欄會變成藍色問題,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2018-06-06
Android框架Volley使用之Json請求實現(xiàn)
這篇文章主要介紹了Android框架Volley使用之Json請求實現(xiàn),本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值 ,需要的朋友可以參考下2019-05-05
音量控制鍵控制的音頻流(setVolumeControlStream)描述
當(dāng)開發(fā)多媒體應(yīng)用或者游戲應(yīng)用的時候,需要使用音量控制鍵來設(shè)置程序的音量大小,在Android系統(tǒng)中有多種音頻流,感興趣的朋友可以了解下2013-01-01

