Android中post和get的提交方式【三种】
向服务器提交数据有两种方式,post和get。两者的区别主要有三点,安全性、长度限制、数据结构。其中get请求安全性相比较而言较差,数据长度受浏览器地址栏限制,没有方法体。两种都是较为重要的数据提交方式。现简单介绍一下三种post和get的提交方式。无论是哪种方法实现post和get,get 的访问路径都要携带数据,而post提交是把数据放在方法体中。
普通方法实现get/post提交:
严格遵照http协议进行数据传输。在安卓开发环境下,由于主线程不能进行网络访问,因此需要在开启一个子线程向服务器提交数据。为了更加直观的观察数据,可以在程序屏幕上显示服务器反馈信息。又由于子线程无法更改ui界面,因此需要引入hnndler代理器。实现get/post提交基本步骤就是,获取url路径,根据路径得到http连接,用httpurlconnection对象设置相关的http配置信息、提交方式以及获取反馈码。当响应码为200时表示提交成功,可以通过httpurlconnection以流的形式获取反馈信息。
普通grt提交方式:
public void load(view view){ final string qq = et_qq.gettext().tostring().trim(); final string pwd = et_pwd.gettext().tostring().trim(); if (textutils.isempty(qq) || textutils.isempty(pwd)) { toast.maketext(mainactivity.this, "qq号或密码为空", 0).show(); return; } final string path = "http://192.168.1.114:8080/qqload/qqload?qq=" + qq + "&pwd=" + pwd; new thread() { public void run() { try { url url = new url(path); httpurlconnection conn = (httpurlconnection) url .openconnection(); conn.setrequestmethod("get"); conn.setreadtimeout(5000); int code = conn.getresponsecode(); if (code == 200) { inputstream is = conn.getinputstream(); string result = streamtools.readstream(is); message msg = message.obtain(); msg.what = success; msg.obj = result; handler.sendmessage(msg); } else { message msg = message.obtain(); msg.what = error1; handler.sendmessage(msg); } } catch (exception e) { // todo auto-generated catch block e.printstacktrace(); message msg = message.obtain(); msg.what = error2; handler.sendmessage(msg); } } }.start(); }
普通post提交方式:
public void load(view view){ final string qq = et_qq.gettext().tostring().trim(); final string pwd = et_pwd.gettext().tostring().trim(); if (textutils.isempty(qq) || textutils.isempty(pwd)) { toast.maketext(mainactivity.this, "qq号或密码为空", 0).show(); return; } final string path = "http://192.168.1.114:8080/qqload/qqload"; new thread() { public void run() { try { url url = new url(path); httpurlconnection conn = (httpurlconnection) url .openconnection(); conn.setrequestmethod("post"); conn.setreadtimeout(5000); conn.setrequestproperty("content-type", "application/x-www-form-urlencoded"); string data = "qq="+urlencoder.encode(qq,"utf-8")+"&pwd=" + urlencoder.encode(pwd,"utf-8"); conn.setrequestproperty("content-length",string.valueof(data.length())); conn.setdooutput(true); conn.getoutputstream().write(data.getbytes()); int code = conn.getresponsecode(); if (code == 200) { inputstream is = conn.getinputstream(); string result = streamtools.readstream(is); message msg = message.obtain(); msg.what = success; msg.obj = result; handler.sendmessage(msg); } else { message msg = message.obtain(); msg.what = error1; handler.sendmessage(msg); } } catch (exception e) { // todo auto-generated catch block e.printstacktrace(); message msg = message.obtain(); msg.what = error2; handler.sendmessage(msg); } } }.start(); }
用httpclien实现get/post提交的只需要一下几个步骤:
1.创建httpclient对象,实现打开浏览器的功能
httpclient client = new defaulthttpclient();
2.输入地址或者数据 ,用到httpget()或httppost(),传入要访问的路径,得到httpget或httppost对象。
httpget httpget = new httpget(path);
3.把获得的httpget或httppost对象发送到服务器,实现敲回车的功能,得到httpresponse对象。
httpresponse response = client.execute(httpget);
4.得到httpresponse对象获取状态行中的状态码,判断状态码状态码。
int code = response.getstatusline().getstatuscode();
5.同样用httpresponse对象获取相应内容,存入流对象。最后将得到的流对象转为字符串进行显示。
inputstream is = response.getentity().getcontent();
其中要注意的一点是用post请求时要传递值所以要多出一个步骤。具体而言,先创建一个list集合,集合的泛型用namevaluepair表示,类似于键值对的形式存储要传递的数据。接着向集合中添加要提交数据。最后用httppost对象把集合存入请求体中。
用httpclient实现get提交:
public void load(view view){ final string qq = et_qq.gettext().tostring().trim(); final string pwd = et_pwd.gettext().tostring().trim(); if (textutils.isempty(qq) || textutils.isempty(pwd)) { toast.maketext(mainactivity.this, "qq号或密码为空", 0).show(); return; } final string path = "http://192.168.1.114:8080/qqload/qqload?qq=" + qq + "&pwd=" + pwd; new thread() { public void run() { try { httpclient client = new defaulthttpclient(); httpget httpget = new httpget(path); httpresponse response = client.execute(httpget); int code = response.getstatusline().getstatuscode(); if (code == 200) { inputstream is = response.getentity().getcontent(); string result = streamtools.readstream(is); message msg = message.obtain(); msg.what = success; msg.obj = result; handler.sendmessage(msg); } else { message msg = message.obtain(); msg.what = error1; handler.sendmessage(msg); } } catch (exception e) { // todo auto-generated catch block e.printstacktrace(); message msg = message.obtain(); msg.what = error2; handler.sendmessage(msg); } } }.start(); }
用httpclient实现post提交:
public void load(view view){ final string qq = et_qq.gettext().tostring().trim(); final string pwd = et_pwd.gettext().tostring().trim(); if (textutils.isempty(qq) || textutils.isempty(pwd)) { toast.maketext(mainactivity.this, "qq号或密码为空", 0).show(); return; } final string path = "http://192.168.1.114:8080/qqload/qqload"; new thread() { public void run() { try { httpclient client = new defaulthttpclient(); httppost httppost = new httppost(path); list<namevaluepair> parameter = new arraylist(); parameter.add(new basicnamevaluepair("qq", qq)); parameter.add(new basicnamevaluepair("pwd", pwd)); httppost.setentity(new urlencodedformentity(parameter,"utf-8")); httpresponse response = client.execute(httppost); int code = response.getstatusline().getstatuscode(); if (code == 200) { inputstream is = response.getentity().getcontent(); string result = streamtools.readstream(is); message msg = message.obtain(); msg.what = success; msg.obj = result; handler.sendmessage(msg); } else { message msg = message.obtain(); msg.what = error1; handler.sendmessage(msg); } } catch (exception e) { // todo auto-generated catch block e.printstacktrace(); message msg = message.obtain(); msg.what = error2; handler.sendmessage(msg); } } }.start(); }
使用开源框架实现get/post提交:
利用框架实现get/post提交不需要再开启子线程。直接在主线程进行get/post的提交,大大的减少了工作量。但操作前需要导包。然后直接创建一个asynchttpclient对象,用asynchttpclient对象的post方法和get方法,发送请求,并在asynchttpresponsehandler()对象中获得相应信息。同样若是post请求,仍旧需要传递值。这里可以用requestparams对象添加要传递的值。
在文件下添加jar包:
用开源框架实现get提交:
public void load(view view){ final string qq = et_qq.gettext().tostring().trim(); final string pwd = et_pwd.gettext().tostring().trim(); if (textutils.isempty(qq) || textutils.isempty(pwd)) { toast.maketext(mainactivity.this, "qq号或密码为空", 0).show(); return; } final string path = "http://192.168.1.114:8080/qqload/qqload?qq=" + qq + "&pwd=" + pwd; asynchttpclient client = new asynchttpclient(); client.get(path, new asynchttpresponsehandler() { @override public void onsuccess(int statuscode, header[] headers, byte[] responsebody) { // todo auto-generated method stub tv_result.settext(new string(responsebody)); } @override public void onfailure(int statuscode, header[] headers, byte[] responsebody, throwable error) { // todo auto-generated method stub tv_result.settext("错误原因:" + new string(responsebody)); } }); }
用开源框架实现post请求:
public void load(view view){ final string qq = et_qq.gettext().tostring().trim(); final string pwd = et_pwd.gettext().tostring().trim(); if (textutils.isempty(qq) || textutils.isempty(pwd)) { toast.maketext(mainactivity.this, "qq号或密码为空", 0).show(); return; } final string path = "http://192.168.1.114:8080/qqload/qqload"; asynchttpclient client = new asynchttpclient(); requestparams params = new requestparams(); params.add("qq", qq); params.add("pwd", pwd); client.post(path,params,new asynchttpresponsehandler() { @override public void onsuccess(int statuscode, header[] headers, byte[] responsebody) { // todo auto-generated method stub tv_result.settext(new string(responsebody)); } @override public void onfailure(int statuscode, header[] headers, byte[] responsebody, throwable error) { // todo auto-generated method stub tv_result.settext(new string(responsebody)); } }); }
通过以上任何一种方式可以实现的功能是,从安卓手机端提交数据到服务器端,服务器端进行判断,并返回相应的结果。三种方式各有利弊,实现效果相同,在实际的使用过程中可以根据本身的需要进行选择。
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持!