博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android网络
阅读量:7006 次
发布时间:2019-06-27

本文共 2826 字,大约阅读时间需要 9 分钟。

Http:

1从网络获取数据

a从网络获取图片,byte[]字节数组保存,并构建一个Bitmap

b.从网络获取为xml/Json,要解析,并封装成对象。

public byte[] getNetImage(String path) throws IOException{  URL url = new URL(path);  HttpURLConnection connection =(HttpURLConnection) url.openConnection();//基于HTTP协议连接对象  connection.setConnectTimeout(5000);  connection.setRequestMethod("GET");  if(connection.getResponseCode() == HttpURLConnection.HTTP_OK){      InputStream in =connection.getInputStream();      return Utils.readStream(in);  }  return null;} //将流读取到字节数组public static byte[] readStream(InputStream in) throws IOException {  ByteArrayOutputStream baos = new ByteArrayOutputStream();  byte[] buffer = new byte[1024];  int len = 0;  while ((len = in.read(buffer)) != -1) {      baos.write(buffer, 0, len);  }  return baos.toByteArray();}

2 GET/POST方式上传到服务器

a.通过HTTP发送参数到服务器

GET不适合传输大数据

  组拼url路径后的参数

POST:Content-Type Content-Length

  组拼将要发送的实体数据

 

HttpClient开源项目

  使用UrlEncodedFormEntity类构造实体数据

  内部做了很多封装,性能不如手写的GET/POST

  操作HTTPS,Cookie编码简单些 

// 发送POST请求public static boolean sendPostRequest(String path, Map
params, String encoding)throws IOException {  StringBuilder builder = new StringBuilder();  for (Map.Entry
entry : params.entrySet()) {    builder.append("?").append(entry.getKey()).append("=").append(entry.getValue()).append("&");  }  builder.deleteCharAt(builder.length() - 1);  URL url = new URL(path);  HttpURLConnection connection = (HttpURLConnection) url.openConnection();  connection.setConnectTimeout(5000);  connection.setRequestMethod("POST");  connection.addRequestProperty("Content-Type", "");  connection.addRequestProperty("Content-Length", String.valueOf(builder.length()));  connection.setDoOutput(true);  OutputStream os = connection.getOutputStream();  os.write(builder.toString().getBytes());// 只有取得服务器响应消息,才真正发送除数据,现在仅放到内存  if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {   return true;  }  return false;} // HttpClientpublic static boolean sendHttpClientRequest(String path, Map
params,String encoding) throws IOException {  List
valuePairs = new ArrayList
();  for (Map.Entry
entry : params.entrySet()) {   valuePairs.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));  }  UrlEncodedFormEntity entity = new UrlEncodedFormEntity(valuePairs, encoding);// 构造实体数据  HttpPost post = new HttpPost(path);// 构造POST请求  post.setEntity(entity);  DefaultHttpClient client = new DefaultHttpClient();// 构造一个浏览器  HttpResponse response = client.execute(post);  if (response.getStatusLine().getStatusCode() == 200) {   return true;  }  return false;}

b.通过Socket上传文件到服务器

先判断文件是否存在

HttpClient内部使用缓存,当上传文件超过1M,产生内存溢出Socket

实体数据长度= 文本类型长度+文件类型长度

 

转载于:https://www.cnblogs.com/liyuejiao/p/4251713.html

你可能感兴趣的文章
我的友情链接
查看>>
python中的asyncore
查看>>
zabbix专题:第二章 zabbix3.0安装详解
查看>>
sublime text 快捷收集
查看>>
VMware ESXI 5.5 系统安装(6.0)
查看>>
python解析Testlink导出的xml并写入excel
查看>>
linux环境变量设置
查看>>
Puppet3.0原理介绍与安装配置
查看>>
时间:2014年3月29日8:55:16 递归与迭代
查看>>
linux第一季运维001
查看>>
5,表达式中的陷阱
查看>>
安卓播放音效
查看>>
scrapy mysql
查看>>
一个比较好的手机归属地查询网站
查看>>
H3C SE 教程笔记——构建安全优化的广域网(上)
查看>>
Jhipster创建一个应用
查看>>
ANR 处理
查看>>
IOS版添加phonegap-银联支付插件教程
查看>>
vim编辑器介绍和使用
查看>>
Linux培训之批量删除文件命令
查看>>