从网络获取数据的几种方法
2015-10-15 15:00
621 查看
从网络上请求数据的几种方法
一、HttpHttpClient:
GET()请求:
1、// 创建一个默认的HttpClient
HttpClient client= new DefaultHttpClient();
client.getParams().setParameter(
CoreConnectionPNames.CONNECTION_TIMEOUT, 2000);// 连接时间
client.getParams().setParameter(
CoreConnectionPNames.SO_TIMEOUT, 2000);// 数据传输时间
2、//创建一个HttpGet请求
HttpGet get= new HttpGet("接口");
注意:接口必须完整,例如http://zuowen.api.juhe.cn/zuowen/typeList?key=cbd1daab865bf46c4772a7bc0eb63cce&id=1
3、//客户端使用GET方式执行请教,获得服务器端的回应response
HttpResponse response =client.execute(get);
4、//判断请求是否成功
if (response.getStatusLine().getStatusCode() == 200) {
// 获得输入流
InputStreamstream = response.getEntity().getContent();
//GSON解析数据
。。。。。。
}
POST()请求:
1、// 创建一个默认的HttpClient
HttpClient client= new DefaultHttpClient();
2、//创建一个HttpGet请求
HttpPost post= new HttpPost ("接口");
接口实例:http://v.juhe.cn/cell/get?mnc=0&cell=28655&lac=17695
3、创建泛型为NameValuePair的集合
List<NameValuePair> list = new ArrayList<NameValuePair>();
list.add(newBasicNameValuePair("key",
"1e1c029cc62bcbfb65934ddcc91b4b7c"));
UrlEncodedFormEntity formEntity =
new UrlEncodedFormEntity(
list);
4、//得到UrlEncodedFormEntity对象
post.setEntity(formEntity);
5、//客户端使用POST方式执行请教,获得服务器端的回应response
HttpResponse response = client.execute(post);
4、//判断请求是否成功
if (response.getStatusLine().getStatusCode() == 200) {
// 获得输入流
InputStreamstream = response.getEntity().getContent();
//GSON解析数据
。。。。。。
}
二、HttpURLConnection
GET()请求:
1、 创建URL对象
URL url = new URL("http://zuowen.api.juhe.cn/zuowen/typeList?key=cbd1daab865bf46c4772a7bc0eb63cce&id=1");
注意:接口完整
2、
// 利用HttpClientConnection对象从网络中获取网页数据
HttpURLConnectionurlConnection = (HttpURLConnection) url
.openConnection();
urlConnection.setRequestMethod("GET");//设置使用GET请求方式
urlConnection.setRequestProperty("Charset","UTF-8");//设置编码方式
urlConnection.setRequestProperty("Connection","Keep-Alive");//设置长时间连接
urlConnection.setConnectTimeout(5000);//设置连接超时
urlConnection.setReadTimeout(5000);//读取超时
3、//对响应码进行判断
if (urlConnection.getResponseCode() == 200) {
// 得到网络返回的输入流
InputStream is =urlConnection.getInputStream();
//GSON解析数据
。。。。。。
}
POST()请求:
2、 创建URL对象
URL url = new URL("http://zuowen.api.juhe.cn/zuowen/typeList");
注意:接口写法
2、//利用HttpClientConnection对象从网络中获取网页数据
HttpURLConnectionurlConnection = (HttpURLConnection) url
.openConnection();
urlConnection.setRequestMethod("POST");//设置使用GET请求方式
urlConnection.setDoOutput(true); // 允许输出
urlConnection.setRequestProperty("Charset","UTF-8");//设置编码方式
urlConnection.setRequestProperty("Connection","Keep-Alive");//设置长时间连接
urlConnection.setConnectTimeout(5000);//设置连接超时
urlConnection.setReadTimeout(5000);//读取超时
3、得到输出流
OutputStreamstream = urlConnection.getOutputStream();
PrintWriter writer =
newPrintWriter(stream);
writer.print("key=cbd1daab865bf46c4772a7bc0eb63cce&id=1");
writer.flush();
writer.close();
4、//对响应码进行判断
if (urlConnection.getResponseCode() == 200) {
// 得到网络返回的输入流
InputStream is =urlConnection.getInputStream();
//GSON解析数据
。。。。。。
}
三、URL
URL url = new URL(http://zuowen.api.juhe.cn/zuowen/typeList?key=cbd1daab865bf46c4772a7bc0eb63cce&id=1);
InputStream stream = url.openStream();
一、HttpHttpClient:
GET()请求:
1、// 创建一个默认的HttpClient
HttpClient client= new DefaultHttpClient();
client.getParams().setParameter(
CoreConnectionPNames.CONNECTION_TIMEOUT, 2000);// 连接时间
client.getParams().setParameter(
CoreConnectionPNames.SO_TIMEOUT, 2000);// 数据传输时间
2、//创建一个HttpGet请求
HttpGet get= new HttpGet("接口");
注意:接口必须完整,例如http://zuowen.api.juhe.cn/zuowen/typeList?key=cbd1daab865bf46c4772a7bc0eb63cce&id=1
3、//客户端使用GET方式执行请教,获得服务器端的回应response
HttpResponse response =client.execute(get);
4、//判断请求是否成功
if (response.getStatusLine().getStatusCode() == 200) {
// 获得输入流
InputStreamstream = response.getEntity().getContent();
//GSON解析数据
。。。。。。
}
POST()请求:
1、// 创建一个默认的HttpClient
HttpClient client= new DefaultHttpClient();
2、//创建一个HttpGet请求
HttpPost post= new HttpPost ("接口");
接口实例:http://v.juhe.cn/cell/get?mnc=0&cell=28655&lac=17695
3、创建泛型为NameValuePair的集合
List<NameValuePair> list = new ArrayList<NameValuePair>();
list.add(newBasicNameValuePair("key",
"1e1c029cc62bcbfb65934ddcc91b4b7c"));
UrlEncodedFormEntity formEntity =
new UrlEncodedFormEntity(
list);
4、//得到UrlEncodedFormEntity对象
post.setEntity(formEntity);
5、//客户端使用POST方式执行请教,获得服务器端的回应response
HttpResponse response = client.execute(post);
4、//判断请求是否成功
if (response.getStatusLine().getStatusCode() == 200) {
// 获得输入流
InputStreamstream = response.getEntity().getContent();
//GSON解析数据
。。。。。。
}
二、HttpURLConnection
GET()请求:
1、 创建URL对象
URL url = new URL("http://zuowen.api.juhe.cn/zuowen/typeList?key=cbd1daab865bf46c4772a7bc0eb63cce&id=1");
注意:接口完整
2、
// 利用HttpClientConnection对象从网络中获取网页数据
HttpURLConnectionurlConnection = (HttpURLConnection) url
.openConnection();
urlConnection.setRequestMethod("GET");//设置使用GET请求方式
urlConnection.setRequestProperty("Charset","UTF-8");//设置编码方式
urlConnection.setRequestProperty("Connection","Keep-Alive");//设置长时间连接
urlConnection.setConnectTimeout(5000);//设置连接超时
urlConnection.setReadTimeout(5000);//读取超时
3、//对响应码进行判断
if (urlConnection.getResponseCode() == 200) {
// 得到网络返回的输入流
InputStream is =urlConnection.getInputStream();
//GSON解析数据
。。。。。。
}
POST()请求:
2、 创建URL对象
URL url = new URL("http://zuowen.api.juhe.cn/zuowen/typeList");
注意:接口写法
2、//利用HttpClientConnection对象从网络中获取网页数据
HttpURLConnectionurlConnection = (HttpURLConnection) url
.openConnection();
urlConnection.setRequestMethod("POST");//设置使用GET请求方式
urlConnection.setDoOutput(true); // 允许输出
urlConnection.setRequestProperty("Charset","UTF-8");//设置编码方式
urlConnection.setRequestProperty("Connection","Keep-Alive");//设置长时间连接
urlConnection.setConnectTimeout(5000);//设置连接超时
urlConnection.setReadTimeout(5000);//读取超时
3、得到输出流
OutputStreamstream = urlConnection.getOutputStream();
PrintWriter writer =
newPrintWriter(stream);
writer.print("key=cbd1daab865bf46c4772a7bc0eb63cce&id=1");
writer.flush();
writer.close();
4、//对响应码进行判断
if (urlConnection.getResponseCode() == 200) {
// 得到网络返回的输入流
InputStream is =urlConnection.getInputStream();
//GSON解析数据
。。。。。。
}
三、URL
URL url = new URL(http://zuowen.api.juhe.cn/zuowen/typeList?key=cbd1daab865bf46c4772a7bc0eb63cce&id=1);
InputStream stream = url.openStream();
相关文章推荐
- java使用siger 轻松获取本机硬件信息(CPU 内存 网络 io等)
- C/S结构网络开发与B/S结构网络开发认识
- 黑马程序员--网络编程
- Http协议与TCP协议简单理解(转)
- Http协议与TCP协议简单理解(转)
- Http与TCP/IP 协议的关系
- HttpClient使用方法(包括POST文件)
- http协议中get和post的区别(转)
- Android中Textview显示带html文本三-------【Textview显示网络图片】
- Mac OS X 跟新到10.11后cocoapods安装出现的问题(- bad response Not Found 404 (http://ruby.taobao.org/latest_spe)
- 【转】【阮一峰的网络日志】Git 使用规范流程
- Android中基于Http协议的网络通信的两种方法Get和Post
- vmware中的网络(bridge,nat)
- 《深入理解Linux网络技术内幕》阅读笔记(三十一)
- TCP协议与UDP协议的区别
- 神奇的卷积神经网络(二)
- 浅谈HTTP的无状态性和Cookie、Session来源
- 黑马程序员-网络编程
- NFS 网络文件系统制作
- TCP/IP协议三次握手与四次握手流程解析