亲爱的!直接建此类传一个URL即可得到json串非常方便
2016-04-24 20:09
656 查看
//新建这个类,在需要的时候通过类名点方法名+你要请求数据的URL调用里面的getstring()方法,返回的就是一个json串用字符串接一下就直接可以用json解析了
get方法:
public class FenLei {
public static String getString(String uri){
HttpClient http=new DefaultHttpClient();
HttpGet httpGet=new HttpGet(uri);
try {
HttpResponse response=http.execute(httpGet);
if(response.getStatusLine().getStatusCode()==200){
HttpEntity entity = response.getEntity();
String string = EntityUtils.toString(entity,"utf-8");
return string;
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
post方法
//获得网络客户端
HttpClient client=new DefaultHttpClient();
//获得Post网络方法
HttpPost post=new HttpPost(uri);
//post的进行传参,NameValuePair名称值对,在http下有这样一个类
List<NameValuePair> list=new ArrayList<NameValuePair>();
//BasicNameValuePair表基本名称值对,前者表示=前的值,后者表示=后的值
list.add(new BasicNameValuePair("key","c56a18e43d8bdea60a341c5e80ade5d1"));
try {
//为网络设置实体内容
post.setEntity(new UrlEncodedFormEntity(list, "utf-8"));
//客户端Httpclient开始执行网络post请求,得到网络请求
HttpResponse response = client.execute(post);
//通过网络请求获得状态行的状态码
int statusCode = response.getStatusLine().getStatusCode();
System.out.println(statusCode);
//判断状态码是否等于200,如果等于200,则证明状态为连接
if(statusCode==200){
//得到请求实体内容的输入流
InputStream is = response.getEntity().getContent();
//实例化一输出流,将读到的内容通过输出流写入的客户端
ByteArrayOutputStream baos=new ByteArrayOutputStream();
//定义一个字节
byte[] b=new byte[1024];
//定义一个长度
int len;
//循环将读到的内容通过输出流写入的客户端
while((len=is.read(b))!=-1){
baos.write(b, 0, len);
}
json = baos.toString();
get方法:
public class FenLei {
public static String getString(String uri){
HttpClient http=new DefaultHttpClient();
HttpGet httpGet=new HttpGet(uri);
try {
HttpResponse response=http.execute(httpGet);
if(response.getStatusLine().getStatusCode()==200){
HttpEntity entity = response.getEntity();
String string = EntityUtils.toString(entity,"utf-8");
return string;
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
post方法
//获得网络客户端
HttpClient client=new DefaultHttpClient();
//获得Post网络方法
HttpPost post=new HttpPost(uri);
//post的进行传参,NameValuePair名称值对,在http下有这样一个类
List<NameValuePair> list=new ArrayList<NameValuePair>();
//BasicNameValuePair表基本名称值对,前者表示=前的值,后者表示=后的值
list.add(new BasicNameValuePair("key","c56a18e43d8bdea60a341c5e80ade5d1"));
try {
//为网络设置实体内容
post.setEntity(new UrlEncodedFormEntity(list, "utf-8"));
//客户端Httpclient开始执行网络post请求,得到网络请求
HttpResponse response = client.execute(post);
//通过网络请求获得状态行的状态码
int statusCode = response.getStatusLine().getStatusCode();
System.out.println(statusCode);
//判断状态码是否等于200,如果等于200,则证明状态为连接
if(statusCode==200){
//得到请求实体内容的输入流
InputStream is = response.getEntity().getContent();
//实例化一输出流,将读到的内容通过输出流写入的客户端
ByteArrayOutputStream baos=new ByteArrayOutputStream();
//定义一个字节
byte[] b=new byte[1024];
//定义一个长度
int len;
//循环将读到的内容通过输出流写入的客户端
while((len=is.read(b))!=-1){
baos.write(b, 0, len);
}
json = baos.toString();
相关文章推荐
- Move.js
- jsp页面上做判断
- JavaScript实现DDoS攻击原理,以及保护措施。
- JBox的简单使用Demo
- SprimgMVC 三 :返回json形式数据
- JS知识点简单总结
- Js答辩总结
- JS答辩习题
- JavaScript 获取访问主机资源信息
- plist解析和JSON序列化与反序列化(JSON解析),XML解析
- c标签遍历嵌套的List集合
- js之事件冒泡和事件捕获详细介绍
- JSTL标签循环集合列表
- 如何解决Servlet与jsp之间传递汉字出现乱码
- javascript KeyCode对照表
- JS的对象,事件
- [JS]常用特效js插件
- 5天揭秘js高级技术-第二天
- js实现的点击div区域外隐藏div区域
- js中判断数据类型的属性typeof、instanceof、 constructor、 prototype