Android網(wǎng)絡通信的實現(xiàn)方式
Android網(wǎng)絡編程分為兩種:基于http協(xié)議的,和基于socket的。
基于Http協(xié)議:HttpClient、HttpURLConnection、AsyncHttpClient框架等
基于Socket:
(1)針對TCP/IP的Socket、ServerSocket
(2)針對UDP/IP的DatagramSocket、DatagramPackage
(3)Apache Mina框架
一、HttpURLConnection的實現(xiàn)方式
String response = null;
Url url = new URL(path);
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); // 新建連接實例
connection.setConnectTimeout(20000);// 設置連接超時時間,單位毫秒
//connection.setReadTimeout(20000);// 設置讀取數(shù)據(jù)超時時間,單位毫秒
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實現(xiàn)方式
HttpResponse mHttpResponse = null;
HttpEntity mHttpEntity = null;
//創(chuàng)建HttpPost對象
//HttpPost httppost = new HttpPost(path);
//設置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ā)送請求并獲得響應對象
mHttpResponse = httpClient.execute(httpGet);//如果是“POST”方式就傳httppost
if (mHttpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
// 獲得響應的消息實體
mHttpEntity = mHttpResponse.getEntity();
// 獲取一個輸入流
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();
}
三、實用AsyncHttpClient框架的實現(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)絡通信幾種方式的全部內(nèi)容,希望對大家的學習有所幫助。
- Android之網(wǎng)絡通信案例分析
- android 檢查網(wǎng)絡連接狀態(tài)實現(xiàn)步驟
- Android中判斷網(wǎng)絡連接是否可用及監(jiān)控網(wǎng)絡狀態(tài)
- Android Handler主線程和一般線程通信的應用分析
- Android 進程間通信實現(xiàn)原理分析
- android中圖片的三級緩存cache策略(內(nèi)存/文件/網(wǎng)絡)
- android 網(wǎng)絡編程之網(wǎng)絡通信幾種方式實例分享
- Android提高之MediaPlayer播放網(wǎng)絡視頻的實現(xiàn)方法
- Android提高之Android手機與BLE終端通信
- Android網(wǎng)絡編程之UDP通信模型實例
相關文章
Android實現(xiàn)上拉加載更多ListView(PulmListView)
這篇文章主要介紹了Android實現(xiàn)上拉加載更多ListView:PulmListView,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-09-09
Android游戲開發(fā)學習①彈跳小球實現(xiàn)方法
這篇文章主要介紹了Android游戲開發(fā)學習①彈跳小球實現(xiàn)方法,涉及Android通過物理引擎BallThread類模擬小球運動的相關技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-10-10
Android?Springboot?實現(xiàn)SSE通信案例詳解
SSE是一種用于實現(xiàn)服務器主動向客戶端推送數(shù)據(jù)的技術,它基于?HTTP?協(xié)議,利用了其長連接特性,在客戶端與服務器之間建立一條持久化連接,并通過這條連接實現(xiàn)服務器向客戶端的實時數(shù)據(jù)推送,這篇文章主要介紹了Android?Springboot?實現(xiàn)SSE通信案例,需要的朋友可以參考下2024-07-07
Android P實現(xiàn)靜默安裝的方法示例(官方Demo)
這篇文章主要介紹了Android P實現(xiàn)靜默安裝,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-02-02

