[安卓基础]学习第五天

来源:互联网 发布:vc6.0连接mysql数据库 编辑:程序博客网 时间:2024/06/05 18:45

一、使用httpurlconnection方式把数据提交到服务器

基于http协议get方式:组拼url地址把数据组拼到url上,大小限制1kb,4kbpost请求:安全,无大小限制

1-1. post和get区别

1. 路径不同2. 请求方式post,通过请求体的方式把数据写给服务器(以流的形式)3. post多了Content-Length,Content-Type还有请求体

1-2 post请求

1. 修改路径2. GET方式改POST3. 新增两个请求头信息    - conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");    - conn.setRequestProperty("Content-Length",data.length() + "");4. 把我们的数据以流的方式进行提交    - conn.setDoOutput(true); // 设置一个标记,允许输出    - conn.getOutputSream(data.getBytes());

二、乱码问题的解决

iso-8859-1
服务器在传输的过程中以二进制的形式传输数据

【小细节】如果提交中文,需要进行URLEncode编码,

  • URLEncode.encode(name);

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

HttpClient是一个接口,我们直接使用它的子类即可

Interface for an HTTP client. HTTP clients encapsulate a smorgasbord of objects required to execute HTTP requests while handling cookies, authentication, connection management, and other features. Thread safety of HTTP clients depends on the implementation and configuration of the specific client.

3-1. get方式请求数据

// 部分代码// 获取HttpClient实例DefaultHttpClient client = DefaultHttpClient();// 准备get请求,定义一个httpget实现HttpGet get = new HttpGet(path);// 执行一个get请求HttpResponse respense = client.execute(get);// 获取服务器返回状态int code = response.getStatusLine().getStatusCode();if(code == 200){    // 以流的形式返回数据    InputStream in = response.getEntity().getContent();    //.......实现部分,代码为未写出}

3-2. post方式请求数据

// 获取HttpClient实例DefaultHttpClient client = DefaultHttpClient();// 准备post请求,定义一个httpget实现HttpPost post = new HttpPost(path);// 准备parametersList<NameValuePair> lists = new ArrayList<NameValuePair>();// 准备NameValuePair,实际上就是我们要提交的用户名和密码,key是服务器keyBasicNameValuePair nameValuePair = new BasicNameValuePair("username",name);BasicNameValuePair pwdValuePair = new BasicNameValuePair("password",pwd);// 把nameValuePair和pwdValuePair加入到集合中lists.add(nameValuePair);lists.add(pwdValuePair);// 准备entityUrlEncodedFormEntity entity = new UrlEncodedFormEntity(lists);// 准备post方式提交的正文,以实体形式准备(键值对形式)post.setEntity(entity);HttpResponse respense = client.execute(post);// 获取服务器返回状态int code = response.getStatusLine().getStatusCode();if(code == 200){    // 以流的形式返回数据    InputStream in = response.getEntity().getContent();    //.......实现部分,代码为未写出}

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

asyncHttpClient

4-1. get方式提交

// 创建 AsyncHttpClientAsyncHttpClient client = new AsyncHttpClient();// 进行get请求client.get(path,new AsyncHttpResponseHandler(){    // 请求成功的回调方法    public void onSuccess(int statusCode, Header[] headers, byte[] responseBody){        Toast.makeText(getApplicationContext(), new String(responseBody),1).show();    }    // 请求失败的回调方法    public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error){        Toast.makeText(getApplicationContext(), new String(responseBody),1).show();    }});

4-2. post方式提交

// 创建 AsyncHttpClientAsyncHttpClient client = new AsyncHttpClient();// 准备请求体的内容 ResquestParams params = new ResquestParams()params.put("username",name);params.put("password",pwd);client.post(path,params,new AsyncHttpResponseHandler(){        // 请求成功的回调方法    public void onSuccess(int statusCode, Header[] headers, byte[] responseBody){    }    // 请求失败的回调方法    public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error){    }};

4-3. 总结

  1. httpurlconnection
  2. httpclient(了解,没人用)
  3. 开源项目(asyncHttpClient)

五、javase多线程下载

 为什么多线程能够提高速度?

注意几点:[1] 不是线程开得越多下载越快 [2]受服务器带宽的影响

image

5-1.线程下载分配公式

- 前面的线程(n)   n*blockSize-(n+1)*blockSize-1- 最后一个线程(m) m*blockSize-length-1

5-2.添加range头信息

作用,告诉服务器,每个文件下载的位置conn.setResquestProperty("Range","bytes=" + startIndex + "-" + endIndex);

5-3.状态码改为206,请求部分资源成功

六、断点续传实现

6-1.解释

就是把当前线程下载的位置给存起来,下次下载的时候就是再上次下载断点处继续下载

七、断点续传逻辑移植到安卓上

八、开源项目实现多线程下载

0 0
原创粉丝点击