IT月光

导航

Httpclient请求数据

package com.baidu.myutils;

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;

public class MyUtils {
    
    String url="http://apis.juhe.cn/goodbook/catalog?key=9d6ef8c31647a206e05fcaff70527182&dtype=json";

    
    public static String Check_j(String url){
        String str="";
        HttpGet get=new HttpGet(url);
        HttpParams params=new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(params, 5000);
        HttpConnectionParams.setSoTimeout(params, 5000);
        HttpClient client=new DefaultHttpClient(params);
        try {
            HttpResponse response=client.execute(get);
            
            
            if(response.getStatusLine().getStatusCode()==200){
                HttpEntity entity=response.getEntity();
                str=EntityUtils.toString(entity, "utf-8");
                
            }
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return str;
    }
}

 

posted on 2016-05-02 17:30  IT月光  阅读(165)  评论(0编辑  收藏  举报