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/ | 润物无声