
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,bytes.length,"UTF-8"); } @OverrIDe public voID onFailure(int i,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中网络通信几种方式的全部内容,希望对大家的学习有所帮助。
总结以上是内存溢出为你收集整理的Android网络通信的实现方式全部内容,希望文章能够帮你解决Android网络通信的实现方式所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)