httpclient 请求http数据,json转map的实例
更新时间:2016年12月31日 10:34:47 投稿:jingxian
下面小编就为大家带来一篇httpclient 请求http数据,json转map的实例。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
实例如下:
package com.idc.dataAudit.dataResource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.methods.GetMethod;
public class RequestHttpManage {
public Map<Integer,String> RequesthttpInfo(String url) {
String resultStr = null;
Map<Integer,String> mapResult = new HashMap<Integer,String>();
try{
HttpClient client = new HttpClient();
HttpMethod method=new GetMethod(url);
//使用POST方法
//HttpMethod method = new PostMethod("http://java.sun.com");
client.executeMethod(method);
//打印服务器返回的状态
//System.out.println(method.getProtocolVersion());
//System.out.println(method.getStatusLine().getStatusCode());
//System.out.println(method.getStatusLine().getReasonPhrase());
// System.out.println(method.getStatusLine().toString());
// System.out.println(method.getStatusLine());
//打印返回的信息
// System.out.println(method.getResponseBodyAsString());
mapResult.put(method.getStatusLine().getStatusCode(), method.getResponseBodyAsString());
//释放连接
method.releaseConnection();
}catch(Exception e){
e.printStackTrace();
}
System.out.println(mapResult);
return mapResult;
}
public Map<String,List<String>> GetMapFromJson(String jsonStr){
Map<String,List<String>> mapResult = new HashMap<String,List<String>>();
try{
JSONObject result = JSONObject.fromObject(jsonStr);//转换为JSONObject
JSONArray nameList = result.getJSONArray("result");//获取JSONArray
int length = nameList.size();
for(int i=0;i<length;i++){
JSONObject tempObject = nameList.getJSONObject(i);
Iterator iter = tempObject.keys();
String key = (String)iter.next();
String keyId = tempObject .getString(key);
List<String> objList = new ArrayList<String>();
for (; iter.hasNext();) {
String keyLocal = (String)iter.next();
// System.out.println(key+"="+tempObject .getString(keyLocal));
objList.add(tempObject .getString(keyLocal));
}
mapResult.put(keyId, objList);
}
}catch(Exception e){
e.printStackTrace();
}
System.out.println(mapResult);
return mapResult;
}
}
以上就是小编为大家带来的httpclient 请求http数据,json转map的实例全部内容了,希望大家多多支持脚本之家~
相关文章
SpringCloud使用Feign实现远程调用流程详细介绍
OpenFeign源于Netflix的Feign,是http通信的客户端。屏蔽了网络通信的细节,直接面向接口的方式开发,让开发者感知不到网络通信细节。所有远程调用,都像调用本地方法一样完成2023-02-02
Spring Boot报错:No session repository could be auto-configured
这篇文章主要给大家介绍了关于Spring Boot报错:No session repository could be auto-configured, check your configuration的解决方法,文中给出了详细的解决方法,对遇到这个问题的朋友们具有一定参考价值,需要的朋友下面来一起看看吧。2017-07-07
SSH框架网上商城项目第18战之过滤器实现购物登录功能的判断
这篇文章主要为大家详细介绍了SSH框架网上商城项目第18战:过滤器实现购物登录功能的判断,感兴趣的小伙伴们可以参考一下2016-06-06


最新评论