喜糖

移动开发工程师 。涉及 android、ios、jni

导航

【一找客户端】针对网络连接的封装

Posted on 2011-09-22 01:05  喜糖  阅读(1324)  评论(0编辑  收藏  举报

andorid框架中加入了org.apache.http包。这样就可以使用apache包中关于HTTP的内容, 极大的方便了针对HTTP协议的开发。

在一找客户端中不仅仅使用apache的方法,而已在apache的基础之上,就对其进行一次封装。 这就是本篇所要介绍的内容。

代码分析。 

1。HttpManager工具类的代码使用

        //传递的参数
List<NameValuePair> params = new ArrayList<NameValuePair>();

//s为接到的返回值
String s = new HttpManager(Const.HOT_URL).submitRequest(params);

2。 HttpManager的初始化

    public HttpManager(String url_in) {
init(url_in);
}

/**
* 初始化参数
*
@param url
*/
private void init(String url_in){
this.url = url_in;
Log.d(Const.TAG, "HttpManager.init|url="+url_in);
try {
httpclient = new DefaultHttpClient(); //设置本地为客户端
httpclient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, Const.TIMEOUT_15);//请求超时
httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, Const.TIMEOUT_15); //读取超时
httpPost = new HttpPost(url); //新建一个Post请求
}catch (Exception e) {
httpPost.abort();
Log.e(Const.TAG, "网络连接不正常!");
}
}


3。 发送Post请求

    /**
* 提交HTTP请求
*
@param params 请求参数
*
@return
*/
public String submitRequest(List<NameValuePair> params){

String res_str = null; //收到的结果

if(httpclient!=null && httpPost!=null && !httpPost.isAborted()){

//设置httpPost请求参数及编码
try {
//用确定编码格式来把参数进行编码
httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
} catch (UnsupportedEncodingException e1) {
Log.e(Const.TAG, "输入参数编码有问题:UnsupportedEncodingException");
return res_str;
}

//第二步,使用execute方法发送HTTP POST请求,并返回HttpResponse对象
try {
httpResponse =httpclient.execute(httpPost);
} catch (ClientProtocolException e) {
Log.e(Const.TAG, "服务器无应答:ClientProtocolException");
return res_str;
} catch (IOException e) {
Log.e(Const.TAG, "服务器无应答:IOException");
return res_str;
} catch (Exception e) {
Log.e(Const.TAG, "服务器无应答:OtherException");
return res_str;
}

//返回结果为200时,处于正常状态。
if(httpResponse!=null && (httpResponse.getStatusLine().getStatusCode() == 200)){

//第三步,使用getEntity方法活得返回结果
try {
res_str = EntityUtils.toString(httpResponse.getEntity(),HTTP.UTF_8);
} catch (ParseException e) {
Log.e(Const.TAG, "返回结果转换异常:ParseException");
e.printStackTrace();
}catch (IOException e) {
Log.e(Const.TAG, "返回结果输出异常:IOException");
}catch (Exception e) {
Log.e(Const.TAG, "返回结果异常:OtherException");

}
}

httpPost.abort();

//强制关闭连接
httpclient.getConnectionManager().closeExpiredConnections();

}

Log.d(Const.TAG, "HttpManager.res_str="+res_str);

return res_str;
}

4。 获取返回值的长度。 

    /**
* 获取返回内容长度
*
@return
*/
public long getContentLength(){

long len = -1;

if(httpclient!=null && httpPost!=null && !httpPost.isAborted()){

//设置httpPost请求参数及编码
try {
List<NameValuePair> params = new ArrayList<NameValuePair>();
httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
} catch (UnsupportedEncodingException e1) {
Log.e(Const.TAG, "getContentLength 输入参数编码有问题:UnsupportedEncodingException");
return len;
}

//第二步,使用execute方法发送HTTP POST请求,并返回HttpResponse对象
try {
httpResponse =httpclient.execute(httpPost);
} catch (ClientProtocolException e) {
Log.e(Const.TAG, "getContentLength 服务器无应答:ClientProtocolException");
} catch (IOException e) {
Log.e(Const.TAG, "getContentLength 服务器无应答:IOException");
} catch (Exception e) {
Log.e(Const.TAG, "getContentLength 服务器无应答:OtherException");
}

if(httpResponse!=null && (httpResponse.getStatusLine().getStatusCode() == 200)){

//第三步,使用getEntity方法活得返回结果
try {
HttpEntity httpEntity = httpResponse.getEntity();
len = httpEntity.getContentLength();
} catch (Exception e) {
Log.e(Const.TAG, "getContentLength 返回结果异常:OtherException");
}
}

httpPost.abort();

httpclient.getConnectionManager().closeExpiredConnections();

}

Log.d(Const.TAG, "HttpManager.getContentLength|len="+len);

return len;
}


PS: 追后,还是附上
【一找客户端】工程代码。注意查看代码:HttpManager