当前位置: 移动技术网 > 移动技术>移动开发>Android > 详解Android:向服务器提供数据之get、post方式

详解Android:向服务器提供数据之get、post方式

2019年07月24日  | 移动技术网移动技术  | 我要评论

在这我们首先了解android客户端向服务器提交数据的底层做法。get、post两种方法提交数据,下面我们用示例了解get以及post方式。

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

<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方式提交数据

需要写一个异步任务类继承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); 
    } 
  } 
} 

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

<uses-permission android:name="android.permission.internet" /> 

三、总结

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方式不会,安全性相对于比较高。

 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网