这里给大家介绍一下网络请求的几种方式:HttpURLConnection中的get、post,方法与HttpClient中的get、post方法
//注意添加用户权限,运行都要在子线程中运行
下面代码:
HttpURLConnection中的get方法
//path网络请求的地址
//自定义内容的编码URLEncoder.encode(city,"utf8")
String path="http://op.juhe.cn/onebox/weather/query"+"?cityname="+ URLEncoder.encode(city,"utf8")+"&key="+"47513a39d68c7316cb37bc137979baaf";
// 获取网络路径
URL url = new URL(path);
//打开连接
HttpURLConnection connection=(HttpURLConnection) url.openConnection();
//对链接进行设置
//设置连接超时时间
connection.setConnectTimeout(5000);
//设置请求方式 默认是get请求
// openConnection.setRequestMethod("GET");
//设置网络读取超时时间
connection.setReadTimeout(5000);
//获取响应码
int responseCode = connection.getResponseCode();
if (responseCode==200) {
//请求成功 获取网络资源
InputStream inputStream = connection.getInputStream();
//parseSteam调用下面的方法,获得到网络内容
String parseSteam = StreamUtils.parseSteam(inputStream);
}
}
//缓存请求到的内容
public String parseSteam(InputStream inputStream){
// 定义一个字节数组输出流
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
// 定义一个字节数组
byte[] b=new byte[1024];
// 定义初始长度
int len=0;
try {
while((len=inputStream.read(b))!=-1){
// 将读的内容,写到字节数组输出流中
byteArrayOutputStream.write(b, 0, len);
}
// 将字节输出流转成字符串
return byteArrayOutputStream.toString("utf-8");
// utf-8 大小写都可以,gbk 必须大写
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
HttpURLConnection中的post方法
以http://op.juhe.cn/onebox/weather/query?cityname=%E6%B8%A9%E5%B7%9E&key=47513a39d68c7316cb37bc13799baaf 为例
URL url = new URL(path);
//打开一个链接
HttpURLConnection openConnection=(HttpURLConnection) url.openConnection();
//设置本次请求的实体内容
String entity="key=8e6d32fe4b1f06ea793efda9fdbb5095&qq="+qq;
//告诉服务器客户端会发送实体内容
openConnection.setDoOutput(true);
//将实体内容打给服务器
OutputStream outputStream = openConnection.getOutputStream();
// 将实体内容发送给服务器
outputStream.write(entity.getBytes());
int responseCode = openConnection.getResponseCode();
if (responseCode==200) {
InputStream inputStream = openConnection.getInputStream();
String string = Utilebyte.getString(inputStream);
handler.obtainMessage(SU, string).sendToTarget();
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
HttpClient的get方法
//创建请求的客户端对象
HttpClient httpClient = new DefaultHttpClient();
//定义get请求对象,path是网络请求的地址
HttpGet httpGet = new HttpGet(path);
//执行get请求--获取到响应的对象
HttpResponse execute = httpClient.execute(httpGet);
// 获取状态行对象
StatusLine statusLine = execute.getStatusLine();
// 获取状态码
int statusCode = statusLine.getStatusCode();
if (statusCode==200) {
// 获取实体对象
HttpEntity entity = execute.getEntity();
//获取实体内容
InputStream inputStream = entity.getContent();
String string = Utilebyte.getString(inputStream);
}
}
HttpClient的post
以http://japi.juhe.cn/qqevaluate/qq?key=8e6d32fe4b1f06ea793efda9fdbb5095&qq=295424589,为例
//创建一个客户端对象
HttpClient httpClient = new DefaultHttpClient();
// 设置请求方式的对象
//path=http://japi.juhe.cn/qqevaluate/qq
HttpPost httpPost = new HttpPost(path);
// 创建放参数的一个集合
List<BasicNameValuePair> parameters=new ArrayList<BasicNameValuePair>();
// 往参数集合中添加数据,拼接地址
//key=8e6d32fe4b1f06ea793efda9fdbb5095
parameters.add(new BasicNameValuePair("key", "8e6d32fe4b1f06ea793efda9fdbb5095"));
//qq对应的qq号
parameters.add(new BasicNameValuePair("qq", qq));
// 设置请求的实体对象
HttpEntity entity = new UrlEncodedFormEntity(parameters);
// 设置httpPost请求的实体内容
httpPost.setEntity(entity);
// 执行post请求
HttpResponse execute = httpClient.execute(httpPost);
// 获取状态行中的状态码
int statusCode = execute.getStatusLine().getStatusCode();
if (statusCode==200) {
InputStream inputStream =execute.getEntity().getContent();
String string = Utilebyte.getString(inputStream);
}
}
//缓存数据方法
public String getString(InputStream inputStream){
ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
byte[] b=new byte[1024];
int len=0;
try {
while((len=inputStream.read(b))!=-1){
arrayOutputStream.write(b, 0, len);
}
return arrayOutputStream.toString("utf-8");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}