博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
android中用get和post方式向服务器提交请求
阅读量:7188 次
发布时间:2019-06-29

本文共 3675 字,大约阅读时间需要 12 分钟。

通过get和post方式向服务器发送请求

首先说一下get和post的区别
get请求方式是将提交的参数拼接在url地址后面,例如http://www.baidu.com/index.jsp?num=23&jjj=888;
但是这种形式对于那种比较隐私的参数是不适合的,而且参数的大小也是有限制的,一般是1K左右吧,对于上传文件
就不是很适合。
post请求方式是将参数放在消息体内将其发送到服务器,所以对大小没有限制,对于隐私的内容也比较合适。
在android中用get方式向服务器提交请求:
在android模拟器中访问本机中的tomcat服务器时,注意:不能写localhost,因为模拟器是一个单独的手机系统,所以要写真是的IP地址。
否则无法访问到服务器。
//要访问的服务器地址,下面的代码是要向服务器提交用户名和密码,提交时中文先要经过URLEncoder编码,因为模拟器默认的编码格式是utf-8
//而tomcat内部默认的编码格式是ISO8859-1,所以先将参数进行编码,再向服务器提交。

private String address = "http://192.168.2.101:80/server/loginServlet";public boolean get(String username, String password) throws Exception {username = URLEncoder.encode(username); // 中文数据需要经过URL编码password = URLEncoder.encode(password);String params = "username=" + username + "&password=" + password;//将参数拼接在URl地址后面URL url = new URL(address + "?" + params);//通过url地址打开连接HttpURLConnection conn = (HttpURLConnection) url.openConnection();//设置超时时间conn.setConnectTimeout(3000);//设置请求方式conn.setRequestMethod("GET");//如果返回的状态码是200,则一切Ok,连接成功。return conn.getResponseCode() == 200;}//这种方式我平时喜欢用的方式    //获得要传递的数据    String username = et1.getText().toString();    String password = et2.getText().toString();        // 创建HttpGet对象    HttpGet request = new HttpGet(url +"name="+username+"&password="+password);    // 使用execute方法发送HTTP GET请求,并返回HttpResponse对象    // DefaultHttpClient为Http客户端管理类,负责发送请    HttpResponse response = httpClient.execute(request);    // 判断请求响应状态码,状态码为200表示服务端成功响应了客户端的请求    if (response.getStatusLine().getStatusCode() == 200) {     // 使用getEntity方法获得返回结果     String data = EntityUtils.toString(response.getEntity(),"gbk");     //获得Message对象     Message msg = handler.obtainMessage(1);    //创建Bundle对象     Bundle bundle = new Bundle();     //用mes传递数据     msg.setData(bundle);    //开启Message对象     msg.sendToTarget();     }   //用post得值public boolean post(String username, String password) throws Exception {username = URLEncoder.encode(username); // 中文数据需要经过URL编码password = URLEncoder.encode(password);String params = "username=" + username + "&password=" + password;byte[] data = params.getBytes();URL url = new URL(address);HttpURLConnection conn = (HttpURLConnection) url.openConnection();conn.setConnectTimeout(3000);//这是请求方式为POSTconn.setRequestMethod("POST");//设置post请求必要的请求头conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); // 请求头, 必须设置conn.setRequestProperty("Content-Length", data.length + ""); // 注意是字节长度, 不是字符长度conn.setDoOutput(true); // 准备写出conn.getOutputStream().write(data); // 写出数据return conn.getResponseCode() == 200;}//下面是我喜欢的方式    //把来传递的数据封装到user对象中    User user = new User();    user.setUserName(et1.getText().toString());    user.setUserPass(et2.getText().toString());    //创建Post对象    HttpPost request = new HttpPost("http://10.0.2.2:8080/system/Servlet");    // 将需要传递的参数封装到List
类型的对象中 List
params = new ArrayList
(); params.add(new BasicNameValuePair("username", user.getUserName())); params.add(new BasicNameValuePair("password", user.getUserPass())); // 将封装参数的对象存入request中,并设置编码方式 request.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8)); // DefaultHttpClient为Http客户端管理类,负责发送请求和接受响应 HttpResponse response = defaultHttpClient.execute(request); // 判断请求响应状态码,状态码为200表示服务端成功响应了客户端的请求 if (response.getStatusLine().getStatusCode() == 200){ // 使用getEntity方法获得返回结果 String data = EntityUtils.toString(response.getEntity(),"gdk"); //创建bundle对象 Bundle bundle = new Bundle(); //用bundle对象来封装data数据 bundle.putString("data", data); //创建Message对象 Message mes = handler.obtainMessage(1); //存储bundle数据 mes.setData(bundle); mes.sendToTarget();

 

转载地址:http://cjjkm.baihongyu.com/

你可能感兴趣的文章
bootstrap input 引入自带图标对不齐
查看>>
计算字符串中每种字母出现的次数
查看>>
让C#可以像Javascript一样操作Json
查看>>
POJ1270 Following Orders(拓扑排序+回溯)
查看>>
FPGA原理图设计---构架
查看>>
多通道 移位寄存器 verilog
查看>>
程序员最头疼的事:命名
查看>>
POJ 3617 Best Cow Line 贪心
查看>>
今天申请了blog很开心``开始我的blog生涯``
查看>>
Linux命令语句秘籍
查看>>
awk用法点滴
查看>>
解决JSP开发Web的中文问题
查看>>
UIscrollView 代理
查看>>
SM37 后台调试
查看>>
Linux内核设计第七周学习总结 Linux内核如何装载和启动一个可执行程序
查看>>
pta l2-4(这是二叉搜索树吗?)
查看>>
uboot分析之u-boot.lds
查看>>
js 遮罩层请稍后
查看>>
明日摘要
查看>>
web基础
查看>>