转自:http://blog.csdn.net/wulianghuan/article/details/8626551

我们知道通过Get方式提交的数据是作为Url地址的一部分进行提交,而且对字节数的长度也有限制,与Get方式类似,http-post参数也是被URL编码的,然而它的变量名和变量值不作为URL的一部分被传送,而是放在实际的HTTP请求消息内部被传送。

可以通过如下的代码设置POST提交方式参数:

  1. HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
  2. urlConnection.setConnectTimeout(3000);
  3. urlConnection.setRequestMethod("POST"); //以post请求方式提交
  4. urlConnection.setDoInput(true);     //读取数据
  5. urlConnection.setDoOutput(true);    //向服务器写数据
  6. //获取上传信息的大小和长度
  7. byte[] myData = stringBuilder.toString().getBytes();
  8. //设置请求体的类型是文本类型,表示当前提交的是文本数据
  9. urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
  10. urlConnection.setRequestProperty("Content-Length", String.valueOf(myData.length));

这里使用一个案例来看一下如何使用post方式提交数据到服务器:

首先我们创建一个java project,只要创建一个类就行,我们创建一个HttpUtils.java类,

【代码如下】:

我们再创建一个服务端工程,一个web project,这里创建一个myhttp的工程,先给它创建一个servlet,用来接收参数访问。

创建的servlet配置如下:

  1. <servlet>
  2. <description>This is the description of my J2EE component</description>
  3. <display-name>This is the display name of my J2EE component</display-name>
  4. <servlet-name>LoginAction</servlet-name>
  5. <servlet-class>com.login.manager.LoginAction</servlet-class>
  6. </servlet>
  7. <servlet-mapping>
  8. <servlet-name>LoginAction</servlet-name>
  9. <url-pattern>/servlet/LoginAction</url-pattern>
  10. </servlet-mapping>

建立的LoginAction.java类继承HttpServlet:

  1. package com.login.manager;
  2. import java.io.IOException;
  3. import java.io.PrintWriter;
  4. import javax.servlet.ServletException;
  5. import javax.servlet.http.HttpServlet;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8. public class LoginAction extends HttpServlet {
  9. /**
  10. * Constructor of the object.
  11. */
  12. public LoginAction() {
  13. super();
  14. }
  15. /**
  16. * Destruction of the servlet. <br>
  17. */
  18. public void destroy() {
  19. super.destroy(); // Just puts "destroy" string in log
  20. // Put your code here
  21. }
  22. /**
  23. * The doGet method of the servlet. <br>
  24. *
  25. * This method is called when a form has its tag value method equals to get.
  26. *
  27. * @param request the request send by the client to the server
  28. * @param response the response send by the server to the client
  29. * @throws ServletException if an error occurred
  30. * @throws IOException if an error occurred
  31. */
  32. public void doGet(HttpServletRequest request, HttpServletResponse response)
  33. throws ServletException, IOException {
  34. this.doPost(request, response);
  35. }
  36. /**
  37. * The doPost method of the servlet. <br>
  38. *
  39. * This method is called when a form has its tag value method equals to post.
  40. *
  41. * @param request the request send by the client to the server
  42. * @param response the response send by the server to the client
  43. * @throws ServletException if an error occurred
  44. * @throws IOException if an error occurred
  45. */
  46. public void doPost(HttpServletRequest request, HttpServletResponse response)
  47. throws ServletException, IOException {
  48. response.setContentType("text/html;charset=utf-8");
  49. request.setCharacterEncoding("utf-8");
  50. response.setCharacterEncoding("utf-8");
  51. PrintWriter out = response.getWriter();
  52. String userName = request.getParameter("username");
  53. String passWord = request.getParameter("password");
  54. String json = request.getParameter("json");
  55. System.out.println("userName:"+userName);
  56. System.out.println("passWord:"+passWord);
  57. System.out.println("json:"+json);
  58. if(userName.equals("admin") && passWord.equals("123456")){
  59. out.print("login successful!");
  60. }else{
  61. out.print("login failed");
  62. }
  63. out.flush();
  64. out.close();
  65. }
  66. /**
  67. * Initialization of the servlet. <br>
  68. *
  69. * @throws ServletException if an error occurs
  70. */
  71. public void init() throws ServletException {
  72. // Put your code here
  73. }
  74. }

我们运行java project,控制台输出如下:

>>>login successful!

Android学习之Http使用Post方式进行数据提交(普通数据和Json数据)的更多相关文章

  1. 【Android】Handler的应用(一):从服务器端加载JSON数据

    最终目的 以JSON的形式,将数据存入服务器端. 在Android中,以Handler加载显示大批量文字. 在此以加载金庸小说<天龙八部(新修版)>为例(2580480 字节). 以tom ...

  2. 【Android学习】四种布局方式

    一.LinearLayout 线性布局,即一行展开或者一列展开,也可以嵌套,需要注意的属性如下: android:orentation  //对齐方式 二.FrameLayout 帧布局,即一层层叠起 ...

  3. Android学习笔记④——页面的布局方式

    FrameLayout(帧布局) 这个布局的特点是简单的默认把每一个视图组件都放在边框内且放在左上角,即使添加多个视图组件,他们也都是重叠在左上角,新的视图会遮挡住旧的视图.可以根据gravity来改 ...

  4. Android学习之Service(1)--->Started方式

    界面退出后进程程序还在运行,不会被杀死,如音乐播发器.后台下载等 注:本文只讨论Started方式 main.xml代码分析 <?xml version="1.0" enco ...

  5. 我有一壶酒 Android学习之Service(1)--->BinderService方式

    本文只讨论扩展Binder类 创建一个Binder.xml <?xml version="1.0" encoding="utf-8"?> <L ...

  6. android 学习随笔十五(Activity的生命周期与摧毁时返回数据 )

    1.Activity的生命周期 onCreate:创建时调用 onStart:在屏幕上可见,但是还没有获得焦点 onResume:可见并且获得焦点 onPause:可见,但是失去焦点 onStop:不 ...

  7. android 学习随笔十(网络:get、post提交数据)

    1.get public class Tools { public static String getTextFromStream(InputStream is){ byte[] b = new by ...

  8. 学习Java 采取令牌的方式避免重复提交

    重复提交原因 从提交页面到成功页面的跳转一般采用视图定位,由于视图定位是在服务端跳转的,如果用户在点击提交之后再次刷新页面,会导致重复提交,数据库的数据会有重复. 采用令牌措施 1.在转账展示页面生成 ...

  9. Android学习笔记之JSON数据解析

    转载:Android学习笔记44:JSON数据解析 JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,采用完全独立于语言的文本格式,为Web应用开发提供了一种 ...

随机推荐

  1. iOS控制器与视图加载方法

    转载记录, 请看原文: 1. iOS中的各种加载方法(initWithNibName,loadNibNamed,initWithCoder,awakeFromNib等等)简单使用   http://w ...

  2. Zookeeper(二)Zookeeper原理与API应用

    一 Zookeeper概述 1.1 概述 Zookeeper是Google的Chubby一个开源的实现.它是一个针对大型分布式系统的可靠协调系统,提供的功能包括:配置维护.名字服务. 分布式同步.组服 ...

  3. 小甲鱼Python笔记(下)

    二十八 二十九  文件 打开文件 open(文件名[,模式][,缓冲]) 注意open是个函数不是方法 模式: 缓冲: 大于1的数字代表缓冲区的大小(单位是字节),-1(或者是任何负数)代表使用默认缓 ...

  4. 【LOJ】 #2013. 「SCOI2016」幸运数字

    题解 最大异或和,明显是个线性基 然而还有那么多路径--那就树分治,反正点数看起来很少,就是为了让人乘上一个60的常数嘛 把一个树的点分树记录下来,然后看看询问的两个点彼此相同的最后一个父亲是谁,把这 ...

  5. 16.Spark Streaming源码解读之数据清理机制解析

    原创文章,转载请注明:转载自 听风居士博客(http://www.cnblogs.com/zhouyf/) 本期内容: 一.Spark Streaming 数据清理总览 二.Spark Streami ...

  6. 《java虚拟机》----垃圾收集、内存分配

    No1: 程序计数器.虚拟机栈.本地方法栈3个区域随线程而生,随线程而灭:栈中的栈帧随着方法的进入和退出而有条不紊的执行着出栈和入栈操作.每一个栈帧中分配多少内存基本上市在类结构确定下来时就已知的,因 ...

  7. 洛谷P1438 无聊的数列 [zkw线段树]

    题目传送门 无聊的数列 题目背景 无聊的YYB总喜欢搞出一些正常人无法搞出的东西.有一天,无聊的YYB想出了一道无聊的题:无聊的数列...(K峰:这题不是傻X题吗) 题目描述 维护一个数列{a[i]} ...

  8. Windows 下安装 tensorflow & keras & opencv 的避坑指南!

    安装 Anaconda3 关键的一步: conda update pip 下面再去安装各种你需要的包,一般不会再报错. pip install -U tensorflow pip install -U ...

  9. SpringMVC框架 之 from标签(转)

    原文地址:http://blog.csdn.net/kutim/article/details/46682547 spring表单标签   <%@taglib uri="http:// ...

  10. openstack vm实例pxe无法启动

    问题如下: 创建vm没有任何报错,打开控制台提示: SeaBIOS (versio xxxxxxx) Machine UUID xxxxxxxxxx iPXE (http://ipxe.org) 00 ...