Andriod通用封装工具类--HttpCilentHelper

package com.dbwen.dish.help;


import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;


import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;


import android.util.Log;


public class HttpCilentHelper {


HttpClient hc ;
// HttpHost proxy ;
    private HttpParams httpParameters;
    private static final int timeoutConnection = 1000 * 60 * 1;
    private static final int timeoutSocket  = 1000 * 60 * 2;








    public HttpCilentHelper() {
         httpParameters = new BasicHttpParams();// Set the timeout in milliseconds until a connection is established.
        HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);// Set the default socket timeout (SO_TIMEOUT) // in milliseconds which is the timeout for waiting for data.
        HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
        hc = new DefaultHttpClient(httpParameters);
}




public String post(String url, Map params) throws Exception{
HttpPost hp = new HttpPost(url);
try {
if(params != null){
UrlEncodedFormEntity ueEntity;
List<NameValuePair> pairList = new ArrayList<NameValuePair>();
Iterator<String> iterator = params.keySet().iterator();
String key;
while(iterator.hasNext()){
key = iterator.next();
if(params.get(key) != null){
pairList.add(new BasicNameValuePair(key, params.get(key).toString()));
}
}
ueEntity = new UrlEncodedFormEntity(pairList, "utf-8");
hp.setEntity(ueEntity);
Log.w("HttpRequest_post", url);
Log.w("HttpRequest_post", EntityUtils.toString(hp.getEntity()));
}

HttpResponse response = hc.execute(hp);
// showResponse(response);
if(response.getStatusLine().getStatusCode() != HttpStatus.SC_OK){
throw new Exception("服务器异常。");
}
return EntityUtils.toString(response.getEntity());

} catch (Exception e) {
e.printStackTrace();
throw new Exception("网络连接出错:" + e.getLocalizedMessage());
}
finally{

}
}

public String get(String url, Map params) throws Exception{
StringBuilder sb = new StringBuilder();

Iterator<String> iterator = params.keySet().iterator();
String key;
while(iterator.hasNext()){
key = iterator.next();
if(params.get(key) != null){
sb.append("&");
sb.append(key);
sb.append("=");
sb.append(params.get(key).toString());
}
}
HttpGet hg = new HttpGet(url + sb.toString().replaceFirst("&", "?"));

try {
Log.w("HttpReqeust_get", hg.getURI().toURL().toString());
HttpResponse response = hc.execute(hg);
// showResponse(response);
if(response.getStatusLine().getStatusCode() != HttpStatus.SC_OK){
throw new Exception("服务器异常。");
}
return EntityUtils.toString(response.getEntity());

} catch (Exception e) {
e.printStackTrace();
throw new Exception("网络连接出错:" + e.getLocalizedMessage());
}
finally{

}
}

public void testSignUP(){

HttpPost hp = new HttpPost(
"http://74.207.247.243/tvqieke001/m/i_signup");
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("fullname", "����ú�"));
params.add(new BasicNameValuePair("username", "xnou@163.com"));
params.add(new BasicNameValuePair("password", "11111111"));
params.add(new BasicNameValuePair("password2", "11111111"));
params.add(new BasicNameValuePair("accept_terms", "1"));
UrlEncodedFormEntity ueEntity;
HttpResponse response;
try {
ueEntity = new UrlEncodedFormEntity(params, "utf-8");
hp.setEntity(ueEntity);
response = hc.execute(hp);
showResponse(response);

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
}

}

public void testLogin(){
HttpPost hp = new HttpPost(
"http://74.207.247.243/tvqieke001/m/i_login");
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("email", "sunyeqi@gmail.com"));
params.add(new BasicNameValuePair("password", "64587284"));
UrlEncodedFormEntity ueEntity;
HttpResponse response;
System.out.println("http request :");

try {

ueEntity = new UrlEncodedFormEntity(params, "utf-8");
hp.setEntity(ueEntity);
System.out.println(EntityUtils.toString(hp.getEntity()));
response = hc.execute(hp);
showResponse(response);

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{

}
}
public void testDashboard(){
HttpGet hg = new HttpGet(
"http://74.207.247.243/tvqieke001/m/i_dashboard?show=all&pg=1");
HttpResponse response;
try {
response = hc.execute(hg);
showResponse(response);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

private void showResponse(HttpResponse response) throws Exception{
Log.w("test","############################");
HttpEntity resEntity = response.getEntity();
Header[] headers = response.getAllHeaders();
Log.w("test","http headers :");
for(int i =0 ; i < headers.length; i++){
Log.w("test",headers[i].getName() + ":" + headers[i].getValue());
}
Log.w("test","http response entity :");
Log.w("test",EntityUtils.toString(resEntity));
Log.w("test","############################");
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("*************test testLogin ***************");
HttpCilentHelper t = new HttpCilentHelper();
t.testLogin();
System.out.println("*************test testLogin end ***************");
System.out.println("*************test testDashboard ***************");
t.testDashboard();
System.out.println("*************test testDashboard end***************");
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值