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

2014-06-05
浏览
导读:相信大家在开发 Android 的过程中遇到过这么一种情况,那就是 Could not find class org.apache.commons.httpclient.HttpClient 。 尤其是在 eclipse 的插件ADT升级之后,很容易出现该问题,昨天Google放出了ADT的升级包,然后我也就升级了一下开发环境,没想

相信大家在开发 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. package com.shiny.mid.net;  
  2.  
  3. import org.apache.commons.httpclient.HttpClient;  
  4. import org.apache.commons.httpclient.HttpStatus;  
  5. import org.apache.commons.httpclient.methods.ByteArrayRequestEntity;  
  6. import org.apache.commons.httpclient.methods.GetMethod;  
  7. import org.apache.commons.httpclient.methods.PostMethod;  
  8.  
  9. public final class HSHttpClient {  
  10.  
  11.  private static final int CONNECTION_TIMEOUT = 20000;  
  12.  private static HSHttpClient mHSHttpClient;  
  13.  
  14.  private HSHttpClient() {  
  15.  }  
  16.  
  17.  public static synchronized HSHttpClient getInstance() {  
  18.   if (mHSHttpClient == null) {  
  19.    mHSHttpClient = new HSHttpClient();  
  20.   }  
  21.   return mHSHttpClient;  
  22.  }  
  23.  
  24.  /**  
  25.   * Using GET method.  
  26.   *  
  27.   * @param url  
  28.   *            The remote URL.  
  29.   * @param queryString  
  30.   *            The query string containing parameters  
  31.   * @return Response string.  
  32.   * @throws Exception  
  33.   */ 
  34.  public String httpGet(String url, String queryString) throws Exception {  
  35.   String responseData = null;  
  36.  
  37.   if (queryString != null && !queryString.equals(“”)) {  
  38.    url += “?” + queryString;  
  39.   }  
  40.  
  41.   HttpClient httpClient = new HttpClient();  
  42.   GetMethod httpGet = new GetMethod(url);  
  43.   httpGet.getParams().setParameter(“http.socket.timeout”,  
  44.     new Integer(CONNECTION_TIMEOUT));  
  45.  
  46.   try {  
  47.    int statusCode = httpClient.executeMethod(httpGet);  
  48.    if (statusCode != HttpStatus.SC_OK) {  
  49.     System.err.println(“HttpGet Method failed: “  
  50.       + httpGet.getStatusLine());  
  51.    }  
  52.    responseData = httpGet.getResponseBodyAsString();  
  53.   } catch (Exception e) {  
  54.    throw new Exception(e);  
  55.   } finally {  
  56.    httpGet.releaseConnection();  
  57.    httpClient = null;  
  58.   }  
  59.  
  60.   return responseData;  
  61.  }  
  62.  
  63.  /**  
  64.   * Using POST method.  
  65.   *  
  66.   * @param url  
  67.   *            The remote URL.  
  68.   * @param queryString  
  69.   *            The query string containing parameters  
  70.   * @return Response string.  
  71.   * @throws Exception  
  72.   */ 
  73.  public String httpPost(String url, String queryString) throws Exception {  
  74.   String responseData = null;  
  75.   HttpClient httpClient = new HttpClient();  
  76.   PostMethod httpPost = new PostMethod(url);  
  77.   httpPost.addParameter(“Content-Type”,  
  78.     ”application/x-www-form-urlencoded”);  
  79.   httpPost.getParams().setParameter(“http.socket.timeout”,  
  80.     new Integer(CONNECTION_TIMEOUT));  
  81.   if (queryString != null && !queryString.equals(“”)) {  
  82.    httpPost.setRequestEntity(new ByteArrayRequestEntity(queryString  
  83.      .getBytes()));  
  84.   }  
  85.  
  86.   try {  
  87.    int statusCode = httpClient.executeMethod(httpPost);  
  88.    if (statusCode != HttpStatus.SC_OK) {  
  89.     System.err.println(“HttpPost Method failed: “  
  90.       + httpPost.getStatusLine());  
  91.    }  
  92.    responseData = httpPost.getResponseBodyAsString();  
  93.   } catch (Exception e) {  
  94.    throw new Exception(e);  
  95.   } finally {  
  96.    httpPost.releaseConnection();  
  97.    httpClient = null;  
  98.   }  
  99.  
  100.   return responseData;  
  101.  }  
  102. }  
  103.  

【基于Google定制的HttpClient】

 

  1. package com.dlgreat.bbshow.net;  
  2.  
  3. import org.apache.http.HttpResponse;  
  4. import org.apache.http.HttpStatus;  
  5. import org.apache.http.client.HttpClient;  
  6. import org.apache.http.client.methods.HttpGet;  
  7. import org.apache.http.client.methods.HttpPost;  
  8. import org.apache.http.entity.StringEntity;  
  9. import org.apache.http.impl.client.DefaultHttpClient;  
  10. import org.apache.http.params.BasicHttpParams;  
  11. import org.apache.http.params.HttpConnectionParams;  
  12. import org.apache.http.params.HttpParams;  
  13. import org.apache.http.util.EntityUtils;  
  14.  
  15. public final class BBHttpClient {  
  16.  
  17.  private static final int CONNECTION_TIMEOUT = 20000;  
  18.  private static BBHttpClient mHSHttpClient;  
  19.  
  20.  private BBHttpClient() {  
  21.  }  
  22.  
  23.  public static synchronized BBHttpClient getInstance() {  
  24.   if (mHSHttpClient == null) {  
  25.    mHSHttpClient = new BBHttpClient();  
  26.   }  
  27.   return mHSHttpClient;  
  28.  }  
  29.  
  30.  /**  
  31.   * Using GET method.  
  32.   *  
  33.   * @param url  
  34.   *            The remote URL.  
  35.   * @param queryString  
  36.   *            The query string containing parameters  
  37.   * @return Response string.  
  38.   * @throws Exception  
  39.   */ 
  40.  public String httpGet(String url, String queryString) throws Exception {  
  41.   if (queryString != null && !queryString.equals(“”)) {  
  42.    url += “?” + queryString;  
  43.   }  
  44.  
  45.   HttpGet httpGet = new HttpGet(url);  
  46.  
  47.   HttpParams params = new BasicHttpParams();  
  48.   HttpConnectionParams.setConnectionTimeout(params, CONNECTION_TIMEOUT);  
  49.   HttpConnectionParams.setSoTimeout(params, CONNECTION_TIMEOUT);  
  50.   HttpClient httpClient = new DefaultHttpClient(params);  
  51.  
  52.   try {  
  53.    HttpResponse response = httpClient.execute(httpGet);  
  54.    if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {  
  55.     System.err.println(“HttpGet Method failed: “  
  56.       + response.getStatusLine().toString());  
  57.     httpGet.abort();  
  58.    }  
  59.    return EntityUtils.toString(response.getEntity());  
  60.   } catch (Exception e) {  
  61.    throw new Exception(e);  
  62.   } finally {  
  63.    httpClient.getConnectionManager().shutdown();  
  64.   }  
  65.  }  
  66.  
  67.  /**  
  68.   * Using POST method.  
  69.   *  
  70.   * @param url  
  71.   *            The remote URL.  
  72.   * @param queryString  
  73.   *            The query string containing parameters  
  74.   * @return Response string.  
  75.   * @throws Exception  
  76.   */ 
  77.  public String httpPost(String url, String queryString) throws Exception {  
  78.   HttpPost httpPost = new HttpPost(url);  
  79.  
  80.   if (queryString != null && !queryString.equals(“”)) {  
  81.    StringEntity se = new StringEntity(queryString);  
  82.    httpPost.setHeader(“Content-Type”,  
  83.      ”application/x-www-form-urlencoded”);  
  84.    httpPost.setEntity(se);  
  85.   }  
  86.  
  87.   HttpParams params = new BasicHttpParams();  
  88.   HttpConnectionParams.setConnectionTimeout(params, CONNECTION_TIMEOUT);  
  89.   HttpConnectionParams.setSoTimeout(params, CONNECTION_TIMEOUT);  
  90.   HttpClient httpClient = new DefaultHttpClient(params);  
  91.  
  92.   try {  
  93.    HttpResponse response = httpClient.execute(httpPost);  
  94.    if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {  
  95.     System.err.println(“HttpPost Method failed: “  
  96.       + response.getStatusLine().toString());  
  97.     httpPost.abort();  
  98.    }  
  99.    return EntityUtils.toString(response.getEntity());  
  100.   } catch (Exception e) {  
  101.    throw new Exception(e);  
  102.   } finally {  
  103.    httpClient.getConnectionManager().shutdown();  
  104.   }  
  105.  }  
  106. }  
  107.  

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

编程实现Android远程控制PC

[Android的系统移植与平台开发]Sensor HAL

[Android的系统移植与平台开发]Sensor HAL

用ViewPager实现高仿图片左右滑动自动切换

详解android Content Provider[6]