国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

詳解Android:向服務(wù)器提供數(shù)據(jù)之get、post方式

2019-12-12 03:32:13
字體:
供稿:網(wǎng)友

在這我們首先了解Android客戶端向服務(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="請輸入用戶名"     />   <EditText     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:id="@+id/et_main_pwd"     android:hint="請輸入用戶名"     />   <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ù)

需要寫一個(gè)異步任務(wù)類繼承AsyncTask,重寫它的兩個(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");    }   //寫一個(gè)異步任務(wù)類繼承AsyncTask,重寫它的兩個(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ù)的類型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一樣需要寫一個(gè)異步任務(wù)類繼承AsyncTask,重寫它的兩個(gè)方法。但是post需要設(shè)置Content-Length、Content-Type以及對外輸出數(shù)據(jù)。而且請求的路徑不同,post相對于比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");    }      //寫一個(gè)異步任務(wù)類繼承AsyncTask,重寫它的兩個(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ù)的類型1、get 2、post       try {         if ("GET".equals(type)) {//用get方式提交                   } else if ("POST".equals(type)) {//用post方式提交           url = new URL(path);//得到請求的路徑           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è)置允許對外輸出數(shù)據(jù)           httpURLConnection.setDoOutput(true);           //將用戶名密碼以流的形式對外輸出           httpURLConnection.getOutputStream().write(s.getBytes());           httpURLConnection.setRequestMethod("POST");//數(shù)據(jù)提交的類型         }         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方式請求的路徑(URL地址)不同。

2、post方式需要對請求頭的設(shè)置。

//設(shè)置Content-Length Content-Type httpURLConnection.setRequestProperty("Content-Length", s.length() + ""); httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 

3、post方式需要請求內(nèi)容,而get方式的相應(yīng)內(nèi)容在URL地址中。

4、post方式與get方式的請求時(shí)所攜帶的內(nèi)容大小不同。

        get:1k。

        post:理論上是無限制的,相對于而言post適合傳大量數(shù)據(jù)。

5、get方式的數(shù)據(jù)直接顯示在URL地址中,這是不安全的;而post方式不會(huì),安全性相對于比較高。

 以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 金塔县| 乐清市| 浪卡子县| 子长县| 虎林市| 哈巴河县| 南澳县| 竹山县| 临沧市| 平陆县| 安国市| 鄯善县| 阿勒泰市| 五台县| 论坛| 蕲春县| 舒城县| 苗栗市| 星座| 庐江县| 霍林郭勒市| 徐闻县| 桐庐县| 巴南区| 桓仁| 米林县| 准格尔旗| 邹平县| 南城县| 平潭县| 石渠县| 兴海县| 彭山县| 武平县| 开鲁县| 梅州市| 大英县| 遂昌县| 永泰县| 永修县| 南安市|