HttpClient使用二

 

   封装后返回Map、实体、List中含有实体, 等。

import java.util.ArrayList;
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.params.HttpMethodParams;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

/**
* @ClassName: HttpClientUtil 
* @date 2015年10月14日 下午4:32:36 
* @Description:
 */
public class HttpClientUtil {
	
	
	public static JSONArray getJsonArrayFromUrl(String urlStr,  
            Map<String, String> params){
		String result = getStrFromUrl(urlStr,params);
		return  JSONArray.fromObject(result ) ; 
	}
	
	public static Map<String,Object> getMapFromUrl(String urlStr,  
            Map<String, String> params){
		String result = getStrFromUrl(urlStr,params);
		return JSONObject.fromObject( result); 
	}
 
	
    /** 
     * 模拟url访问 从特定的url中获取json 
     * @param urlStr 
     * @param params 
     * @return json object ,or null if failed 
     * 参数经过封装后传过来 ,提交为 post请求 
     */  
    public static String getStrFromUrl(String urlStr,  
            Map<String, String> params) {  
        HttpClient httpClient = new DefaultHttpClient();  
        httpClient.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "GBK");
        
        HttpPost httpPost = new HttpPost(urlStr);  
        
        String content="" ; 
        try {  
            if (params != null) {  
                Iterator<String> keys = params.keySet().iterator();  
                List<NameValuePair> nvps = new ArrayList<NameValuePair>();  
                while (keys.hasNext()) {  
                    String key = keys.next();  
                    nvps.add(new BasicNameValuePair(key, params.get(key)));  
                }  
                httpPost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));  
            }  
            HttpResponse response = httpClient.execute(httpPost);  
            HttpEntity entity = response.getEntity();
            content = EntityUtils.toString(entity , "utf-8"); 
            
        } catch (Exception e) {  
             e.printStackTrace();  
        }  
  
        return content;  
    }
    /**
     *  查询list<实体> 数据 
    *   @Title getListFromUrl 
    *   @param url
    *   @param params
    *   @param clazz
    *   @return
    *   @return List<T>    
    *   @throws 
    *
     */
	public static <T extends Object> List<T> getListFromUrl(
			String url, Map<String, String> params , Class<T> clazz ) {
		String result = getStrFromUrl(url,params);
		if(result==null || "".equals( result)){
			return null ;
		}
		JSONArray jsonarray = JSONArray.fromObject(result );  
		List<T> list = (List<T>)JSONArray.toList(jsonarray, clazz); 
		return list;
	}  
	
	/**
	 * 得到对象从实体里面
	*   @Title getEntityFromUrl 
	*   @param url
	*   @param params
	*   @param clazz
	*   @return
	*   @return T    
	*   @Description  
	*   @throws 
	*
	 */
	public static <T extends Object> T getEntityFromUrl(
			String url, Map<String, String> params , Class<T> clazz ) {
		String result = getStrFromUrl(url,params);
		if(result==null || "".equals( result)){
			return null ;
		}
		JSONObject jb = JSONObject.fromObject(result);
		T t = (T) JSONObject.toBean( jb,  clazz ) ;
		return  t ; 
	}  

 


	public static void main(String[] args) {
//		Map map = new HashMap();
//		map.put("result", "1");
//		map.put("status", "2");
//		NetworkdriveFile net = new NetworkdriveFile();
//		net.setChapter_id("1");
//		net.setChapter_name("name");;
//		net.setCom_name("com Name");
//		
//		NetworkdriveFile net2 = new NetworkdriveFile();
//		net2.setChapter_id("1");
//		net2.setChapter_name("name");;
//		net2.setCom_name("com Name");
//		List list = new ArrayList();
//		list.add( net);
//		list.add(net2); 
//		map.put("list", list);
//		
//		
//		String str = JSONObject.fromObject(map ).toString();
//		System.out.println( map );
//		HashMap map3 = reflect(JSONObject.fromObject(str) ) ; 
//		System.out.println( map3.get("result" ));
//		List list3 = reflect(JSONArray.fromObject( map3.get("list")));
//		NetworkdriveFile ndf =  (NetworkdriveFile) JSONObject.toBean( JSONObject.fromObject(list3.get(0) )  ,   NetworkdriveFile.class ); 
//		System.out.println( list3.get(0) );
		 
//		JSONObject map5 = JSONObject.fromObject(str  ) ; 
//		List<NetworkdriveFile> list5  = JSONArray.toList(JSONArray.fromObject(map5.get("list") ), NetworkdriveFile.class); 
//		System.out.println( list5 );
//		System.out.println( map5.get("result"));
//		
//		List list2 = (List) map1.get("list"); 
//		JSONArray.toList(list2, NetworkdriveFile.class) ; 
//		
//		NetworkdriveFile net3 = (NetworkdriveFile)JSONObject.toBean((JSONObject)list2.get(0) ) ; 
		
		 System.out.println( "22222222222");
//		List<NetworkdriveFile> list2 = HttpClientUtil.getListFromUrl2( NetworkdriveFile.class );
//		System.out.println( list2 );
		 
	}
}

 

你可能感兴趣的:(HttpClient使用二)