一、HttpURLconnection的介绍
在Android开发中网络请求是最常用的操作之一, Android SDK中对HTTP(超文本传输协议)也提供了很好的支持,这里包括两种接口:
1、标准Java接口(java.NET) —-HttpURLConnection,可以实现简单的基于URL请求、响应功能;
2、Apache接口(org.appache.http)—-HttpClient,使用起来更方面更强大。
但在android API23的SDK中Google将HttpClient移除了。Google建议使用httpURLconnection进行网络访问操作。
HttpURLconnection是基于http协议的,支持get,post,put,delete等各种请求方式,最常用的就是get和post,下面针对这两种请求方式进行讲解。
二、Get请求的使用方法
HttpURLconnection是同步的请求,所以必须放在子线程中。使用示例如下:
new Thread(new Runnable() {
@Override
public void run() {
try {
String url = "https://www.baidu.com/";
URL url = new URL(url);
//得到connection对象。
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//设置请求方式
connection.setRequestMethod("GET");
//连接
connection.connect();
//得到响应码
int responseCode = connection.getResponseCode();
if(responseCode == HttpURLConnection.HTTP_OK){
//得到响应流
InputStream inputStream = connection.getInputStream();
//将响应流转换成字符串
String result = is2String(inputStream);//将流转换为字符串。
Log.d("kwwl","result============="+result);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
get请求的使用方法如上。如果需要传递参数,则直接把参数拼接到url后面,其他完全相同,如下:
String url = "https://www.baidu.com/?userName=zhangsan&password=123456";
注意点:
1,url与参数之间用?隔开。
2,键值对中键与值用=连接。
3,两个键值对之间用&连接。
分析:
1, 使用connection.setRequestMethod(“GET”);设置请求方式。
2, 使用connection.connect();连接网络。请求行,请求头的设置必须放在网络连接前。
3, connection.getInputStream()只是得到一个流对象,并不是数据,不过我们可以从流中读出数据,从流中读取数据的操作必须放在子线程。
4, connection.getInputStream()得到一个流对象,从这个流对象中只能读取一次数据,第二次读取时将会得到空数据。
三、Post请求的使用方法
1,post的基本用法如下:
使用示例如下:
new Thread(new Runnable() {
@Override
public void run() {
try {
URL url = new URL(getUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");//设置请求方式为POST
connection.setDoOutput(true);//允许写出
connection.setDoInput(true);//允许读入
connection.setUseCaches(false);//不使用缓存
connection.connect();//连接
int responseCode = connection.getResponseCode();
if(responseCode == HttpURLConnection.HTTP_OK){
InputStream inputStream = connection.getInputStream();
String result = is2String(inputStream);//将流转换为字符串。
Log.d("kwwl","result============="+result);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
注:post请求与get请求有很多相似,只是在连接之前多了一些设置,两者可以对比学习使用。
2,使用post请求传递键值对参数
new Thread(new Runnable() {
@Override
public void run() {
try {
URL url = new URL(getUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setUseCaches(false);
connection.connect();
String body = "userName=zhangsan&password=123456";
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream(), "UTF-8"));
writer.write(body);
writer.close();
int responseCode = connection.getResponseCode();
if(responseCode == HttpURLConnection.HTTP_OK){
InputStream inputStream = connection.getInputStream();
String result = is2String(inputStream);//将流转换为字符串。
Log.d("kwwl","result============="+result);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
分析:
1,post方式传递参数的本质是:从连接中得到一个输出流,通过输出流把数据写到服务器。
2,数据的拼接采用键值对格式,键与值之间用=连接。每个键值对之间用&连接。
3,使用post请求传递json格式参数
post请求也可以传递json格式的参数,使用示例如下:
new Thread(new Runnable() {
@Override
public void run() {
try {
URL url = new URL(getUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setUseCaches(false);
connection.setRequestProperty("Content-Type", "application/json;charset=utf-8");//设置参数类型是json格式
connection.connect();
String body = "{userName:zhangsan,password:123456}";
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream(), "UTF-8"));
writer.write(body);
writer.close();
int responseCode = connection.getResponseCode();
if(responseCode == HttpURLConnection.HTTP_OK){
InputStream inputStream = connection.getInputStream();
String result = is2String(inputStream);//将流转换为字符串。
Log.d("kwwl","result============="+result);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
传递json格式的参数与传递键值对参数不同点有两个:
1,传递json格式数据时需要在请求头中设置参数类型是json格式。
2,body是json格式的字符串。
四,设置请求头
Get请求与post请求都可以设置请求头,设置请求头的方式也是相同的。为了节约篇幅,重复的代码不再列出,核心代码如下:
connection.setRequestMethod("POST");
connection.setRequestProperty("version", "1.2.3");//设置请求头
connection.setRequestProperty("token", token);//设置请求头
connection.connect();
注意:
1,请求头必须在connection.connect();代码前设置。
2,可以设置多个请求头参数。