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;
    }


  
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值