Android網(wǎng)絡(luò)通信的實(shí)現(xiàn)方式
Android網(wǎng)絡(luò)編程分為兩種:基于http協(xié)議的,和基于socket的。
基于Http協(xié)議:HttpClient、HttpURLConnection、AsyncHttpClient框架等
基于Socket:
(1)針對(duì)TCP/IP的Socket、ServerSocket
(2)針對(duì)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對(duì)象 //HttpPost httppost = new HttpPost(path); //設(shè)置httpPost請(qǐng)求參數(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ā)送請(qǐng)求并獲得響應(yīng)對(duì)象 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)頁(yè)
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ò)通信幾種方式的全部?jī)?nèi)容,希望對(duì)大家的學(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中圖片的三級(jí)緩存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í)間選擇控件DatePicker和TimePicker實(shí)例
本篇文章主要介紹了Android之日期時(shí)間選擇控件DatePicker和TimePicker實(shí)例,具有一定的參考價(jià)值,有興趣的可以了解一下2017-05-05Android實(shí)現(xiàn)系統(tǒng)級(jí)懸浮按鈕
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)系統(tǒng)級(jí)懸浮按鈕的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03詳解Android studio 3+版本apk安裝失敗問題
這篇文章主要介紹了詳解Android studio 3+版本apk安裝失敗問題,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04android xml實(shí)現(xiàn)按鈕的圓角、陰影效果及按下變化效果的實(shí)現(xiàn)代碼
這篇文章主要介紹了android xml實(shí)現(xiàn)按鈕的圓角、陰影效果以及按下變化效果,通過五個(gè)xml文件實(shí)現(xiàn)按鈕的圓角陰影效果,代碼也很簡(jiǎn)單,需要的朋友可以參考下2021-05-05Android實(shí)現(xiàn)用代碼簡(jiǎn)單安裝和卸載APK的方法
這篇文章主要介紹了Android實(shí)現(xiàn)用代碼簡(jiǎn)單安裝和卸載APK的方法,涉及Android針對(duì)APK文件及package的相關(guān)操作技巧,需要的朋友可以參考下2016-08-08Android實(shí)現(xiàn)倒計(jì)時(shí)結(jié)束后跳轉(zhuǎn)頁(yè)面功能
最近在工作中遇到一個(gè)需求,需要在倒計(jì)時(shí)一段時(shí)間后進(jìn)行跳轉(zhuǎn)頁(yè)面,通過查找相關(guān)資料發(fā)現(xiàn)其中涉及的知識(shí)還不少,所以分享出來,下面這篇文章主要給大家介紹了關(guān)于Android實(shí)現(xiàn)倒計(jì)時(shí)結(jié)束后跳轉(zhuǎn)頁(yè)面功能的相關(guān)資料,需要的朋友可以參考下。2017-11-11Android編程之播放器MediaPlayer實(shí)現(xiàn)均衡器效果示例
這篇文章主要介紹了Android編程之播放器MediaPlayer實(shí)現(xiàn)均衡器效果,結(jié)合具體實(shí)例形式分析了Android調(diào)用MediaPlayer相關(guān)API構(gòu)造均衡器的具體步驟與相關(guān)功能實(shí)現(xiàn)方法,需要的朋友可以參考下2017-08-08Android仿微信和QQ多圖合并框架(類似群頭像)的實(shí)現(xiàn)方法
這篇文章主要給大家介紹了關(guān)于Android仿微信和QQ多圖合并框架的相關(guān)資料,其實(shí)就是我們平時(shí)所見的群聊頭像,文中通過示例代碼介紹的非常詳細(xì),對(duì)各位Android開發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-12-12