Android使用httpPost向服務(wù)器發(fā)送請求的方法
更新時間:2015年12月29日 12:39:26 作者:q757989418
這篇文章主要介紹了Android使用httpPost向服務(wù)器發(fā)送請求的方法,實例分析了Android針對HttpPost類的操作技巧,需要的朋友可以參考下
本文實例講述了Android使用httpPost向服務(wù)器發(fā)送請求的方法。分享給大家供大家參考,具體如下:
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import android.util.Log;
public class RequestByHttpPost {
public static String TIME_OUT = "操作超時";
public static String doPost(List<NameValuePair> params,String url) throws Exception{
String result = null;
// 新建HttpPost對象
HttpPost httpPost = new HttpPost(url);
// 設(shè)置字符集
HttpEntity entity = new UrlEncodedFormEntity(params, HTTP.UTF_8);
// 設(shè)置參數(shù)實體
httpPost.setEntity(entity);
// 獲取HttpClient對象
HttpClient httpClient = new DefaultHttpClient();
//連接超時
httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 30000);
//請求超時
httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 30000);
try {
// 獲取HttpResponse實例
HttpResponse httpResp = httpClient.execute(httpPost);
// 判斷是夠請求成功
if (httpResp.getStatusLine().getStatusCode() == 200) {
// 獲取返回的數(shù)據(jù)
result = EntityUtils.toString(httpResp.getEntity(), "UTF-8");
Log.i("HttpPost", "HttpPost方式請求成功,返回數(shù)據(jù)如下:");
Log.i("result", result);
} else {
Log.i("HttpPost", "HttpPost方式請求失敗");
}
} catch (ConnectTimeoutException e){
result = TIME_OUT;
}
return result;
}
}
可以直接用的完整類。
希望本文所述對大家Android程序設(shè)計有所幫助。
您可能感興趣的文章:
- android客戶端從服務(wù)器端獲取json數(shù)據(jù)并解析的實現(xiàn)代碼
- Android客戶端post請求服務(wù)器端實例
- Android編程之客戶端通過socket與服務(wù)器通信的方法
- android異步請求服務(wù)器數(shù)據(jù)示例
- Android編程向服務(wù)器發(fā)送請求時出現(xiàn)中文亂碼問題的解決方法
- Android解析服務(wù)器端發(fā)來的xml數(shù)據(jù)示例
- Android TCP 文件客戶端與服務(wù)器DEMO介紹
- Android程序開發(fā)通過HttpURLConnection上傳文件到服務(wù)器
- Android封裝的http請求實用工具類
- android實用工具類分享(獲取內(nèi)存/檢查網(wǎng)絡(luò)/屏幕高度/手機分辨率)
- Android開發(fā)實現(xiàn)查詢遠程服務(wù)器的工具類QueryUtils完整實例
相關(guān)文章
Jetpack Compose實現(xiàn)列表和動畫效果詳解
這篇文章主要為大家詳細(xì)講講Jetpack Compose實現(xiàn)列表和動畫效果的方法步驟,文中的代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2022-06-06
詳解Android數(shù)據(jù)存儲之SQLCipher數(shù)據(jù)庫加密
對于已經(jīng)ROOT的手機來說的沒有任何安全性可以,一旦被利用將會導(dǎo)致數(shù)據(jù)庫數(shù)據(jù)的泄漏,本篇文章主要介紹了Android數(shù)據(jù)存儲之SQLCipher數(shù)據(jù)庫加密,具有一定的參考價值,有需要的可以了解一下。2016-12-12
adb wireless進行Android手機調(diào)試詳解
這篇文章給大家講解了在Android手機上使用adb wireless進行調(diào)試的步驟以及問題解決辦法,有需要的跟著學(xué)習(xí)下吧。2017-12-12
Android編程實現(xiàn)設(shè)置按鈕背景透明與半透明及圖片背景透明的方法
這篇文章主要介紹了Android編程實現(xiàn)設(shè)置按鈕背景透明與半透明及圖片背景透明的方法,結(jié)合實例形式較為詳細(xì)的分析了Button及ImageButton的背景屬性設(shè)置技巧,非常簡單實用,需要的朋友可以參考下2015-12-12

