亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android網(wǎng)絡(luò)通信的實(shí)現(xiàn)方式

 更新時(shí)間:2016年06月03日 09:00:13   作者:warmor  
這篇文章主要為大家詳細(xì)介紹了Android網(wǎng)絡(luò)通信的實(shí)現(xiàn)方式,四種實(shí)現(xiàn)網(wǎng)絡(luò)通信的方式供大家學(xué)習(xí),感興趣的小伙伴們可以參考一下

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í)有所幫助。

相關(guān)文章

最新評(píng)論