編輯: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/ | 潤物無聲
很多程序在完全載入之前往往會先顯示一個載入界面,提示用戶等待,這樣對提高用戶體
JSON代表JavaScript對象符號。它是一個獨立的數據交換格式,是XML的最佳替代品。本章介紹了如何解析JSON文件,並從中提取所需的信息。Android提供了四個
Android提供了許多方法來控制播放的音頻/視頻文件和流。其中該方法是通過一類稱為MediaPlayer。Android是提供MediaPlayer類訪問內置的媒體播放
第一,這貨速度太快,第二,模仿真機環境,第三,秒殺任何Android模擬器包括真機,不多說上圖,我忒忙! 官網: http://www.genymotion.co