相信大家在开发 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | 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; } } |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | 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(); } } } |
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(); } } }
楼主还有其他方法吗,可以私下知道我是新手qq:1219527704
博主:和你遇到同样的问题,但是我不知道怎么像你说的那样改源码文件,能指点下吗,感觉不敬啊!先谢过…………[失落]
@1314hwl: qq:405146836交流下