欧美一级特黄大片做受成人-亚洲成人一区二区电影-激情熟女一区二区三区-日韩专区欧美专区国产专区

Android應(yīng)用中如何利用get與post方式向服務(wù)器傳遞數(shù)據(jù)

這篇文章將為大家詳細(xì)講解有關(guān)Android應(yīng)用中如何利用get與post方式向服務(wù)器傳遞數(shù)據(jù),文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。

專業(yè)成都網(wǎng)站建設(shè)公司,做排名好的好網(wǎng)站,排在同行前面,為您帶來客戶和效益!創(chuàng)新互聯(lián)為您提供成都網(wǎng)站建設(shè),五站合一網(wǎng)站設(shè)計制作,服務(wù)好的網(wǎng)站設(shè)計公司,成都網(wǎng)站建設(shè)、做網(wǎng)站負(fù)責(zé)任的成都網(wǎng)站制作公司!

需要在布局文件中增加兩個個EditText控件和兩個登錄的Button控件。其中一個Button是使用get方式提交數(shù)據(jù),一個是使用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ù)

需要寫一個異步任務(wù)類繼承AsyncTask,重寫它的兩個方法。代碼如下:

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ù)的點擊事件 
  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"); 
 
  } 
  //寫一個異步任務(wù)類繼承AsyncTask,重寫它的兩個方法 
  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 + "&#63;" + 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一樣需要寫一個異步任務(wù)類繼承AsyncTask,重寫它的兩個方法。但是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ù)的點擊事件 
  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"); 
 
  } 
   
  //寫一個異步任務(wù)類繼承AsyncTask,重寫它的兩個方法 
  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方式的請求時所攜帶的內(nèi)容大小不同。

        get:1k。

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

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

關(guān)于Android應(yīng)用中如何利用get與post方式向服務(wù)器傳遞數(shù)據(jù)就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

標(biāo)題名稱:Android應(yīng)用中如何利用get與post方式向服務(wù)器傳遞數(shù)據(jù)
當(dāng)前路徑:http://www.aaarwkj.com/article26/psoojg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站設(shè)計、App開發(fā)網(wǎng)站改版、Google、網(wǎng)站內(nèi)鏈、做網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站優(yōu)化排名
国产在线不卡中文字幕| 自拍偷拍欧美日韩第一页| 国产精品一区午夜福利| 高潮的毛片激情久久精品| 韩国福利短片在线观看| 丁香色婷婷国产精品视频| 久久国产精品亚洲熟女66r| 国产精品三级玖玖玖电影| 91人妻互换一区二区| 亚洲欧美日韩之国产综合| 日韩精品熟妻人女亚洲一区| 成人午夜在线三级内射| 亚洲精品在线免费av| 亚洲人色中文字幕天堂| 亚洲一区二区三区国色天香| 亚洲av中文久久精品国内| 欧美日本一区二区四区| 国产精品日产三级在线观看| 午夜福利片免费在线观看| 年轻的母亲韩国三级| 亚洲国产综合亚洲综合国产| 日韩欧美黄网站免费看| 日本人妻精品在线观看| 亚洲天堂男人的天堂狠狠操| 成人偷拍自拍在线视频| 国产综合一区在线观看97| 视频一区视频二区三区| 国产在线乱码一区二区| 精品国产乱码一区二区三区四区| 啄木乌法国一区二区三区| 国产男女在线视频观看| 九九久久精品久久久精品| 日韩精品伦理中文字幕| 少妇视频资源一区二区三区 | 日韩不卡的在线视频| 高清一区高清二区高清三区| 欧美日韩国产天天干| 婷婷网色偷偷亚洲男人| 久久96国产精品久久秘臀| 亚洲精品成人免费电影| 日韩午夜电影一区二区三区|