详解Android:向服务器提供数据之get、post方式
在这我们首先了解Android客户端向服务器提交数据的底层做法。get、post两种方法提交数据,下面我们用示例了解get以及post方式。
需要在布局文件中增加两个个EditText控件和两个登录的Button控件。其中一个Button是使用get方式提交数据,一个是使用post提交数据。
一、使用get方式提交数据
需要写一个异步任务类继承AsyncTask,重写它的两个方法。代码如下:
publicclassMainActivityextendsAppCompatActivity{ privateEditTextet_main_name; privateEditTextet_main_pwd; privateHttpURLConnectionhttpURLConnection; privateURLurl; @Override protectedvoidonCreate(BundlesavedInstanceState){ 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提交数据的点击事件 publicvoidgetdata(Viewview){ Stringname=et_main_name.getText().toString(); Stringpwds=et_main_pwd.getText().toString(); Stringpath="http://192.168.43.143:8080/login/login.xhtml"; newmyTask().execute(name,pwds,path,"GET"); } //写一个异步任务类继承AsyncTask,重写它的两个方法 classmyTaskextendsAsyncTask{ @Override protectedObjectdoInBackground(Object[]params){ //获取参数的值 Stringname=params[0].toString(); Stringpwds=params[1].toString(); Stringpath=params[2].toString(); Stringtype=params[3].toString(); Strings="uname="+name+"&pwd="+pwds; //判断提交数据的类型1、get2、post try{ if("GET".equals(type)){//用get方式提交 path=path+"?"+s; url=newURL(path); httpURLConnection=(HttpURLConnection)url.openConnection(); httpURLConnection.setRequestMethod("GET"); }elseif("POST".equals(type)){//用post方式提交 } httpURLConnection.setConnectTimeout(5000); if(httpURLConnection.getResponseCode()==200){ InputStreaminputStream=httpURLConnection.getInputStream(); BufferedReaderbufferedReader=newBufferedReader(newInputStreamReader(inputStream)); Stringresult=bufferedReader.readLine(); returnresult; } }catch(MalformedURLExceptione){ e.printStackTrace(); }catch(ProtocolExceptione){ e.printStackTrace(); }catch(IOExceptione){ e.printStackTrace(); } returnnull; } @Override protectedvoidonPostExecute(Objecto){ Stringstr=(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安全。代码如下:
publicclassMainActivityextendsAppCompatActivity{ privateEditTextet_main_name; privateEditTextet_main_pwd; privateHttpURLConnectionhttpURLConnection; privateURLurl; @Override protectedvoidonCreate(BundlesavedInstanceState){ 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提交数据的点击事件 publicvoidpostdata(Viewview){ Stringname=et_main_name.getText().toString(); Stringpwds=et_main_pwd.getText().toString(); Stringpath="http://192.168.43.143:8080/login/login.xhtml"; newmyTask().execute(name,pwds,path,"POST"); } //写一个异步任务类继承AsyncTask,重写它的两个方法 classmyTaskextendsAsyncTask{ @Override protectedObjectdoInBackground(Object[]params){ //获取参数的值 Stringname=params[0].toString(); Stringpwds=params[1].toString(); Stringpath=params[2].toString(); Stringtype=params[3].toString(); Strings="uname="+name+"&pwd="+pwds; //判断提交数据的类型1、get2、post try{ if("GET".equals(type)){//用get方式提交 }elseif("POST".equals(type)){//用post方式提交 url=newURL(path);//得到请求的路径 httpURLConnection=(HttpURLConnection)url.openConnection(); //设置Content-LengthContent-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){//判断响应码 InputStreaminputStream=httpURLConnection.getInputStream(); BufferedReaderbufferedReader=newBufferedReader(newInputStreamReader(inputStream)); Stringresult=bufferedReader.readLine(); returnresult; } }catch(MalformedURLExceptione){ e.printStackTrace(); }catch(ProtocolExceptione){ e.printStackTrace(); }catch(IOExceptione){ e.printStackTrace(); } returnnull; } @Override protectedvoidonPostExecute(Objecto){ Stringstr=(String)o; Toast.makeText(MainActivity.this,str,Toast.LENGTH_SHORT).show(); super.onPostExecute(o); } } }
另外,需要增加网络权限:
三、总结
1、get方式与post方式请求的路径(URL地址)不同。
2、post方式需要对请求头的设置。
//设置Content-LengthContent-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方式不会,安全性相对于比较高。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。