欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android网络通信的实现方式

程序员文章站 2024-03-03 22:40:16
android网络编程分为两种:基于http协议的,和基于socket的。 基于http协议:httpclient、httpurlconnection、asynchttp...

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中网络通信几种方式的全部内容,希望对大家的学习有所帮助。