Http获取网络数据的步骤
添加权限
// 1.得到图片的url路径
URL url = new URL(path);
// 2.通过路径打开一个http的连接,并设置请求参数
HttpURLConnection conn = (HttpURLConnection) url.openConnection();// http
// 3.得到响应码,判断与服务器连接是否正常
int code = conn.getResponseCode(); // 200 OK 404 资源没找到 503服务器内部错误
if (code == 200) {
// 4.得到服务器返回的数据流
InputStream is = conn.getInputStream();
}
Http请求的小细节
//初始化时的设置
conn.setRequestMethod("GET");//设置请求参数为get, 默认的请求方式就是get,此处区分大小写
conn.setConnectTimeout(5000);//设置请求服务器的超时时间.
//服务器返回数据属性的获取
String type = conn.getContentType();//获取返回数据的类型
int length = conn.getContentLength();//获取返回数据的长度
//User-Agent
当前浏览器的类型,服务器可以根据浏览器的类型的不同,返回不同的页面
获取输入流的处理
- 将流转换为Bitmap
Bitmap bitmap = BitmapFactory.decodeStream(is);
iv.setImageBitmap(bitmap);
- 将流转化为字符串
public static String readStream(InputStream is) throws Exception{
//ByteArrayOutputStream类是在创建它的实例时,程序内部创建一个byte型数组的缓冲区,
//缓冲区会随着数据的不断写入而自动增长。可使用 toByteArray()和 toString()获取数据
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = -1;
while((len = is.read(buffer))!=-1){
baos.write(buffer, 0, len);
}
is.close();
String temp = baos.toString();
if(temp.contains("charset=utf-8")){
return temp;
}else if(temp.contains("gb2312")){
return baos.toString("gb2312");
}return null;
}
乱码相关的处理
- string.getBytes(String charsetName)和new String(byte[] bytes, String charsetName)
//将字符串按指定的编码转化为byte数组,默认采用本地码表
byte[] bytes = string.getBytes(String charsetName)
//将byte数组按指定的编码转化为字符串
new String(byte[] bytes, String charsetName)
**注意:出现乱码时不要修改文件,修改后无论怎么切换编码都是错的了**
- 提交数据中含有中文的注意事项
客户端
//提交的数据中含有中文时,将字符串qq按照编码UTF-8进行编码
URLEncoder.encode(qq, "UTF-8");
**服务端**
String qq =request.getParameter("qq");//tomcat采用的编码是iso-8859-1
System.out.println("qq:"+new String(qq.getBytes("iso-8859-1"),"utf-8"));
GET请求和POST请求的区别
优缺点
* GET请求
优点:使用非常方便,只需要在url后面组拼数据。
缺点:数据在url的后面组拼,不安全。有数据长度限制。
* POST请求
优点:安全,数据不是在url后面组拼而是通过流的方式写给服务器。数据长度不受限制
缺点:编写麻烦。
数据提交
* GET请求
1. 需要在url后面组拼提交的数据
* POST请求
1. 不需要组拼任何的数据
2. 必须指定请求的数据类型,是一个经过url编码的表单数据。Content-Type
3. 以流的方式把数据写给服务器,所以必须指定提交数据的长度。Content-Length
GET方式提交数据
new Thread(){
public void run() {
try {
//GET请求方式的特点:在url后面组拼数据
String path = "http://192.168.1.103:8080/web/LoginServlet?qq="+qq+"&pwd="+pwd);
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5000);
int code = conn.getResponseCode();
if(code == 200){
InputStream is = conn.getInputStream();
String result = StreamTools.readStream(is);
Message msg = Message.obtain();
msg.what = SUCCESS;
msg.obj = result;
handler.sendMessage(msg);
}else{
Message msg = Message.obtain();
msg.what = ERROR;
handler.sendMessage(msg);
}
} catch (Exception e) {
e.printStackTrace();
Message msg = Message.obtain();
msg.what = ERROR;
handler.sendMessage(msg);
}
};
}.start();
POST方式提交数据
new Thread(){
public void run() {
//路径不需要组拼
String path = "http://192.168.1.103:8080/web/LoginServlet";
try {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//1.设置请求方式为POST
conn.setRequestMethod("POST"); //注意单词必须大写.
conn.setConnectTimeout(5000);
//2.设置http请求数据的类型为表单类型
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
//3.设置给服务器写的数据的长度
//qq=10000&pwd=abcde
String data = "qq="+qq+"&pwd="+pwd;
conn.setRequestProperty("Content-Length", String.valueOf(data.length()));
//4.记得指定要给服务器写数据
conn.setDoOutput(true);
//5.开始向服务器写数据
conn.getOutputStream().write(data.getBytes());
int code = conn.getResponseCode();
if(code == 200){
InputStream is = conn.getInputStream();
String result = StreamTools.readStream(is);
Message msg = Message.obtain();
msg.what = SUCCESS;
msg.obj = result;
handler.sendMessage(msg);
}else{
Message msg = Message.obtain();
msg.what = ERROR;
handler.sendMessage(msg);
}
} catch (Exception e) {
e.printStackTrace();
Message msg = Message.obtain();
msg.what = ERROR;
handler.sendMessage(msg);
}
};
}.start();
HttpClient
轻量级的浏览器,将请求和响应都封装成了对象
- 打开浏览器
- 输入地址(数据)
- 敲回车
HttpClient的GET和POST请求
GET请求
new Thread(){
public void run() {
try {
String path = "http://192.168.1.103:8080/web/LoginServlet?qq="+URLEncoder.encode(qq, "utf-8")+"&pwd="+URLEncoder.encode(pwd, "utf-8");
//1.打开浏览器
HttpClient client = new DefaultHttpClient();
//2.输入地址或者数据
HttpGet httpGet = new HttpGet(path);
//3.敲回车
HttpResponse response = client.execute(httpGet);
//获取状态码
int code = response.getStatusLine().getStatusCode();
if(code == 200){
InputStream is = response.getEntity().getContent();
String result = StreamTools.readStream(is);
Message msg = Message.obtain();
msg.what = SUCCESS;
msg.obj = result;
handler.sendMessage(msg);
}else{
Message msg = Message.obtain();
msg.what = ERROR;
handler.sendMessage(msg);
}
} catch (Exception e) {
e.printStackTrace();
Message msg = Message.obtain();
msg.what = ERROR;
handler.sendMessage(msg);
}
};
}.start();
POST请求
new Thread(){
public void run() {
try {
String path = "http://192.168.1.103:8080/web/LoginServlet";
//1.打开浏览器
HttpClient client = new DefaultHttpClient();
//2.输入地址或者数据
HttpPost httpPost = new HttpPost(path);
List<NameValuePair> parameters = new ArrayList<NameValuePair>();
parameters.add(new BasicNameValuePair("qq", qq));
parameters.add(new BasicNameValuePair("pwd", pwd));
httpPost.setEntity(new UrlEncodedFormEntity(parameters, "utf-8"));
//3.敲回车
HttpResponse response = client.execute(httpPost);
//获取状态码
int code = response.getStatusLine().getStatusCode();
if(code == 200){
InputStream is = response.getEntity().getContent();
String result = StreamTools.readStream(is);
Message msg = Message.obtain();
msg.what = SUCCESS;
msg.obj = result;
handler.sendMessage(msg);
}else{
Message msg = Message.obtain();
msg.what = ERROR;
handler.sendMessage(msg);
}
} catch (Exception e) {
e.printStackTrace();
Message msg = Message.obtain();
msg.what = ERROR;
handler.sendMessage(msg);
}
};
}.start();
async-http开源框架的GET请求
String path = "http://192.168.1.103:8080/web/LoginServlet?qq="+URLEncoder.encode(qq)+"&pwd="+URLEncoder.encode(pwd);
AsyncHttpClient client = new AsyncHttpClient();
client.get(path, new AsyncHttpResponseHandler() {
//请求成功的回调
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
tv_status.setText(new String(responseBody));
}
//请求失败的回调
@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
tv_status.setText("http请求失败"+new String(responseBody));
}
});
async-http开源框架的POST请求
String path = "http://192.168.1.103:8080/web/LoginServlet";
AsyncHttpClient client = new AsyncHttpClient();
RequestParams params = new RequestParams();
params.put("qq", qq);
params.put("pwd", pwd);
client.post(path, params, new AsyncHttpResponseHandler(){
//请求成功的回调
@Override
public void onSuccess(int statusCode, Header[] headers,
byte[] responseBody) {
tv_status.setText("登陆结果:"+new String(responseBody));
}
//请求失败的回调
@Override
public void onFailure(int statusCode, Header[] headers,
byte[] responseBody, Throwable error) {
tv_status.setText("请求失败请检查网络");
}
});
async-http开源框架的文件上传
文件上传的原理
文件上传其实就是一个POST请求
Content-Type为multipart/form-data
具体代码
AsyncHttpClient client = new AsyncHttpClient();
RequestParams params = new RequestParams();
try {
params.put("file", file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
client.post("http://192.168.1.103:8080/web/UploadServlet", params, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
Toast.makeText(MainActivity.this, "上传成功", 0).show();
}
@Override
public void onFailure(int statusCode, Header[] headers,
byte[] responseBody, Throwable error) {
Toast.makeText(MainActivity.this, "上传失败", 0).show();
}
});
多线程下载
- 多线程下载的步骤
1,在客户端本地创建一个空白文件,文件的大小跟服务器的一模一样。RandomAccessFile
2,开启若干个线程去下载服务器的资源
3,当所有线程都下载完毕,多线程下载就结束了
public class MultiThread {
private static String path = "";
private static int threadCount = 3;
/**
* 多线程下载
*/
public static void main(String[] args) {
try {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
int code = conn.getResponseCode();
if (code == 200) {
int length = conn.getContentLength();
//在客户端设置等长度的文件
RandomAccessFile raf = new RandomAccessFile(getFileName(path), "rw");
raf.setLength(length);
raf.close();
int size = length/threadCount;
for (int threadId = 0; threadId < threadCount; threadId++) {
int startThread = threadId*size;
int endThread = (threadId+1)*size-1;
if (threadId == threadCount-1) {
endThread = length-1;
}
new Mythread(threadId, startThread, endThread).start();
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static class Mythread extends Thread{
private int threadId;
private int startThread;
private int endThread;
public Mythread(int threadId, int startThread, int endThread) {
super();
this.threadId = threadId;
this.startThread = startThread;
this.endThread = endThread;
}
@Override
public void run() {
try {
URL url = new URL(getFileName(path));
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//设置每个线程下载一部分
conn.setRequestProperty("Range", "bytes="+startThread+endThread);
InputStream is = conn.getInputStream();
byte []arr = new byte[1024];
RandomAccessFile raf = new RandomAccessFile(getFileName(path), "rw");
raf.seek(startThread);
int len;
while ((len = is.read(arr)) != -1) {
raf.write(arr, 0, len);
}
raf.close();
is.close();
super.run();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private static String getFileName(String path) {
int indexOf = path.indexOf("/");
String filename = path.substring(indexOf+1);
return filename;
}
多线程断点下载
断点续传的原理
记录每个线程的下载进度,下次再下载的时候从该位置开始下载多线程断点下载的小细节
1,每个下载任务都需要自己的记录进度的文件
2,都下载完后删除记录文件
3,保证同步使用synchronize代码块
4,使用FileOutputStream数据不一定每一次都写到存储设备里,有可能写到硬盘的缓存里,使用RandomAccessFile将模式设置为rwd,可以保证每次都将数据写到磁盘里
/**
* 多线程下载
*/
public static void main(String[] args) {
try {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
int code = conn.getResponseCode();
if (code == 200) {
int length = conn.getContentLength();
RandomAccessFile raf = new RandomAccessFile(getFileName(path), "rw");
raf.setLength(length);
raf.close();
runThreadCount = threadCount;
int size = length/threadCount;
for (int threadId = 0; threadId < threadCount; threadId++) {
int startThread = threadId*size;
int endThread = (threadId+1)*size-1;
if (threadId == threadCount-1) {
endThread = length-1;
}
new Mythread(threadId, startThread, endThread).start();
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static class Mythread extends Thread{
private int threadId;
private int startThread;
private int endThread;
private int correntposition;
public Mythread(int threadId, int startThread, int endThread) {
this.threadId = threadId;
this.startThread = startThread;
this.endThread = endThread;
correntposition = startThread;
}
@Override
public void run() {
try {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
RandomAccessFile raf = new RandomAccessFile(getFileName(path), "rw");
File file = new File(threadId+".position");
if (file.exists() && file.length()>0) {
BufferedReader br = new BufferedReader(new FileReader(file));
correntposition = Integer.parseInt(br.readLine());
conn.setRequestProperty("Range", "bytes="+correntposition+"-"+endThread);
raf.seek(correntposition);
br.close();
}else {
conn.setRequestProperty("Range", "bytes="+startThread+"-"+endThread);//为每个线程分配任务
raf.seek(startThread);//使线程下载的数据顺序排列
}
InputStream is = conn.getInputStream();
byte []arr = new byte[1024];
int len;
while ((len = is.read(arr)) != -1) {
raf.write(arr, 0, len);
correntposition+=len;
File file2 = new File(threadId+".position");
//FileOutputStream fos = new FileOutputStream(file2);
//使用FileOutputStream有可能会=产生缓存,不能直接写到硬盘中
//RandomAccessFile的rwd模式可以直接写到硬盘中
RandomAccessFile fos = new RandomAccessFile(file2,"rwd");
fos.write((correntposition+"").getBytes());
fos.close();
//模拟下载完成的界面表示
/*
int max = endIndex - startIndex;
int now = endIndex - curent;
if (threadId == 0) {
pb1.setMax(max);
pb1.setProgress(now);
}else if (threadId == 1) {
pb2.setMax(max);
pb2.setProgress(now);
}else if (threadId == 2) {
pb3.setMax(max);
pb3.setProgress(now);
}*/
}
raf.close();
is.close();
File f = new File(threadId+".position");
f.renameTo(new File(threadId+".position.finish"));
/**
* 当线程下载完成后,记录当前位置的文件中的信息会出现错误
* 所以当每个线程下载完成后就要删除记录当前线程位置的文件
* 加锁是保证同步执行
*/
synchronized (MultiThread.class) {
runThreadCount--;
if (runThreadCount <= 0) {
for (int i = 0; i < threadCount; i++) {
File file3 = new File(i+".position.finish");
file3.delete();
}
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private static String getFileName(String path) {
int indexOf = path.lastIndexOf("/");
String filename = path.substring(indexOf+1);
return filename;
}
使用xUtils完成多线程断点下载
HttpUtils http = new HttpUtils();
//第一个参数:服务器地址
//第二个参数:要下载到哪里
//是否断点续传
//下载的一些回调函数
http.download(path, "/mnt/sdcard/xxx.exe", true, new RequestCallBack<File>() {
//下载成功的回调
@Override
public void onSuccess(ResponseInfo<File> arg0) {
Toast.makeText(MainActivity.this, "下载成功", 0).show();
}
//进度更新的回调
@Override
public void onLoading(long total, long current, boolean isUploading) {
pb0.setMax((int) total);
pb0.setProgress((int) current);
super.onLoading(total, current, isUploading);
}
//下载失败的回调
@Override
public void onFailure(HttpException arg0, String arg1) {
Toast.makeText(MainActivity.this, "下载失败"+arg1, 0).show();
}
});