Android使用httpPost向服務(wù)器發(fā)送請求的方法
更新時間:2015年12月29日 12:39:26 作者:q757989418
這篇文章主要介紹了Android使用httpPost向服務(wù)器發(fā)送請求的方法,實(shí)例分析了Android針對HttpPost類的操作技巧,需要的朋友可以參考下
本文實(shí)例講述了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ù)實(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實(shí)例
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ù)并解析的實(shí)現(xiàn)代碼
- Android客戶端post請求服務(wù)器端實(shí)例
- 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請求實(shí)用工具類
- android實(shí)用工具類分享(獲取內(nèi)存/檢查網(wǎng)絡(luò)/屏幕高度/手機(jī)分辨率)
- Android開發(fā)實(shí)現(xiàn)查詢遠(yuǎn)程服務(wù)器的工具類QueryUtils完整實(shí)例
相關(guān)文章
Android實(shí)現(xiàn)老虎機(jī)小游戲代碼示例
大家好,本篇文章主要講的是Android實(shí)現(xiàn)老虎機(jī)小游戲代碼示例,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下,方便下次瀏覽2021-12-12
Android Studio3.6設(shè)置Gradle Offline Mode的方法
這篇文章主要介紹了Android Studio3.6設(shè)置Gradle Offline Mode的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
Android實(shí)現(xiàn)EditText輸入金額
EditText是Android中一個非常實(shí)用的控件,有很多InputType,可以來達(dá)到不同的輸入效果,下面通過實(shí)例代碼給大家解析android實(shí)現(xiàn)edittext輸入金額,需要的朋友參考下吧2016-12-12
Android自定義View獲取注冊驗(yàn)證碼倒計時按鈕
這篇文章主要介紹了Android自定義View獲取驗(yàn)證碼倒計時按鈕的制作方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-10-10
Android時間選擇器、日期選擇器實(shí)現(xiàn)代碼
這篇文章主要為大家分別介紹了Android時間選擇器、日期選擇器實(shí)現(xiàn)代碼,感興趣的小伙伴們可以參考一下2016-04-04

