国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android網絡通信的實現方式

2019-12-12 06:16:07
字體:
來源:轉載
供稿:網友

Android網絡編程分為兩種:基于http協議的,和基于socket的。
基于Http協議:HttpClient、HttpURLConnection、AsyncHttpClient框架等
基于Socket
(1)針對TCP/IP的Socket、ServerSocket
(2)針對UDP/IP的DatagramSocket、DatagramPackage
(3)Apache Mina框架
一、HttpURLConnection的實現方式

String response = null; Url url = new URL(path); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); // 新建連接實例 connection.setConnectTimeout(20000);// 設置連接超時時間,單位毫秒 //connection.setReadTimeout(20000);// 設置讀取數據超時時間,單位毫秒 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實現方式

HttpResponse mHttpResponse = null; HttpEntity mHttpEntity = null; //創建HttpPost對象 //HttpPost httppost = new HttpPost(path); //設置httpPost請求參數 //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 = ""; // 發送請求并獲得響應對象 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框架的實現方式

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視圖組件顯示網頁

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中網絡通信幾種方式的全部內容,希望對大家的學習有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 台南县| 遂昌县| 千阳县| 普兰县| 盘锦市| 米林县| 景宁| 改则县| 平塘县| 息烽县| 茶陵县| 谷城县| 郁南县| 衡阳县| 桦南县| 三穗县| 蛟河市| 青田县| 大足县| 沽源县| 夹江县| 馆陶县| 北碚区| 朔州市| 江山市| 谷城县| 溧阳市| 磐石市| 五原县| 延边| 巫山县| 冷水江市| 津市市| 南漳县| 耿马| 都昌县| 新建县| 那坡县| 即墨市| 祁阳县| 洪泽县|