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

