网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

Android应用中如何利用get与post方式向服务器传递数据

这篇文章将为大家详细讲解有关Android应用中如何利用get与post方式向服务器传递数据,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

专业成都网站建设公司,做排名好的好网站,排在同行前面,为您带来客户和效益!创新互联为您提供成都网站建设,五站合一网站设计制作,服务好的网站设计公司,成都网站建设、做网站负责任的成都网站制作公司!

需要在布局文件中增加两个个EditText控件和两个登录的Button控件。其中一个Button是使用get方式提交数据,一个是使用post提交数据。

 
   
  

一、使用get方式提交数据

需要写一个异步任务类继承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提交数据的点击事件 
  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"); 
 
  } 
  //写一个异步任务类继承AsyncTask,重写它的两个方法 
  class myTask extends AsyncTask{ 
    @Override 
    protected Object doInBackground(Object[] params) { 
      //获取参数的值 
      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; 
 
      //判断提交数据的类型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方式提交数据

post和get一样需要写一个异步任务类继承AsyncTask,重写它的两个方法。但是post需要设置Content-Length、Content-Type以及对外输出数据。而且请求的路径不同,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提交数据的点击事件 
  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"); 
 
  } 
   
  //写一个异步任务类继承AsyncTask,重写它的两个方法 
  class myTask extends AsyncTask{ 
    @Override 
    protected Object doInBackground(Object[] params) { 
      //获取参数的值 
      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; 
 
      //判断提交数据的类型1、get 2、post 
      try { 
        if ("GET".equals(type)) {//用get方式提交 
          
        } else if ("POST".equals(type)) {//用post方式提交 
          url = new URL(path);//得到请求的路径 
          httpURLConnection = (HttpURLConnection) url.openConnection(); 
          //设置Content-Length Content-Type 
          httpURLConnection.setRequestProperty("Content-Length", s.length() + ""); 
          httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
          //设置允许对外输出数据 
          httpURLConnection.setDoOutput(true); 
          //将用户名密码以流的形式对外输出 
          httpURLConnection.getOutputStream().write(s.getBytes()); 
          httpURLConnection.setRequestMethod("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); 
    } 
  } 
} 

另外,需要增加网络权限:

 

三、总结

1、get方式与post方式请求的路径(URL地址)不同。

2、post方式需要对请求头的设置。

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

3、post方式需要请求内容,而get方式的相应内容在URL地址中。

4、post方式与get方式的请求时所携带的内容大小不同。

        get:1k。

        post:理论上是无限制的,相对于而言post适合传大量数据。

5、get方式的数据直接显示在URL地址中,这是不安全的;而post方式不会,安全性相对于比较高。

关于Android应用中如何利用get与post方式向服务器传递数据就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。


分享题目:Android应用中如何利用get与post方式向服务器传递数据
文章URL:http://bjjierui.cn/article/psoojg.html

其他资讯