Android学习之Http使用Post方式进行数据提交(普通数据和Json数据)
转自:http://blog.csdn.net/wulianghuan/article/details/8626551
我们知道通过Get方式提交的数据是作为Url地址的一部分进行提交,而且对字节数的长度也有限制,与Get方式类似,http-post参数也是被URL编码的,然而它的变量名和变量值不作为URL的一部分被传送,而是放在实际的HTTP请求消息内部被传送。
可以通过如下的代码设置POST提交方式参数:
- HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
- urlConnection.setConnectTimeout(3000);
- urlConnection.setRequestMethod("POST"); //以post请求方式提交
- urlConnection.setDoInput(true); //读取数据
- urlConnection.setDoOutput(true); //向服务器写数据
- //获取上传信息的大小和长度
- byte[] myData = stringBuilder.toString().getBytes();
- //设置请求体的类型是文本类型,表示当前提交的是文本数据
- urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
- urlConnection.setRequestProperty("Content-Length", String.valueOf(myData.length));
这里使用一个案例来看一下如何使用post方式提交数据到服务器:
首先我们创建一个java project,只要创建一个类就行,我们创建一个HttpUtils.java类,
【代码如下】:
我们再创建一个服务端工程,一个web project,这里创建一个myhttp的工程,先给它创建一个servlet,用来接收参数访问。
创建的servlet配置如下:
- <servlet>
- <description>This is the description of my J2EE component</description>
- <display-name>This is the display name of my J2EE component</display-name>
- <servlet-name>LoginAction</servlet-name>
- <servlet-class>com.login.manager.LoginAction</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>LoginAction</servlet-name>
- <url-pattern>/servlet/LoginAction</url-pattern>
- </servlet-mapping>
建立的LoginAction.java类继承HttpServlet:
- package com.login.manager;
- import java.io.IOException;
- import java.io.PrintWriter;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- public class LoginAction extends HttpServlet {
- /**
- * Constructor of the object.
- */
- public LoginAction() {
- super();
- }
- /**
- * Destruction of the servlet. <br>
- */
- public void destroy() {
- super.destroy(); // Just puts "destroy" string in log
- // Put your code here
- }
- /**
- * The doGet method of the servlet. <br>
- *
- * This method is called when a form has its tag value method equals to get.
- *
- * @param request the request send by the client to the server
- * @param response the response send by the server to the client
- * @throws ServletException if an error occurred
- * @throws IOException if an error occurred
- */
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- this.doPost(request, response);
- }
- /**
- * The doPost method of the servlet. <br>
- *
- * This method is called when a form has its tag value method equals to post.
- *
- * @param request the request send by the client to the server
- * @param response the response send by the server to the client
- * @throws ServletException if an error occurred
- * @throws IOException if an error occurred
- */
- public void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- response.setContentType("text/html;charset=utf-8");
- request.setCharacterEncoding("utf-8");
- response.setCharacterEncoding("utf-8");
- PrintWriter out = response.getWriter();
- String userName = request.getParameter("username");
- String passWord = request.getParameter("password");
- String json = request.getParameter("json");
- System.out.println("userName:"+userName);
- System.out.println("passWord:"+passWord);
- System.out.println("json:"+json);
- if(userName.equals("admin") && passWord.equals("123456")){
- out.print("login successful!");
- }else{
- out.print("login failed");
- }
- out.flush();
- out.close();
- }
- /**
- * Initialization of the servlet. <br>
- *
- * @throws ServletException if an error occurs
- */
- public void init() throws ServletException {
- // Put your code here
- }
- }
我们运行java project,控制台输出如下:
>>>login successful!
Android学习之Http使用Post方式进行数据提交(普通数据和Json数据)的更多相关文章
- 【Android】Handler的应用(一):从服务器端加载JSON数据
最终目的 以JSON的形式,将数据存入服务器端. 在Android中,以Handler加载显示大批量文字. 在此以加载金庸小说<天龙八部(新修版)>为例(2580480 字节). 以tom ...
- 【Android学习】四种布局方式
一.LinearLayout 线性布局,即一行展开或者一列展开,也可以嵌套,需要注意的属性如下: android:orentation //对齐方式 二.FrameLayout 帧布局,即一层层叠起 ...
- Android学习笔记④——页面的布局方式
FrameLayout(帧布局) 这个布局的特点是简单的默认把每一个视图组件都放在边框内且放在左上角,即使添加多个视图组件,他们也都是重叠在左上角,新的视图会遮挡住旧的视图.可以根据gravity来改 ...
- Android学习之Service(1)--->Started方式
界面退出后进程程序还在运行,不会被杀死,如音乐播发器.后台下载等 注:本文只讨论Started方式 main.xml代码分析 <?xml version="1.0" enco ...
- 我有一壶酒 Android学习之Service(1)--->BinderService方式
本文只讨论扩展Binder类 创建一个Binder.xml <?xml version="1.0" encoding="utf-8"?> <L ...
- android 学习随笔十五(Activity的生命周期与摧毁时返回数据 )
1.Activity的生命周期 onCreate:创建时调用 onStart:在屏幕上可见,但是还没有获得焦点 onResume:可见并且获得焦点 onPause:可见,但是失去焦点 onStop:不 ...
- android 学习随笔十(网络:get、post提交数据)
1.get public class Tools { public static String getTextFromStream(InputStream is){ byte[] b = new by ...
- 学习Java 采取令牌的方式避免重复提交
重复提交原因 从提交页面到成功页面的跳转一般采用视图定位,由于视图定位是在服务端跳转的,如果用户在点击提交之后再次刷新页面,会导致重复提交,数据库的数据会有重复. 采用令牌措施 1.在转账展示页面生成 ...
- Android学习笔记之JSON数据解析
转载:Android学习笔记44:JSON数据解析 JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,采用完全独立于语言的文本格式,为Web应用开发提供了一种 ...
随机推荐
- Remove Duplicates from Sorted Array I&&II——怎样防超时
Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...
- IEEEXtreme 10.0 - Checkers Challenge
这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Checkers Challenge 题目来源 第10届IEEE极限编程大赛 https://www.hac ...
- [loj6038]「雅礼集训 2017 Day5」远行 lct+并查集
给你 n 个点,支持 m 次操作,每次为以下两种:连一条边,保证连完后是一棵树/森林:询问一个点能到达的最远的点与该点的距离.强制在线. n≤3×10^5 n≤3×10^5 ,m≤5×10^5 m≤5 ...
- scrapy 学习笔记1
最近一段时间开始研究爬虫,后续陆续更新学习笔记 爬虫,说白了就是获取一个网页的html页面,然后从里面获取你想要的东西,复杂一点的还有: 反爬技术(人家网页不让你爬,爬虫对服务器负载很大) 爬虫框架( ...
- MQTT协议以及库函数说明
内容简介:之前我们写了一篇ESP8266AT指令的说明文档,在客户端连接上服务器之后,接下来便是网络通信.为此我们又写了这一片MQTT协议的说明文档,更加深层次的讲述了通信的原理.此文档只是我们在调试 ...
- cordova 导致css中绝对定位top:0会被顶到视图之外
IOS7+ webview全屏导致状态栏悬浮在页面上 解决方案:打开 ios项目/classes/MainViewController.m,修改viewWillAppear方法 - (void)vie ...
- 自定义mvc验证特性,手机号号段老增加,给自定义一个RegularExpress
public class PhoneExpressionAttribute: RegularExpressionAttribute, IClientValidatable { public Phone ...
- ubuntu下 pycharm使用andcoda下的tensorflow
在ubuntu中 tensorflow是安装在andconda里的一个虚拟环境中,他就相当于一个容器,将tensorflow的整个环境去模拟隔离出来,因为我们直接使用andconda库去作为pycha ...
- CodeForces 811C Vladik and Memorable Trip
$dp$. 记录$dp[i]$表示以位置$i$为结尾的最大值. 枚举最后一段是哪一段,假设为$[j,i]$,那么可以用$max(dp[1]...dp[j-1]) + val[j][i]$去更新$dp[ ...
- 浅谈MVVM模式和MVP模式——Vue.js向
浅谈MVVM模式和MVP模式--Vue.js向 传统前端开发的MVP模式 MVP开发模式的理解过程 首先代码分为三层: model层(数据层), presenter层(控制层/业务逻辑相关) view ...