Android网络通信的实现方式
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中网络通信几种方式的全部内容,希望对大家的学习有所帮助。
相关文章
Android之日期时间选择控件DatePicker和TimePicker实例
本篇文章主要介绍了Android之日期时间选择控件DatePicker和TimePicker实例,具有一定的参考价值,有兴趣的可以了解一下2017-05-05详解Android studio 3+版本apk安装失败问题
这篇文章主要介绍了详解Android studio 3+版本apk安装失败问题,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2020-04-04android xml实现按钮的圆角、阴影效果及按下变化效果的实现代码
这篇文章主要介绍了android xml实现按钮的圆角、阴影效果以及按下变化效果,通过五个xml文件实现按钮的圆角阴影效果,代码也很简单,需要的朋友可以参考下2021-05-05Android编程之播放器MediaPlayer实现均衡器效果示例
这篇文章主要介绍了Android编程之播放器MediaPlayer实现均衡器效果,结合具体实例形式分析了Android调用MediaPlayer相关API构造均衡器的具体步骤与相关功能实现方法,需要的朋友可以参考下2017-08-08Android仿微信和QQ多图合并框架(类似群头像)的实现方法
这篇文章主要给大家介绍了关于Android仿微信和QQ多图合并框架的相关资料,其实就是我们平时所见的群聊头像,文中通过示例代码介绍的非常详细,对各位Android开发者们具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。2017-12-12
最新评论