Android学习笔记【05】【网络编程之二】

一、

二、乱码问题解决

URLEncoder进行URL编码:

1 static String encode(String s, String charsetName);

三、以httpClient方式提交数据到服务器

Get方式:

 1 //URL地址
 2 String path="http://xxx.xx.x.xx:xxx/xxx/xx";
 3 //获取HttpClient实例(HttpClient是接口)
 4 DefaultHttpClient client=new DefaultHttpClient();
 5 //定义HttpGet请求
 6 HttpGet get=new HttpGet(path);
 7 //执行Get请求,获取响应
 8 HttpResponse response=client.execute(get);
 9 //获取状态码
10 int code = response.getStatusLine().getStatusCode();
11 //获取数据流
12 InputStream input=response.getEntity().getContent();

POST方式:

 1 //URL地址
 2 String path="http://xxx.xx.x.xx:xxx/xxx/xx";
 3 //获取HttpClient实例
 4 DefaultHttpClient client=new DefaultHttpClient();
 5 //定义HttpGet请求
 6 HttpPost post=new HttpPost(path);
 7 
 8 //准备数据
 9 List<NameValuePair> list=new ArrayList<NameValuePair>();
10 BasicNameValuePair nameValuePair=new BasicNameValuePair("name","zhangsan");
11 BasicNameValuePair passwordValuePair=new BasicNameValuePair("password","123");
12 
13 list.Add(nameValuePair);
14 list.Add(passwordValuePair);
15 
16 UrlEncodeFormEntity entity=new UrlEncodeFormEntity(list);
17 //执行Post请求,获取响应
18 HttpResponse response=client.execute(post);
19 //获取状态码
20 int code = response.getStatusLine().getStatusCode();
21 //获取数据流
22 InputStream input=response.getEntity().getContent();

四、开源项目方式提交数据到服务器

asyncHttpClient

五、JavaSE多线程下载

六、断点续传实现

Activity类getCacheDir()和getFilesDir()方法:
getCacheDir()方法用于获取/data/data/<package name>/cache目录
getFilesDir()方法用于获取/data/data/<package name>/files目录

posted on 2017-08-26 00:37  风云剑策  阅读(109)  评论(0编辑  收藏  举报

导航