一、使用httpurlconnection方式把数据提交到服务器
基于http协议
get方式:组拼url地址把数据组拼到url上,大小限制1kb,4kb
post请求:安全,无大小限制
1-1. post和get区别
1. 路径不同
2. 请求方式post,通过请求体的方式把数据写给服务器(以流的形式)
3. post多了Content-Length,Content-Type还有请求体
1-2 post请求
1. 修改路径
2. GET方式改POST
3. 新增两个请求头信息
- 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编码,
三、以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方式请求数据
DefaultHttpClient client = DefaultHttpClient();
HttpGet get = new HttpGet(path);
HttpResponse respense = client.execute(get );
int code = response.getStatusLine().getStatusCode();
if (code == 200 ){
InputStream in = response.getEntity().getContent();
}
3-2. post方式请求数据
DefaultHttpClient client = DefaultHttpClient();
HttpPost post = new HttpPost(path);
List<NameValuePair> lists = new ArrayList<NameValuePair>();
BasicNameValuePair nameValuePair = new BasicNameValuePair("username" ,name);
BasicNameValuePair pwdValuePair = new BasicNameValuePair("password" ,pwd);
lists.add(nameValuePair);
lists.add(pwdValuePair);
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(lists);
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方式提交
AsyncHttpClient client = new AsyncHttpClient();
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方式提交
AsyncHttpClient 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. 总结
httpurlconnection httpclient(了解,没人用) 开源项目(asyncHttpClient)
五、javase多线程下载
为什么多线程能够提高速度?
注意几点:[1] 不是线程开得越多下载越快 [2]受服务器带宽的影响
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.解释
就是把当前线程下载的位置给存起来,下次下载的时候就是再上次下载断点处继续下载
七、断点续传逻辑移植到安卓上
八、开源项目实现多线程下载