Android 开发之 HttpClient Class can not find 之解决办法

相信大家在开发 Android 的过程中遇到过这么一种情况,那就是 “ Could not find class ‘org.apache.commons.httpclient.HttpClient’”。

尤其是在 eclipse 的插件ADT升级之后,很容易出现该问题,昨天Google放出了ADT的升级包,然后我也就升级了一下开发环境,没想到前天还运行好好的程序,今天突然就不会工作了,检查log发现,HttpClient无法找到,但是在普通的Java运行环境下就可以正常运行。

因为Apache的HttpClient开发包,Google自己也定制了一份,已经内置到开发环境中了,所以我们如果使用纯粹的Apache原生态的HttpClient开发包就可能出现冲突的问题,每当Google升级ADT之后,说不定程序就Over了。

实在没有办法,那就把基于Apache原生态的HttpClient完全替换成Google定制后的类型吧,转换前后的代码对比如下:

基于Apache原生态的HttpClient

package com.shiny.mid.net;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.ByteArrayRequestEntity;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;

public final class HSHttpClient {

private static final int CONNECTION_TIMEOUT = 20000;
private static HSHttpClient mHSHttpClient;

private HSHttpClient() {
}

public static synchronized HSHttpClient getInstance() {
if (mHSHttpClient == null) {
mHSHttpClient = new HSHttpClient();
}
return mHSHttpClient;
}

/**
* Using GET method.
*
* @param url
*            The remote URL.
* @param queryString
*            The query string containing parameters
* @return Response string.
* @throws Exception
*/
public String httpGet(String url, String queryString) throws Exception {
String responseData = null;

if (queryString != null && !queryString.equals("")) {
url += "?" + queryString;
}

HttpClient httpClient = new HttpClient();
GetMethod httpGet = new GetMethod(url);
httpGet.getParams().setParameter("http.socket.timeout",
new Integer(CONNECTION_TIMEOUT));

try {
int statusCode = httpClient.executeMethod(httpGet);
if (statusCode != HttpStatus.SC_OK) {
System.err.println("HttpGet Method failed: "
+ httpGet.getStatusLine());
}
responseData = httpGet.getResponseBodyAsString();
} catch (Exception e) {
throw new Exception(e);
} finally {
httpGet.releaseConnection();
httpClient = null;
}

return responseData;
}

/**
* Using POST method.
*
* @param url
*            The remote URL.
* @param queryString
*            The query string containing parameters
* @return Response string.
* @throws Exception
*/
public String httpPost(String url, String queryString) throws Exception {
String responseData = null;
HttpClient httpClient = new HttpClient();
PostMethod httpPost = new PostMethod(url);
httpPost.addParameter("Content-Type",
"application/x-www-form-urlencoded");
httpPost.getParams().setParameter("http.socket.timeout",
new Integer(CONNECTION_TIMEOUT));
if (queryString != null && !queryString.equals("")) {
httpPost.setRequestEntity(new ByteArrayRequestEntity(queryString
.getBytes()));
}

try {
int statusCode = httpClient.executeMethod(httpPost);
if (statusCode != HttpStatus.SC_OK) {
System.err.println("HttpPost Method failed: "
+ httpPost.getStatusLine());
}
responseData = httpPost.getResponseBodyAsString();
} catch (Exception e) {
throw new Exception(e);
} finally {
httpPost.releaseConnection();
httpClient = null;
}

return responseData;
}
}

基于Google定制的HttpClient

package com.dlgreat.bbshow.net;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
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 final class BBHttpClient {

private static final int CONNECTION_TIMEOUT = 20000;
private static BBHttpClient mHSHttpClient;

private BBHttpClient() {
}

public static synchronized BBHttpClient getInstance() {
if (mHSHttpClient == null) {
mHSHttpClient = new BBHttpClient();
}
return mHSHttpClient;
}

/**
* Using GET method.
*
* @param url
*            The remote URL.
* @param queryString
*            The query string containing parameters
* @return Response string.
* @throws Exception
*/
public String httpGet(String url, String queryString) throws Exception {
if (queryString != null && !queryString.equals("")) {
url += "?" + queryString;
}

HttpGet httpGet = new HttpGet(url);

HttpParams params = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(params, CONNECTION_TIMEOUT);
HttpConnectionParams.setSoTimeout(params, CONNECTION_TIMEOUT);
HttpClient httpClient = new DefaultHttpClient(params);

try {
HttpResponse response = httpClient.execute(httpGet);
if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
System.err.println("HttpGet Method failed: "
+ response.getStatusLine().toString());
httpGet.abort();
}
return EntityUtils.toString(response.getEntity());
} catch (Exception e) {
throw new Exception(e);
} finally {
httpClient.getConnectionManager().shutdown();
}
}

/**
* Using POST method.
*
* @param url
*            The remote URL.
* @param queryString
*            The query string containing parameters
* @return Response string.
* @throws Exception
*/
public String httpPost(String url, String queryString) throws Exception {
HttpPost httpPost = new HttpPost(url);

if (queryString != null && !queryString.equals("")) {
StringEntity se = new StringEntity(queryString);
httpPost.setHeader("Content-Type",
"application/x-www-form-urlencoded");
httpPost.setEntity(se);
}

HttpParams params = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(params, CONNECTION_TIMEOUT);
HttpConnectionParams.setSoTimeout(params, CONNECTION_TIMEOUT);
HttpClient httpClient = new DefaultHttpClient(params);

try {
HttpResponse response = httpClient.execute(httpPost);
if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
System.err.println("HttpPost Method failed: "
+ response.getStatusLine().toString());
httpPost.abort();
}
return EntityUtils.toString(response.getEntity());
} catch (Exception e) {
throw new Exception(e);
} finally {
httpClient.getConnectionManager().shutdown();
}
}
}

本文原始链接:http://blog.zhourunsheng.com/2012/03/android-%e5%bc%80%e5%8f%91%e4%b9%8b-httpclient-class-can-not-find-%e4%b9%8b%e8%a7%a3%e5%86%b3%e5%8a%9e%e6%b3%95/(迁移后固定链接原样保留)

历史评论 · 3 条

只读存档

以下评论来自原 WordPress 站点,已剔除邮箱、IP、浏览器标识与用户 ID,正文中的个人信息也已掩码。这是内容保全存档,不参与新评论的回复;想说点什么,请到下方「加入讨论」。

  1. 1314hwl #488
    博主:和你遇到同样的问题,但是我不知道怎么像你说的那样改源码文件,能指点下吗,感觉不敬啊!先谢过…………[失落]
  2. 1314hwl #489 回复
    @1314hwl: qq:405146836交流下
  3. 刘宇洲 #639
    楼主还有其他方法吗,可以私下知道我是新手qq:1219527704

加入讨论

新评论 — 条

评论保存在本站自托管服务里,无需注册登录,填个昵称就能留言;不依赖任何第三方 Cookie 或账号。请尽量说点有用的 :)