Android中使用Post請(qǐng)求的方法
本文實(shí)例講述了Android中使用Post請(qǐng)求的方法。分享給大家供大家參考。具體如下:
一、需要用到的場(chǎng)景
在jQuery中使用$.post()就可以方便的發(fā)起一個(gè)post請(qǐng)求,在android程序中有時(shí)也要從服務(wù)器獲取一些數(shù)據(jù),就也必須得使用post請(qǐng)求了。
二、需要用到的主要類(lèi)
在android中使用post請(qǐng)求主要要用到的類(lèi)是HttpPost、HttpResponse、EntityUtils
三、主要思路
1、創(chuàng)建HttpPost實(shí)例,設(shè)置需要請(qǐng)求服務(wù)器的url。
2、為創(chuàng)建的HttpPost實(shí)例設(shè)置參數(shù),參數(shù)設(shè)置時(shí)使用鍵值對(duì)的方式用到NameValuePair類(lèi)。
3、發(fā)起post請(qǐng)求獲取返回實(shí)例HttpResponse
4、使用EntityUtils對(duì)返回值的實(shí)體進(jìn)行處理(可以取得返回的字符串,也可以取得返回的byte數(shù)組)
代碼也比較簡(jiǎn)單,注釋也加上了,就直接貼出來(lái)了
package com.justsy.url; import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; import org.apache.http.protocol.HTTP; import org.apache.http.util.EntityUtils; import android.app.Activity; import android.os.Bundle; public class HttpURLActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); System.out.println("start url..."); String url = "http://192.168.2.112:8080/JustsyApp/Applet"; // 第一步,創(chuàng)建HttpPost對(duì)象 HttpPost httpPost = new HttpPost(url); // 設(shè)置HTTP POST請(qǐng)求參數(shù)必須用NameValuePair對(duì)象 List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("action", "downloadAndroidApp")); params.add(new BasicNameValuePair("packageId", "89dcb664-50a7-4bf2-aeed-49c08af6a58a")); params.add(new BasicNameValuePair("uuid", "test_ok1")); HttpResponse httpResponse = null; try { // 設(shè)置httpPost請(qǐng)求參數(shù) httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8)); httpResponse = new DefaultHttpClient().execute(httpPost); //System.out.println(httpResponse.getStatusLine().getStatusCode()); if (httpResponse.getStatusLine().getStatusCode() == 200) { // 第三步,使用getEntity方法活得返回結(jié)果 String result = EntityUtils.toString(httpResponse.getEntity()); System.out.println("result:" + result); T.displayToast(HttpURLActivity.this, "result:" + result); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } System.out.println("end url..."); setContentView(R.layout.main); } }
ADD:使用HttpURLConnection 進(jìn)行post請(qǐng)求:
String path = "http://192.168.2.115:8080/android-web-server/httpConnectServlet.do?PackageID=89dcb664-50a7-4bf2-aeed-49c08af6a58a"; URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST"); conn.setConnectTimeout(5000); System.out.println(conn.getResponseCode());
希望本文所述對(duì)大家的Android程序設(shè)計(jì)有所幫助。
相關(guān)文章
Android?Activity?View加載與繪制流程深入刨析源碼
這篇文章主要介紹了Android?Activity?View的加載與繪制流程源碼分析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08Android?ViewPager?+?Fragment實(shí)現(xiàn)滑動(dòng)頁(yè)面效果
本文通過(guò)實(shí)例代碼較詳細(xì)的給大家介紹了Android?ViewPager?+?Fragment實(shí)現(xiàn)滑動(dòng)頁(yè)面效果,需要的朋友可以參考下2018-06-06Android組件實(shí)現(xiàn)長(zhǎng)按彈出上下文菜單功能的方法
這篇文章主要介紹了Android組件實(shí)現(xiàn)長(zhǎng)按彈出上下文菜單功能的方法,結(jié)合實(shí)例形式分析了Android實(shí)現(xiàn)長(zhǎng)按彈出上下文菜單的具體步驟與相關(guān)操作技巧,需要的朋友可以參考下2017-07-07Android實(shí)現(xiàn)多線程下載圖片的方法
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)多線程下載圖片的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09Android用注解與反射實(shí)現(xiàn)Butterknife功能
Butterknife是一個(gè)在android上實(shí)現(xiàn)ioc(控制反轉(zhuǎn))的一個(gè)庫(kù)。ioc的核心是解耦。解耦的目的是修改耦合對(duì)象時(shí)不影響另外一個(gè)對(duì)象,降低模塊之間的關(guān)聯(lián)。在Spring中ioc更多的是依靠xml的配置。而android上的IOC框架可以不使用xml配置2022-11-11Android解決getExternalStorageDirectory在29后廢棄問(wèn)題(推薦)
這篇文章主要介紹了Android解決getExternalStorageDirectory在29后廢棄問(wèn)題(推薦),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02將替代ListView的RecyclerView 的使用詳解(一)
這篇文章主要介紹了將替代ListView的RecyclerView 的使用詳解(一)的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-07-07