Android中使用Post請求的方法
本文實例講述了Android中使用Post請求的方法。分享給大家供大家參考。具體如下:
一、需要用到的場景
在jQuery中使用$.post()就可以方便的發(fā)起一個post請求,在android程序中有時也要從服務(wù)器獲取一些數(shù)據(jù),就也必須得使用post請求了。
二、需要用到的主要類
在android中使用post請求主要要用到的類是HttpPost、HttpResponse、EntityUtils
三、主要思路
1、創(chuàng)建HttpPost實例,設(shè)置需要請求服務(wù)器的url。
2、為創(chuàng)建的HttpPost實例設(shè)置參數(shù),參數(shù)設(shè)置時使用鍵值對的方式用到NameValuePair類。
3、發(fā)起post請求獲取返回實例HttpResponse
4、使用EntityUtils對返回值的實體進行處理(可以取得返回的字符串,也可以取得返回的byte數(shù)組)
代碼也比較簡單,注釋也加上了,就直接貼出來了
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對象 HttpPost httpPost = new HttpPost(url); // 設(shè)置HTTP POST請求參數(shù)必須用NameValuePair對象 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請求參數(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 進行post請求:
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());
希望本文所述對大家的Android程序設(shè)計有所幫助。
相關(guān)文章
Android?Activity?View加載與繪制流程深入刨析源碼
這篇文章主要介紹了Android?Activity?View的加載與繪制流程源碼分析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08Android?ViewPager?+?Fragment實現(xiàn)滑動頁面效果
本文通過實例代碼較詳細的給大家介紹了Android?ViewPager?+?Fragment實現(xiàn)滑動頁面效果,需要的朋友可以參考下2018-06-06Android組件實現(xiàn)長按彈出上下文菜單功能的方法
這篇文章主要介紹了Android組件實現(xiàn)長按彈出上下文菜單功能的方法,結(jié)合實例形式分析了Android實現(xiàn)長按彈出上下文菜單的具體步驟與相關(guān)操作技巧,需要的朋友可以參考下2017-07-07Android用注解與反射實現(xiàn)Butterknife功能
Butterknife是一個在android上實現(xiàn)ioc(控制反轉(zhuǎn))的一個庫。ioc的核心是解耦。解耦的目的是修改耦合對象時不影響另外一個對象,降低模塊之間的關(guān)聯(lián)。在Spring中ioc更多的是依靠xml的配置。而android上的IOC框架可以不使用xml配置2022-11-11Android解決getExternalStorageDirectory在29后廢棄問題(推薦)
這篇文章主要介紹了Android解決getExternalStorageDirectory在29后廢棄問題(推薦),本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-02-02將替代ListView的RecyclerView 的使用詳解(一)
這篇文章主要介紹了將替代ListView的RecyclerView 的使用詳解(一)的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-07-07