Android 用HttpURLConnection訪問(wèn)網(wǎng)絡(luò)的方法
一、 HttpURLConnection以GET方式訪問(wèn)網(wǎng)絡(luò):
HttpURLConnection connection = null;
try {
URL url = new URL("https://www.xxx.com/");
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");//設(shè)置訪問(wèn)方式為“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èn)網(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);// 忽略緩存
// 建立輸出流,并寫(xiě)入數(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訪問(wèn)是不允許在主線程進(jìn)行的,否則會(huì)報(bào)錯(cuò)。因此上面的操作應(yīng)該在新線程中進(jìn)行。
2. 一般要用HttpURLConnection.getResponseCode() == 200來(lái)判斷是否正常響應(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ù)之間以&隔開(kāi)。比如上面的代碼中dataOutputStream.writeBytes(“username=admin&password=888888”);
5. 上面用到了StringBuilder和StringBuffer,沒(méi)有什么特別用意,只是順便用下。StringBuilder在單線程下比StringBuffer更高效,但不是線程安全的。
以上這篇Android 用HttpURLConnection訪問(wèn)網(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程序開(kāi)發(fā)通過(guò)HttpURLConnection上傳文件到服務(wù)器
- Android 中HttpURLConnection與HttpClient使用的簡(jiǎn)單實(shí)例
- Android HttpURLConnection.getResponseCode()錯(cuò)誤解決方法
- Android使用HttpURLConnection實(shí)現(xiàn)網(wǎng)絡(luò)訪問(wèn)流程
相關(guān)文章
Android實(shí)現(xiàn)圖片點(diǎn)擊爆炸效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)圖片點(diǎn)擊爆炸效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-08-08
Android中使用TextView實(shí)現(xiàn)圖文混排的方法
向TextView或EditText中添加圖像比直接添加文本復(fù)雜一點(diǎn)點(diǎn),需要用到<img>標(biāo)簽。接下來(lái)通過(guò)本文給大家介紹Android中使用TextView實(shí)現(xiàn)圖文混排的方法,希望對(duì)大家有所幫助2016-02-02
Android編程中出現(xiàn)The connection to adb is down問(wèn)題的解決方法
這篇文章主要介紹了Android編程中出現(xiàn)The connection to adb is down問(wèn)題的解決方法,涉及Android進(jìn)程與服務(wù)的相關(guān)操作技巧,需要的朋友可以參考下2015-12-12
解決Android啟動(dòng)APP的一瞬間系統(tǒng)欄會(huì)變成藍(lán)色問(wèn)題
這篇文章主要介紹了解決Android啟動(dòng)APP的一瞬間系統(tǒng)欄會(huì)變成藍(lán)色問(wèn)題,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-06-06
Android框架Volley使用之Json請(qǐng)求實(shí)現(xiàn)
這篇文章主要介紹了Android框架Volley使用之Json請(qǐng)求實(shí)現(xiàn),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下2019-05-05
音量控制鍵控制的音頻流(setVolumeControlStream)描述
當(dāng)開(kāi)發(fā)多媒體應(yīng)用或者游戲應(yīng)用的時(shí)候,需要使用音量控制鍵來(lái)設(shè)置程序的音量大小,在Android系統(tǒng)中有多種音頻流,感興趣的朋友可以了解下2013-01-01

