詳解Android:向服務(wù)器提供數(shù)據(jù)之get、post方式
在這我們首先了解Android客戶(hù)端向服務(wù)器提交數(shù)據(jù)的底層做法。get、post兩種方法提交數(shù)據(jù),下面我們用示例了解get以及post方式。
需要在布局文件中增加兩個(gè)個(gè)EditText控件和兩個(gè)登錄的Button控件。其中一個(gè)Button是使用get方式提交數(shù)據(jù),一個(gè)是使用post提交數(shù)據(jù)。
<EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/et_main_name" android:hint="請(qǐng)輸入用戶(hù)名" /> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/et_main_pwd" android:hint="請(qǐng)輸入用戶(hù)名" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="登錄GET" android:onClick="getdata" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="登錄POST" android:onClick="postdata" />
一、使用get方式提交數(shù)據(jù)
需要寫(xiě)一個(gè)異步任務(wù)類(lèi)繼承AsyncTask,重寫(xiě)它的兩個(gè)方法。代碼如下:
public class MainActivity extends AppCompatActivity { private EditText et_main_name; private EditText et_main_pwd; private HttpURLConnection httpURLConnection; private URL url; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et_main_name = (EditText) findViewById(R.id.et_main_name); et_main_pwd = (EditText) findViewById(R.id.et_main_pwd); } //獲取GET提交數(shù)據(jù)的點(diǎn)擊事件 public void getdata(View view){ String name=et_main_name.getText().toString(); String pwds=et_main_pwd.getText().toString(); String path="http://192.168.43.143:8080/login/login.xhtml"; new myTask().execute(name,pwds,path,"GET"); } //寫(xiě)一個(gè)異步任務(wù)類(lèi)繼承AsyncTask,重寫(xiě)它的兩個(gè)方法 class myTask extends AsyncTask{ @Override protected Object doInBackground(Object[] params) { //獲取參數(shù)的值 String name = params[0].toString(); String pwds = params[1].toString(); String path = params[2].toString(); String type = params[3].toString(); String s = "uname=" + name + "&pwd=" + pwds; //判斷提交數(shù)據(jù)的類(lèi)型1、get 2、post try { if ("GET".equals(type)) {//用get方式提交 path = path + "?" + s; url = new URL(path); httpURLConnection = (HttpURLConnection) url.openConnection(); httpURLConnection.setRequestMethod("GET"); } else if ("POST".equals(type)) {//用post方式提交 } httpURLConnection.setConnectTimeout(5000); if (httpURLConnection.getResponseCode() == 200) { InputStream inputStream = httpURLConnection.getInputStream(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); String result = bufferedReader.readLine(); return result; } } catch (MalformedURLException e) { e.printStackTrace(); }catch (ProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } @Override protected void onPostExecute(Object o) { String str= (String) o; Toast.makeText(MainActivity.this,str,Toast.LENGTH_SHORT).show(); super.onPostExecute(o); } } }
二、使用post方式提交數(shù)據(jù)
post和get一樣需要寫(xiě)一個(gè)異步任務(wù)類(lèi)繼承AsyncTask,重寫(xiě)它的兩個(gè)方法。但是post需要設(shè)置Content-Length、Content-Type以及對(duì)外輸出數(shù)據(jù)。而且請(qǐng)求的路徑不同,post相對(duì)于比get安全。代碼如下:
public class MainActivity extends AppCompatActivity { private EditText et_main_name; private EditText et_main_pwd; private HttpURLConnection httpURLConnection; private URL url; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et_main_name = (EditText) findViewById(R.id.et_main_name); et_main_pwd = (EditText) findViewById(R.id.et_main_pwd); } //獲取POST提交數(shù)據(jù)的點(diǎn)擊事件 public void postdata(View view){ String name=et_main_name.getText().toString(); String pwds=et_main_pwd.getText().toString(); String path="http://192.168.43.143:8080/login/login.xhtml"; new myTask().execute(name,pwds,path,"POST"); } //寫(xiě)一個(gè)異步任務(wù)類(lèi)繼承AsyncTask,重寫(xiě)它的兩個(gè)方法 class myTask extends AsyncTask{ @Override protected Object doInBackground(Object[] params) { //獲取參數(shù)的值 String name = params[0].toString(); String pwds = params[1].toString(); String path = params[2].toString(); String type = params[3].toString(); String s = "uname=" + name + "&pwd=" + pwds; //判斷提交數(shù)據(jù)的類(lèi)型1、get 2、post try { if ("GET".equals(type)) {//用get方式提交 } else if ("POST".equals(type)) {//用post方式提交 url = new URL(path);//得到請(qǐng)求的路徑 httpURLConnection = (HttpURLConnection) url.openConnection(); //設(shè)置Content-Length Content-Type httpURLConnection.setRequestProperty("Content-Length", s.length() + ""); httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); //設(shè)置允許對(duì)外輸出數(shù)據(jù) httpURLConnection.setDoOutput(true); //將用戶(hù)名密碼以流的形式對(duì)外輸出 httpURLConnection.getOutputStream().write(s.getBytes()); httpURLConnection.setRequestMethod("POST");//數(shù)據(jù)提交的類(lèi)型 } httpURLConnection.setConnectTimeout(5000); if (httpURLConnection.getResponseCode() == 200) {//判斷響應(yīng)碼 InputStream inputStream = httpURLConnection.getInputStream(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); String result = bufferedReader.readLine(); return result; } } catch (MalformedURLException e) { e.printStackTrace(); }catch (ProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } @Override protected void onPostExecute(Object o) { String str= (String) o; Toast.makeText(MainActivity.this,str,Toast.LENGTH_SHORT).show(); super.onPostExecute(o); } } }
另外,需要增加網(wǎng)絡(luò)權(quán)限:
<uses-permission android:name="android.permission.INTERNET" />
三、總結(jié)
1、get方式與post方式請(qǐng)求的路徑(URL地址)不同。
2、post方式需要對(duì)請(qǐng)求頭的設(shè)置。
//設(shè)置Content-Length Content-Type httpURLConnection.setRequestProperty("Content-Length", s.length() + ""); httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
3、post方式需要請(qǐng)求內(nèi)容,而get方式的相應(yīng)內(nèi)容在URL地址中。
4、post方式與get方式的請(qǐng)求時(shí)所攜帶的內(nèi)容大小不同。
get:1k。
post:理論上是無(wú)限制的,相對(duì)于而言post適合傳大量數(shù)據(jù)。
5、get方式的數(shù)據(jù)直接顯示在URL地址中,這是不安全的;而post方式不會(huì),安全性相對(duì)于比較高。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- android AsynTask處理返回?cái)?shù)據(jù)和AsynTask使用get,post請(qǐng)求
- Android中post和get的提交方式【三種】
- Android中使用OkHttp包處理HTTP的get和post請(qǐng)求的方法
- Android中使用HttpURLConnection實(shí)現(xiàn)GET POST JSON數(shù)據(jù)與下載圖片
- android平臺(tái)HttpGet、HttpPost請(qǐng)求實(shí)例
- android使用url connection示例(get和post數(shù)據(jù)獲取返回?cái)?shù)據(jù))
- Android發(fā)送GET與POST請(qǐng)求的DEMO詳解
- android之HttpPost&HttpGet使用方法介紹
- Android HttpClient GET或者POST請(qǐng)求基本使用方法
- 安卓GET與POST網(wǎng)絡(luò)請(qǐng)求的三種方式
相關(guān)文章
Android 百度地圖定位實(shí)現(xiàn)仿釘釘簽到打卡功能的完整代碼
這篇文章主要介紹了Android 百度地圖定位實(shí)現(xiàn)仿釘釘簽到打卡功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04Android Build類(lèi)的詳解及簡(jiǎn)單實(shí)例
這篇文章主要介紹了Android Build類(lèi)的詳解及簡(jiǎn)單實(shí)例的相關(guān)資料,希望通過(guò)本文大家能夠理解掌握這部分內(nèi)容,需要的朋友可以參考下2017-08-08Android操作存放在assets文件夾下SQLite數(shù)據(jù)庫(kù)的方法
這篇文章主要介紹了Android操作存放在assets文件夾下SQLite數(shù)據(jù)庫(kù)的方法,實(shí)例分析了Android操作SQLite數(shù)據(jù)庫(kù)的相關(guān)技巧,需要的朋友可以參考下2015-06-06Flutter開(kāi)發(fā)之對(duì)角棋游戲?qū)崿F(xiàn)實(shí)例詳解
這篇文章主要為大家介紹了Flutter開(kāi)發(fā)之對(duì)角棋游戲?qū)崿F(xiàn)實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11Android編程實(shí)現(xiàn)XML解析與保存的三種方法詳解
這篇文章主要介紹了Android編程實(shí)現(xiàn)XML解析與保存的三種方法,結(jié)合實(shí)例形式詳細(xì)分析了Android實(shí)現(xiàn)xml解析的SAX、DOM、PULL三種方法的相關(guān)操作技巧,需要的朋友可以參考下2017-08-08如何玩轉(zhuǎn)Android矢量圖VectorDrawable
這篇文章主要教大家如何玩轉(zhuǎn)Android矢量圖VectorDrawable,對(duì)矢量圖感興趣的小伙伴們可以參考一下2016-05-05Android編程開(kāi)發(fā)之EditText中inputType屬性小結(jié)
這篇文章主要介紹了Android編程開(kāi)發(fā)之EditText中inputType屬性用法,分析說(shuō)明了Android中EditText的inputType屬性具體含義與使用技巧,需要的朋友可以參考下2016-01-01Android應(yīng)用開(kāi)發(fā)工程目錄作用介紹
這篇文章主要介紹了Android應(yīng)用開(kāi)發(fā)工程目錄作用介紹,本文對(duì)Android項(xiàng)目中的每個(gè)目錄的作用作了總結(jié),需要的朋友可以參考下2014-10-10Android Studio中導(dǎo)入JNI生成的.so庫(kù)的實(shí)現(xiàn)方法
這篇文章主要介紹了Android Studio中導(dǎo)入JNI生成的.so庫(kù)的實(shí)現(xiàn)方法的相關(guān)資料,這里不僅提供實(shí)現(xiàn)方案并提供了實(shí)現(xiàn)的方法,需要的朋友可以參考下2017-07-07Android?Framework原理Binder驅(qū)動(dòng)源碼解析
這篇文章主要為大家介紹了Android?Framework原理Binder驅(qū)動(dòng)源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01