編輯:關於android開發
相信大家在開發 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/ | 潤物無聲
對話框AlertDialog的基本創建,對話框alertdialog 測試代碼: 布局: <RelativeLayout xmlns:
Android端通過HttpURLConnection上傳文件到服務器 Android端通過HttpURLConnection上傳文件到服務器 一:實現原理 最近在做An
1.在程序中添加一個斷點 如果所示:在Eclipse中添加了一個程序斷點 在Eclipse中一共有三種添加斷點的方法 第一種: 在紅框區域右鍵
【React Native開發】React Native移植原生Android項目 (一)前言 前三節課程我們已經對於React Native For Android的環