Android網(wǎng)絡(luò)通信的實(shí)現(xiàn)方式
Android網(wǎng)絡(luò)編程分為兩種:基于http協(xié)議的,和基于socket的。
基于Http協(xié)議:HttpClient、HttpURLConnection、AsyncHttpClient框架等
基于Socket:
(1)針對TCP/IP的Socket、ServerSocket
(2)針對UDP/IP的DatagramSocket、DatagramPackage
(3)Apache Mina框架
一、HttpURLConnection的實(shí)現(xiàn)方式
String response = null;
Url url = new URL(path);
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); // 新建連接實(shí)例
connection.setConnectTimeout(20000);// 設(shè)置連接超時(shí)時(shí)間,單位毫秒
//connection.setReadTimeout(20000);// 設(shè)置讀取數(shù)據(jù)超時(shí)時(shí)間,單位毫秒
connection.setDoInput(true);// 是否打開輸入流 true|false
connection.setRequestMethod("POST");// 提交方法POST|GET
//connection.setUseCaches(false);// 是否緩存true|false
//connection.setRequestProperty("accept", "*/*");
//connection.setRequestProperty("Connection", "Keep-Alive");
//connection.setRequestProperty("Charset", "UTF-8");
//connection.setRequestProperty("Content-Length", String.valueOf(data.length));
//connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.connect();// 打開連接端口
int responseCode = conn.getResponseCode();
BufferedReader reader = null;
if (responseCode == 200) {
reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"));
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
response = buffer.toString();
} else {
response = "返回碼:"+responseCode;
}
reader.close();
conn.disconnect();
二、HttpClient實(shí)現(xiàn)方式
HttpResponse mHttpResponse = null;
HttpEntity mHttpEntity = null;
//創(chuàng)建HttpPost對象
//HttpPost httppost = new HttpPost(path);
//設(shè)置httpPost請求參數(shù)
//httppost.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
HttpGet httpGet = new HttpGet(path);
HttpClient httpClient = new DefaultHttpClient();
InputStream inputStream = null;
BufferedReader bufReader = null;
String result = "";
// 發(fā)送請求并獲得響應(yīng)對象
mHttpResponse = httpClient.execute(httpGet);//如果是“POST”方式就傳httppost
if (mHttpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
// 獲得響應(yīng)的消息實(shí)體
mHttpEntity = mHttpResponse.getEntity();
// 獲取一個(gè)輸入流
inputStream = mHttpEntity.getContent();
bufReader = new BufferedReader(new InputStreamReader(inputStream));
String line = "";
while (null != (line = bufReader.readLine())) {
result += line;
}
//result = EntityUtils.toString(mHttpResponse.getEntity());
}
if (inputStream != null) {
inputStream.close();
}
bufReader.close();
if (httpClient != null) {
httpClient.getConnectionManager().shutdown();
}
三、實(shí)用AsyncHttpClient框架的實(shí)現(xiàn)方式
AsyncHttpClient client = new AsyncHttpClient();
client.get(url, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int i, Header[] headers, byte[] bytes) {
String response = new String(bytes, 0, bytes.length, "UTF-8");
}
@Override
public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
}
});
四、使用WebView視圖組件顯示網(wǎng)頁
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
myWebView.loadUrl("http://"+networkAddress);
以上就是Android中網(wǎng)絡(luò)通信幾種方式的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助。
- Android之網(wǎng)絡(luò)通信案例分析
- android 檢查網(wǎng)絡(luò)連接狀態(tài)實(shí)現(xiàn)步驟
- Android中判斷網(wǎng)絡(luò)連接是否可用及監(jiān)控網(wǎng)絡(luò)狀態(tài)
- Android Handler主線程和一般線程通信的應(yīng)用分析
- Android 進(jìn)程間通信實(shí)現(xiàn)原理分析
- android中圖片的三級緩存cache策略(內(nèi)存/文件/網(wǎng)絡(luò))
- android 網(wǎng)絡(luò)編程之網(wǎng)絡(luò)通信幾種方式實(shí)例分享
- Android提高之MediaPlayer播放網(wǎng)絡(luò)視頻的實(shí)現(xiàn)方法
- Android提高之Android手機(jī)與BLE終端通信
- Android網(wǎng)絡(luò)編程之UDP通信模型實(shí)例
相關(guān)文章
Android 設(shè)置Edittext獲取焦點(diǎn)并彈出軟鍵盤
本文主要介紹了Android設(shè)置Edittext獲取焦點(diǎn)并彈出軟鍵盤的實(shí)現(xiàn)代碼。具有很好的參考價(jià)值。下面跟著小編一起來看下吧2017-04-04
Android實(shí)現(xiàn)上拉加載更多ListView(PulmListView)
這篇文章主要介紹了Android實(shí)現(xiàn)上拉加載更多ListView:PulmListView,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09
實(shí)例詳解Android Webview攔截ajax請求
本篇內(nèi)容主要給大家講解了Android Webview攔截ajax請求的詳細(xì)講解,需要的朋友一起來學(xué)習(xí)一下。2017-11-11
詳解關(guān)于MIUI 9沉浸式狀態(tài)欄的最新適配
由于各系統(tǒng)版本的限制,沉浸式狀態(tài)欄對系統(tǒng)有要求,本篇文章主要介紹了詳解關(guān)于MIUI 9沉浸式狀態(tài)欄的最新適配,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2018-05-05
Android游戲開發(fā)學(xué)習(xí)①彈跳小球?qū)崿F(xiàn)方法
這篇文章主要介紹了Android游戲開發(fā)學(xué)習(xí)①彈跳小球?qū)崿F(xiàn)方法,涉及Android通過物理引擎BallThread類模擬小球運(yùn)動(dòng)的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10
Android模仿To圈兒個(gè)人資料界面層疊淡入淡出顯示效果
這篇文章主要介紹了Android模仿To圈兒個(gè)人資料界面層疊淡入淡出顯示效果的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-07-07
Android?Springboot?實(shí)現(xiàn)SSE通信案例詳解
SSE是一種用于實(shí)現(xiàn)服務(wù)器主動(dòng)向客戶端推送數(shù)據(jù)的技術(shù),它基于?HTTP?協(xié)議,利用了其長連接特性,在客戶端與服務(wù)器之間建立一條持久化連接,并通過這條連接實(shí)現(xiàn)服務(wù)器向客戶端的實(shí)時(shí)數(shù)據(jù)推送,這篇文章主要介紹了Android?Springboot?實(shí)現(xiàn)SSE通信案例,需要的朋友可以參考下2024-07-07
Android P實(shí)現(xiàn)靜默安裝的方法示例(官方Demo)
這篇文章主要介紹了Android P實(shí)現(xiàn)靜默安裝,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02

