三种POST和GET的提交方式.docx

上传人:小飞机 文档编号:3204631 上传时间:2023-03-11 格式:DOCX 页数:11 大小:39.03KB
返回 下载 相关 举报
三种POST和GET的提交方式.docx_第1页
第1页 / 共11页
三种POST和GET的提交方式.docx_第2页
第2页 / 共11页
三种POST和GET的提交方式.docx_第3页
第3页 / 共11页
三种POST和GET的提交方式.docx_第4页
第4页 / 共11页
三种POST和GET的提交方式.docx_第5页
第5页 / 共11页
亲,该文档总共11页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《三种POST和GET的提交方式.docx》由会员分享,可在线阅读,更多相关《三种POST和GET的提交方式.docx(11页珍藏版)》请在三一办公上搜索。

1、三种POST和GET的提交方式三种POST和GET的提交方式 向服务器提交数据有两种方式,post和get。两者的区别主要有三点,安全性、长度限制、数据结构。其中get请求安全性相比较而言较差,数据长度受浏览器地址栏限制,没有方法体。两种都是较为重要的数据提交方式。现简单介绍一下三种post和get的提交方式。无论是哪种方法实现post和get,get 的访问路径都要携带数据,而post提交是把数据放在方法体中。 普通方法实现get/post提交: 严格遵照Http协议进行数据传输。在安卓开发环境下,由于主线程不能进行网络访问,因此需要在开启一个子线程向服务器提交数据。为了更加直观的观察数据,

2、可以在程序屏幕上显示服务器反馈信息。又由于子线程无法更改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.ge

3、tText.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 = (Ht

4、tpURLConnection) 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.

5、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) f

6、inal 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 UR

7、L 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

8、); conn.setRequestProperty(Content-Length,String.valueOf(data.length); etDoOutput(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.wha

9、t = 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; 复制代码 用h

10、ttpclien实现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.execut

11、e(httpGet); 4. 得到HttpResponse对象获取状态行中的状态码,判断状态码状态码。 int code = response.getStatusLine.getStatusCode; 5. 同样用HttpResponse对象获取相应内容,存入流对象。最后将得到的流对象转为字符串进行显示。 InputStream is = response.getEntity.getContent; 其中要注意的一点是用post请求时要传递值所以要多出一个步骤。具体而言,先创建一个list集合,集合的泛型用NameValuePair表示,类似于键值对的形式存储要传递的数据。接着向集合中添加要提

12、交数据。最后用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 Strin

13、g 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) Inp

14、utStream 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-gener

15、ated 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)

16、| 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 parameter = new ArrayList; paramete

17、r.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.g

18、etContent; 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

19、; Message msg = Message.obtain; msg.what = ERROR2; handler.sendMessage(msg); .start; 复制代码 使用开源框架实现get/post提交: 利用框架实现get/post提交不需要再开启子线程。直接在主线程进行get/post的提交,大大的减少了工作量。但操作前需要导包。然后直接创建一个AsyncHttpClient对象,用AsyncHttpClient对象的post方法和get方法,发送请求,并在AsyncHttpResponseHandler对象中获得相应信息。同样若是post请求,仍旧需要传递值。这里可以用Re

20、questParams对象添加要传递的值。 在文件下添加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 Strin

21、g 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(ne

22、w 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.toS

23、tring.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

24、= 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); );

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 生活休闲 > 在线阅读


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号