直接上代码,post请求
public static String httpPost(String posturl,
List<BasicNameValuePair> listParmas, final Context context) {
activityContext = context;
String res = "";
/**
* 先创建连接
*/
HttpClient client = getHttpClient();
/**
* 使用Post请求,传入的Http地址正确
*/
HttpPost post = new HttpPost(posturl);
try {
if (listParmas.size() != 0) {
post.setEntity(new UrlEncodedFormEntity(listParmas, "utf-8"));
}
post.setHeader("token", HttpUrlNew.TOAKEN);
/**
* 开始执行post连接
*/
post.getParams().setParameter(
HttpConnectionParams.CONNECTION_TIMEOUT, TIME_OUT);// 请求超时
post.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT,
TIME_OUT);// 读取超时
HttpResponse response = client.execute(post);
if (response.getStatusLine().getStatusCode() == 200) {// 代表请求数据成功
HttpEntity entity = response.getEntity();
res = EntityUtils.toString(entity);
} else {
HttpEntity entity = response.getEntity();
res = EntityUtils.toString(entity);
handler.sendEmptyMessage(Util.HTTP_CONNECTION_500);
}
} catch (ClientProtocolException e) {
handler.sendEmptyMessage(Util.HTTP_CONNECTION_CLIENT_PROTOCOL_EXCEPTION);
e.printStackTrace();
} catch (ConnectTimeoutException e) {
handler.sendEmptyMessage(Util.HTTP_CONNECTION_TIMEOUT);
// e.printStackTrace();
} catch (UnknownHostException e) {
handler.sendEmptyMessage(Util.HTTP_CONNECTION_UNKONW_HOST);
} catch (IOException e) {
handler.sendEmptyMessage(Util.HTTP_CONNECTION_IO_EXCEPTION);
}
return res;
}
get请求
public static String httpGet(String posturl, Context context) {
activityContext = context;
String res = "";
/**
* 先创建连接
*/
// HttpClient client = getHttpClient();
HttpClient client = new DefaultHttpClient();
/**
* 使用get请求,传入的Http地址正确
*/
HttpGet get = new HttpGet(posturl);
try {
get.setHeader("token", HttpUrlNew.TOAKEN);
/**
* 开始执行get连接
*/
get.getParams().setParameter(
HttpConnectionParams.CONNECTION_TIMEOUT, TIME_OUT);// 请求超时
get.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT,
TIME_OUT);// 读取超时
HttpResponse response = client.execute(get);
if (response.getStatusLine().getStatusCode() == 200) {// 代表请求数据成功
HttpEntity entity = response.getEntity();
res = EntityUtils.toString(entity);
} else {
handler.sendEmptyMessage(Util.HTTP_CONNECTION_500);
}
} catch (ClientProtocolException e) {
handler.sendEmptyMessage(Util.HTTP_CONNECTION_CLIENT_PROTOCOL_EXCEPTION);
e.printStackTrace();
} catch (ConnectTimeoutException e) {
handler.sendEmptyMessage(Util.HTTP_CONNECTION_TIMEOUT);
// e.printStackTrace();
} catch (UnknownHostException e) {
handler.sendEmptyMessage(Util.HTTP_CONNECTION_UNKONW_HOST);
} catch (IOException e) {
handler.sendEmptyMessage(Util.HTTP_CONNECTION_IO_EXCEPTION);
}
return res;
}
上面方法中用到的getHttpClient方法
public static synchronized DefaultHttpClient getHttpClient() {
try {
if (defaultClient == null) {
// Log.v(TAG, "->> httpClient is null ->> do getHttpClient");
// 设置组件参数, HTTP协议的版本,1.1/1.0/0.9
HttpParams params = new BasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setUserAgent(params, "HttpComponents/1.1");
HttpProtocolParams.setUseExpectContinue(params, true);
// 设置连接超时时间
int REQUEST_TIMEOUT = 15 * 1000; // 设置请求超时15秒钟
int SO_TIMEOUT = 15 * 1000; // 设置等待数据超时时间15秒钟
HttpConnectionParams.setConnectionTimeout(params,
REQUEST_TIMEOUT);
HttpConnectionParams.setSoTimeout(params, SO_TIMEOUT);
ConnManagerParams.setTimeout(params, 1000); // 从连接池中取连接的超时时间
// 设置访问协议
SchemeRegistry schreg = new SchemeRegistry();
schreg.register(new Scheme("http", PlainSocketFactory
.getSocketFactory(), 80));
schreg.register(new Scheme("https", SSLSocketFactory
.getSocketFactory(), 443));
// 使用线程安全的连接管理来创建HttpClient
ClientConnectionManager conMgr = new ThreadSafeClientConnManager(
params, schreg);
defaultClient = new DefaultHttpClient(conMgr, params);
}
return defaultClient;
} catch (Exception e) {
return null;
}
}
在这个方法中defaultClient 字段是DefaultHttpClient对象.
在请求之后通过handler将得到的请求信息来更新页面.这里就不再贴代码了,