android端向服务器提交请求的几种方式
1、GET方式
其实GET方式说白了,就是拼接字符串。。最后拼成的字符串的格式是: path ? username= ....& password= ......
public boolean loginByGet(String path, String username , String password) throws Exception{
String url_path = path +"?username=" + URLEncoder.encode(username, "utf-8") + "&password="+password;
URL url = new URL(url_path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5000);
if(conn.getResponseCode() == 200){
return true;
}
return false;
}
2、POST方式
post方式中,一定要设置以下两个请求参数
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length", entity.length + "");
完整代码如下:
public boolean loginByPost(String path,String username , String password) throws Exception{
System.out.println("LoginService的loginByPost()");
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setConnectTimeout(5000);
String value = "username=" + username +"&" + "password=" +password;
byte[] entity = value.getBytes();
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length", entity.length + "");
conn.setDoOutput(true);
OutputStream os = conn.getOutputStream();
os.write(entity);
if(conn.getResponseCode() == 200){
return true;
}
return false;
}
3、通过HttpClient以GET方式提交请求
使用HttpClient这个第三方框架以后,我们在编写代码的时候就看不到URL、conn之类的。他其实就是对Http协议进行了封装
public boolean loginByHttpClientGet(String path,String username , String password) throws Exception{
String value = path + "?username=" + username +"&password=" + password;
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(value);
HttpResponse httpResponse = httpClient.execute(httpGet);
if(httpResponse.getStatusLine().getStatusCode() == 200){
return true;
}
return false;
}
4、通过HttpClient以POST方式提交请求
public boolean loginByHttpClientPost(String path,String username , String password)throws Exception{
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(path);
List<NameValuePair> parameters = new ArrayList<NameValuePair>();
parameters.add(new BasicNameValuePair("username", username));
parameters.add(new BasicNameValuePair("password", password));
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(parameters,"utf-8");
httpPost.setEntity(entity);
HttpResponse httpResponse = httpClient.execute(httpPost);
if(httpResponse.getStatusLine().getStatusCode() == 200){
return true;
}
return false;
}
android端向服务器提交请求的几种方式的更多相关文章
- android端从服务器抓取的几种常见的数据的处理方式
1.图片 public void look(View v) { String path = et_path.getText().toString(); try { URL url = new URL( ...
- android 向serverGet和Post请求的两种方式,android向server发送文件,自己组装协议和借助第三方开源
一个适用于Android平台的第三方和apache的非常多东西类似,仅仅是用于Android上 我在项目里用的是这个 https://github.com/loopj/android-async-ht ...
- android中用get和post方式向服务器提交请求
通过get和post方式向服务器发送请求首先说一下get和post的区别get请求方式是将提交的参数拼接在url地址后面,例如http://www.baidu.com/index.jsp?num=23 ...
- android客户端向服务器发送请求中文乱码的问
android客户端向服务器发送请求的时候,并将参数保存到数据库时遇到了中文乱码的问题: 解决方法: url = "http://xxxx.com/Orders/saveorder.html ...
- 讨论HTTP POST 提交数据的几种方式
转自:http://www.cnblogs.com/softidea/p/5745369.html HTTP/1.1 协议规定的 HTTP 请求方法有 OPTIONS.GET.HEAD.POST.PU ...
- C#中Post请求的两种方式发送参数链和Body的
POST请求 有两种方式 一种是组装key=value这种参数对的方式 一种是直接把一个字符串发送过去 作为body的方式 我们在postman中可以看到 sfdsafd sdfsdfds publi ...
- 第二节:SSL证书的申请、配置(IIS通用)及跳转Https请求的两种方式
一. 相关概念介绍 1. SSL证书服务 SSL证书服务由"服务商"联合多家国内外数字证书管理和颁发的权威机构.在xx云平台上直接提供的服务器数字证书.您可以在阿里云.腾讯云等平台 ...
- 怎样在Android开发中FPS游戏实现的两种方式比较
怎样在Android开发中FPS游戏实现的两种方式比较 如何用Android平台开发FPS游戏,其实现过程有哪些方法,这些方法又有哪些不同的地方呢?首先让我们先了解下什么是FPS 英文名:FPS (F ...
- [Android] Android ViewPager 中加载 Fragment的两种方式 方式(二)
接上文: https://www.cnblogs.com/wukong1688/p/10693338.html Android ViewPager 中加载 Fragmenet的两种方式 方式(一) 二 ...
随机推荐
- mysql事务和锁InnoDB(转)
背景 MySQL/InnoDB的加锁分析,一直是一个比较困难的话题.我在工作过程中,经常会有同事咨询这方面的问题.同时,微博上也经常会收到MySQL锁相关的私信,让我帮助解决一些死锁的问题.本文,准备 ...
- mysql basic operation,mysql总结
mysql> select * from wifi_data where dev_id like "0023-AABBCCCCBBAA" ; 1.显示数据库列表.show d ...
- Ch02 从零开始实例学习3
提纲:---------------------------- 演练2-3:添加控制器 知识点2-3:控制器的职责 知识点2-4:控制器的类别与方法 ------------------------- ...
- Qt中Ui名字空间以及setupUi函数的原理和实现
用最新的QtCreator选择GUI的应用会产生含有如下文件的工程 下面就简单分析下各部分的功能. .pro文件是供qmake使用的文件,不是本文的重点[不过其实也很简单的],在此不多赘述. 所以呢, ...
- One simple health check for oracle with sql
There are some sqls which is used for check the oracle database's health condition. ------numbers of ...
- Swift - 使用NSURLSession加载数据、下载、上传文件
NSURLSession类支持三种类型的任务:加载数据.下载和上传.下面通过样例分别进行介绍. 1,使用Data Task加载数据 使用全局的sharedSession()和dataTaskWithR ...
- 微软Ajax--UpdatePanel控件
今天用做日历显示本月的考勤记录,用到了UpdatePanel控件,才发现对这个控件并不太了解,所以找了点儿资料,整理了一下给大家发上来! 一.UpdatePanel的结构 <asp:Script ...
- Android KeyCode(官方)
Constants public static final int ACTION_DOWN Added in API level 1 getAction() value: the key has be ...
- javascript 判断IOS版本号
先来观察 iOS 的 User-Agent 串: iPhone 4.3.2 系统: Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_3_2 like Mac OS X; ...
- Windows 8 动手实验系列教程 实验6:设置和首选项
动手实验 实验6:设置和首选项 2012年9月 简介 实验3介绍了合约并演示了应用程序如何轻松地与共享和搜索合约实现集成.合约同样包含设置超级按钮,它对活动的Windows应用商店应用的设置进行修改. ...