導航:首頁 > 操作系統 > androidhttpclient包

androidhttpclient包

發布時間:2022-09-27 15:09:20

『壹』 android中httpclient掉用的是什麼包

應該是你的Android版本太高,所以httpClient不能用了吧,把版本調整低點就可以了

『貳』 如何在Android開發中用HttpClient連接網路數據

HttpClient網路訪問

一、HttpClient網路訪問:
(一)、簡介:
1、Apache組織提供了HttpClient項目,可以實現網路訪問。在Android中,成功集成了HttpClient,所以在Android中可以直接使用HttpClient訪問網路。

2、與HttpURLConnection相比,HttpClient將前者中的輸入、輸出流操作,統一封裝成HttpGet、HttpPost、HttpRequest類。

HttpClient:網路連接對象;
HttpGet:代表發送GET請求;
HttpPost:代表發送POST請求;
HttpResponse:代表處理伺服器響應的對象。
HttpEntity對象:該對象中包含了伺服器所有的返回內容。
3、使用步驟:(六部曲)【重點】
創建HttpClient對象:通過實例化DefaultHttpClient獲得;
創建HttpGet或HttpPost對象:通過實例化 HttpGet或HttpPost 獲得,而構造方法的參數是urlstring(即需要訪問的網路url地址)。也可以通過調用setParams()方法來添加請求參數;
調用HttpClient對象的execute()方法,參數是剛才創建的 HttpGet或HttpPost對象 ,返回值是HttpResponse對象;
通過response對象中的getStatusLine()方法和getStatusCode()方法獲取伺服器響應狀態是否是200。
調用 HttpResponse對象的getEntity()方法,返回HttpEntity對象。而該對象中包含了伺服器所有的返回內容。
藉助EntityUtils的toString()方法或toByteArray()對 HttpEntity對象進行處理,也可以通過IO流對 HttpEntity對象進行操作。

(二)、封裝HttpClientHelper工具類:

public class HttpClientHelper {
public static HttpClient checkNetwork(String url) {
HttpClient httpClient = new DefaultHttpClient();
HttpGet request = new HttpGet(url);
HttpResponse httpResponse = null;
try {
httpResponse = httpClient.execute(request);
if (httpResponse.getStatusLine().getStatusCode() == 200) {
return httpClient;
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}

/**
* 作用:實現網路訪問文件,將獲取到數據儲存在文件流中
*
* @param url
* :訪問網路的url地址
* @return inputstream
*/
public static InputStream loadFileFromURL(String url) {
HttpClient httpClient = new DefaultHttpClient();
HttpGet requestGet = new HttpGet(url);
HttpResponse httpResponse;
try {
httpResponse = httpClient.execute(requestGet);
if (httpResponse.getStatusLine().getStatusCode() == 200) {
HttpEntity entity = httpResponse.getEntity();
return entity.getContent();
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

/**
* 作用:實現網路訪問文件,將獲取到的數據存在位元組數組中
*
* @param url
* :訪問網路的url地址
* @return byte[]
*/
public static byte[] loadByteFromURL(String url) {
HttpClient httpClient = new DefaultHttpClient();
HttpGet requestGet = new HttpGet(url);
try {
HttpResponse httpResponse = httpClient.execute(requestGet);
if (httpResponse.getStatusLine().getStatusCode() == 200) {
HttpEntity httpEntity = httpResponse.getEntity();
return EntityUtils.toByteArray(httpEntity);
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("====>" + e.toString());
}
return null;
}

/**
* 作用:實現網路訪問文件,返回字元串
*
* @param url
* :訪問網路的url地址
* @return String
*/
public static String loadTextFromURL(String url) {
HttpClient httpClient = new DefaultHttpClient();
HttpGet requestGet = new HttpGet(url);
try {
HttpResponse httpResponse = httpClient.execute(requestGet);
if (httpResponse.getStatusLine().getStatusCode() == 200) {
HttpEntity httpEntity = httpResponse.getEntity();
return EntityUtils.toString(httpEntity, "utf-8");
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

/**
* 作用:實現網路訪問文件,先給伺服器通過「GET」方式提交數據,再返回相應的數據
*
* @param url
* :訪問網路的url地址
* @param params
* String url:訪問url時,需要傳遞給伺服器的參數。
* 第二個參數格式為:username=wangxiangjun&password=123456
* @return byte[]
*/
public static byte[] doGetSubmit(String url, String params) {
HttpClient httpClient = new DefaultHttpClient();
HttpGet requestGet = new HttpGet(url + "?" + params);
try {
HttpResponse httpResponse = httpClient.execute(requestGet);
if (httpResponse.getStatusLine().getStatusCode() == 200) {
HttpEntity httpEntity = httpResponse.getEntity();
return EntityUtils.toByteArray(httpEntity);
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

/**
* 作用:實現網路訪問文件,先給伺服器通過「POST」方式提交數據,再返回相應的數據
*
* @param url
* :訪問網路的url地址
* @param params
* String url:訪問url時,需要傳遞給伺服器的參數。 第二個參數為:List<NameValuePair>
* @return byte[]
*/
public static byte[] doPostSubmit(String url, List<NameValuePair> params) {
HttpClient httpClient = new DefaultHttpClient();
HttpPost requestPost = new HttpPost(url);
try {
requestPost.setEntity(new UrlEncodedFormEntity(params, "utf-8"));
HttpResponse httpResponse = httpClient.execute(requestPost);
if (httpResponse.getStatusLine().getStatusCode() == 200) {
HttpEntity httpEntity = httpResponse.getEntity();
return EntityUtils.toByteArray(httpEntity);
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

/**
* 作用:實現網路訪問文件,先給伺服器通過「POST」方式提交數據,再返回相應的數據
*
* @param url
* :訪問網路的url地址
* @param params
* String url:訪問url時,需要傳遞給伺服器的參數。 Map<String , Object>
* @return byte[]
*/
public static byte[] doPostSubmit(String url, Map<String, Object> params) {
HttpClient httpClient = new DefaultHttpClient();
HttpPost requestPost = new HttpPost(url);

List<BasicNameValuePair> parameters = new ArrayList<BasicNameValuePair>();
try {
if (params != null) {
for (Map.Entry<String, Object> entry : params.entrySet()) {
String key = entry.getKey();
String value = entry.getValue().toString();
BasicNameValuePair nameValuePair = new BasicNameValuePair(
key, value);
parameters.add(nameValuePair);
}
}
requestPost
.setEntity(new UrlEncodedFormEntity(parameters, "utf-8"));
HttpResponse httpResponse = httpClient.execute(requestPost);

if (httpResponse.getStatusLine().getStatusCode() == 200) {
HttpEntity httpEntity = httpResponse.getEntity();
return EntityUtils.toByteArray(httpEntity);
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}

『叄』 Apache HttpClient 棄用(Android 9.0)

在 Android 6.0 中, 我們取消了對 Apache HTTP 客戶端的支持 。 從 Android 9 開始,默認情況下該內容庫已從 bootclasspath 中移除且不可用於應用。

要繼續使用 Apache HTTP 客戶端,以 Android 9 及更高版本為目標的應用可以向其 AndroidManifest.xml 添加以下內容:
注意:這個要放在application的節點下面

註:擁有最低 SDK 版本 23 或更低版本的應用需要 android:required="false" 屬性,因為在 API 級別低於 24 的設備上,org.apache.http.legacy 庫不可用。 (在這些設備上,Apache HTTP 類在 bootclasspath 中提供。)

作為使用運行時 Apache 庫的替代,應用可以在其 APK 中綁定自己的 org.apache.http 庫版本。 如果進行此操作,您必須將該庫重新打包(使用一個類似 Jar Jar 的實用程序)以避免運行時中提供的類存在類兼容性問題。

『肆』 android 包含 httpclient 嗎

android是java,httpclient好像是C,都不一樣好吧

『伍』 Android SDK使用HttpClient的問題,為什麼老是報錯,困擾了我好幾天,求大神解救!

騷年,忘了在Manifast裡面配置這個Activity了吧

開玩笑吧,不能在主線程操作UI。 UI線程一般就是主線程,不再UI線程操作UI在哪操作?!!
另開一個線程操作網路訪是為了防止阻塞UI線程,網路操作結果要反映到UI線程中還是要操作UI線程,不過不能直接操作,要通過handler機制

『陸』 android httpclient 還能用嗎

Android有一個AndroidHttpClient,實現了HttpClient介面,但是已經標記為Deprecated,不鼓勵使用了。因為Android鼓勵開發者使用HttpURLConnection這一套(在java.net包名下)。在Android blog官網上有專門說過原因,感興趣你可以搜一下。基本意思就是HttpURLConnection在Android下針對手機環境做過一些優化,而AndroidHttpClient已經不再維護。

『柒』 android的自帶的httpClient 怎麼上傳文件

Android上傳文件到服務端可以使用HttpConnection 上傳文件,也可以使用Android封裝好的HttpClient類。當僅僅上傳文件可以直接使用httpconnection 上傳比較方便快捷。

1、使用HttpConection上傳文件。將文件轉換成表單數據流。主要的思路就自己構造個http協議內容,服務端解析報文獲得表單數據。代碼片段:

[java] view plain
HttpURLConnection con;
try {
con = (HttpURLConnection) url.openConnection();
con.setConnectTimeout(C_TimeOut);
/* 允許Input、Output,不使用Cache */
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
/* 設置傳送的method=POST */
con.setRequestMethod("POST");
/* setRequestProperty */
con.setRequestProperty("Connection", "Keep-Alive");
con.setRequestProperty("Charset", "UTF-8");
con.setRequestProperty("Content-Type","multipart/form-data;boundary=" + boundary);

/* 設置DataOutputStream */
DataOutputStream ds = new DataOutputStream(con.getOutputStream());
FileInputStream fStream = new FileInputStream(file);

/* 設置每次寫入1024bytes */
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];

int length = -1;
/* 從文件讀取數據至緩沖區 */
while((length = fStream.read(buffer)) != -1)
{
/* 將資料寫入DataOutputStream中 */
ds.write(buffer, 0, length);
}
fStream.close();
ds.flush();
ds.close();

可以參考

①《在 Android 上通過模擬 HTTP multipart/form-data 請求協議信息實現圖片上傳》 (http://bertlee.iteye.com/blog/1134576)。

②《關於android Http訪問,上傳,用了三個方法 》

2、使用Android HttpClient類上傳參數。下面我在網上搜到得代碼,忘記出處了

[java] view plain
private static boolean sendPOSTRequestHttpClient(String path,
Map<String, String> params) throws Exception {
// 封裝請求參數
List<NameValuePair> pair = new ArrayList<NameValuePair>();
if (params != null && !params.isEmpty()) {
for (Map.Entry<String, String> entry : params.entrySet()) {
pair.add(new BasicNameValuePair(entry.getKey(), entry
.getValue()));
}
}
// 把請求參數變成請求體部分
UrlEncodedFormEntity uee = new UrlEncodedFormEntity(pair, "utf-8");
// 使用HttpPost對象設置發送的URL路徑
HttpPost post = new HttpPost(path);
// 發送請求體
post.setEntity(uee);
// 創建一個瀏覽器對象,以把POST對象向伺服器發送,並返回響應消息
DefaultHttpClient dhc = new DefaultHttpClient();
HttpResponse response = dhc.execute(post);
if (response.getStatusLine().getStatusCode() == 200) {
Log.i("http", "httpclient");
return true;
}
return false;
}

3、使用httpClient上傳文字信息和文件信息。使用httpClient上傳文件非常的方便。不過需要導入apache-mime4j-0.6.jar 和httpmime-4.0.jar兩個.jar包。

[java] view plain
// 封裝請求參數
MultipartEntity mpEntity = new MultipartEntity();
if (params != null && !params.isEmpty()) {
for (Map.Entry<String, String> entry : params.entrySet()) {

StringBody par = new StringBody(entry.getValue());
mpEntity.addPart(entry.getKey(), par);
}
}
// 圖片
if (!imagepath.equals("")) {
FileBody file = new FileBody(new File(imagepath));
mpEntity.addPart("photo", file);
}
// 使用HttpPost對象設置發送的URL路徑
HttpPost post = new HttpPost(path);
// 發送請求體
post.setEntity(mpEntity);
// 創建一個瀏覽器對象,以把POST對象向伺服器發送,並返回響應消息
DefaultHttpClient dhc = new DefaultHttpClient();
HttpResponse response = dhc.execute(post);

FileBody類可以把文件封裝到表單中,實現附件的上傳。

關於httpClient上傳文件可以參考鏈接: http://www.eoeandroid.com/forum.php?mod=viewthread&tid=76721&page=1

需要用的的ja下載地址r:http://download.csdn.net/detail/china1988s/3791514

參考:

①《在 Android 上通過模擬 HTTP multipart/form-data 請求協議信息實現圖片上傳》 (http://bertlee.iteye.com/blog/1134576)。

②《關於android Http訪問,上傳,用了三個方法 》

『捌』 eclipse httpclient4.jar 沖突 怎麼忽略android自帶的http包

一般jar包之間的沖突大多隻能是版本見得沖突。當你搭建完ssh框架之後,應該檢查加進來的jar包。有版本沖突的就刪掉。當我們加入spring和struts2的Jar時可能會有commons-logging-1.1.jar,commons-logging-1.1.1.jar的沖突,刪除低版本的即可。不過struts和hibernate之間的jar包沖突很少見,大概沒有吧。

『玖』 android開發工具ADT怎麼導入httpclient包求具體操作步驟。

在工程中(如果沒有)創建libs文件夾,把jar包放進去,右鍵點擊工程properties

選擇jar文件

『拾』 android httpclient在哪

1.Android API 23中拋棄了HttpClient ,不建議再使用,如果非要使用則導入該jar包,並在build.gradle文件中添加:
android {
useLibrary 'org.apache.http.legacy'
}

2.建議使用okhttp

閱讀全文

與androidhttpclient包相關的資料

熱點內容
找漫畫看應該下載什麼app 瀏覽:182
如何在vps上搭建自己的代理伺服器 瀏覽:744
nginxphp埠 瀏覽:403
內臟pdf 瀏覽:152
怎麼看雲伺服器架構 瀏覽:85
我的世界國際服為什麼登不進伺服器 瀏覽:996
微盟程序員老婆 瀏覽:930
intellij創建java 瀏覽:110
java連接odbc 瀏覽:38
啟動修復無法修復電腦命令提示符 瀏覽:359
手機編程是什麼 瀏覽:98
山東移動程序員 瀏覽:163
蘇州java程序員培訓學校 瀏覽:479
單片機液晶驅動 瀏覽:856
魔拆app里能拆到什麼 瀏覽:132
新預演算法的立法理念 瀏覽:144
wdcpphp的路徑 瀏覽:136
單片機p0口電阻 瀏覽:926
瀏覽器中調簡訊文件夾 瀏覽:595
五菱宏光空調壓縮機 瀏覽:70