`
4ever000_1988
  • 浏览: 24355 次
  • 性别: Icon_minigender_1
  • 来自: 济南
社区版块
存档分类
最新评论
收藏列表
标题 标签 来源
HttpRequest android,http请求
public class BaseRequest {
    // 相当于一个浏览器,通过它的API我们可以很方便的发出get或者post请求
    private DefaultHttpClient client;
    
    public BaseRequest() {
        // 可以通过 HttpProtocolParams来设置HTTP头信息
        BasicHttpParams basichttpparams = new BasicHttpParams();
        // 设置连接池中取连接的超时时间 
        ConnManagerParams.setTimeout(basichttpparams, 1000L);
        // 设置连接超时
        HttpConnectionParams.setConnectionTimeout(basichttpparams, 5000);
        // 设置请求超时
        HttpConnectionParams.setSoTimeout(basichttpparams, 5000);
        // 设置我们的HttpClient支持HTTP和HTTPS两种模式
        SchemeRegistry schemeregistry = new SchemeRegistry();
        PlainSocketFactory plainsocketfactory = PlainSocketFactory.getSocketFactory();
        schemeregistry.register(new Scheme("http", plainsocketfactory, 80));
        SSLSocketFactory sslsocketfactory = SSLSocketFactory.getSocketFactory();
        schemeregistry.register(new Scheme("https", sslsocketfactory, 443));
        // 使用线程安全的连接管理来创建HttpClient
        client = new DefaultHttpClient(new ThreadSafeClientConnManager(basichttpparams, schemeregistry), basichttpparams);
    }
    
    public InputStream getRequest(String url)
            throws HttpException, IOException {
        HttpGet httpget = new HttpGet(url);
        HttpResponse httpresponse = client.execute(httpget);
        if(httpresponse.getStatusLine().getStatusCode() == 200) {
            return httpresponse.getEntity().getContent();
        } else {
            throw new HttpException("Error Response:" 
                + httpresponse.getStatusLine().toString());
        }
    }
    
    public String getRequest(List<NameValuePair> paramsList, String url)
            throws HttpException, IOException {
        HttpGet httpGet;
        HttpResponse httpResponse;
        NameValuePair nameValuePair;
        StringBuffer paramsBuffer = new StringBuffer();
        if(paramsList != null && paramsList.size() > 0) {
            Iterator<NameValuePair> iterator = paramsList.iterator();
            while (iterator.hasNext()) {
                nameValuePair = iterator.next();  
                URLEncoder.encode(nameValuePair.getValue(), "UTF-8");
                paramsBuffer.append(nameValuePair.getName())
                            .append('=')
                            .append(nameValuePair.getValue())
                            .append('&');
            }
            
            paramsBuffer.deleteCharAt(paramsBuffer.length() - 1);
        }
        
        if (paramsBuffer.length() > 0) {
            url = url + "?" + paramsBuffer.toString();
        }
        
        httpGet = new HttpGet(url);
        httpResponse = client.execute(httpGet);
        if(httpResponse.getStatusLine().getStatusCode() == 200) {
            return EntityUtils.toString(httpResponse.getEntity());
        } else {
            throw new HttpException("Error Response:" + 
                httpResponse.getStatusLine().toString());
        }
    }
    
    public String getInputString(HttpURLConnection connection)
            throws IOException {
        InputStream is = connection.getInputStream();
        if (is == null) {
            return null;
        }
        //封装字符流读写类
        BufferedReader bufferedReader = new BufferedReader(
                new InputStreamReader(is, HTTP.UTF_8), 8192);
        StringBuilder stringBuilder = new StringBuilder();
        String line;
        while ((line = bufferedReader.readLine()) != null) {
            stringBuilder.append(line);
        }
        if (bufferedReader != null) {
            bufferedReader.close();
        }
        connection.disconnect();
        return stringBuilder.toString();
    }
    
    public String postRequest(List<NameValuePair> paramsList, String url)
            throws HttpException, IOException {
        HttpPost httpPost = new HttpPost(url);
        UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(paramsList, "UTF-8");
        httpPost.setEntity(urlEncodedFormEntity);
        HttpResponse httpResponse = client.execute(httpPost);
        if(httpResponse.getStatusLine().getStatusCode() == 200) {
            return EntityUtils.toString(httpResponse.getEntity());
        } else {
                throw new HttpException("Error Response:" 
                    + httpResponse.getStatusLine().toString());
            }
        }
}
Global site tag (gtag.js) - Google Analytics