通过POST方式传递数据给服务器
2016-04-07 10:43
288 查看
1.新建动态web工程(ServerPOST)
2.新建servlet。设置如下:
3.ServerPOST.java
4.测试效果
5.建立客户端程序(UserInformationPOST)
布局文件,MainActivity.java和上一篇(http://blog.csdn.net/lxj1137800599/article/details/51077569)完全一样
UploadUserInfoService.java
6.测试结果
2.新建servlet。设置如下:
3.ServerPOST.java
package com.example.servletPOST; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class ServerPOST */ @WebServlet("/ServerPOST") public class ServerPOST extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public ServerPOST() { super(); } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse * response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // String name = request.getParameter("name"); // String age = request.getParameter("age"); // System.out.println("POST : " + name + " " + age); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse * response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //这次是POST请求 String name = request.getParameter("name"); String age = request.getParameter("age"); System.out.println("POST : " + name + " " + age); } }
4.测试效果
5.建立客户端程序(UserInformationPOST)
布局文件,MainActivity.java和上一篇(http://blog.csdn.net/lxj1137800599/article/details/51077569)完全一样
UploadUserInfoService.java
package com.example.userinformationpost; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder; import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; public class UploadUserInfoService { public static boolean save(String name, String age) throws Exception { String path = "http://10.107.56.64:8080/ServerPOST/ServerPOST"; Map<String, String> params = new HashMap<String, String>(); params.put("name", name); params.put("age", age); return sendPOSTRequest(path, params, "UTF-8"); } private static boolean sendPOSTRequest(String path, Map<String, String> params, String encoding) throws Exception { StringBuilder sb = new StringBuilder(path); if (params != null && !params.isEmpty()) { sb.append("?"); for (Entry<String, String> entry : params.entrySet()) { sb.append(entry.getKey()).append("="); sb.append(URLEncoder.encode(entry.getValue(), encoding)) .append("&"); } sb.deleteCharAt(sb.length() - 1); } byte[] data = sb.toString().getBytes(); // 上面的sb用来构造url HttpURLConnection connection = (HttpURLConnection) new URL( sb.toString()).openConnection(); connection.setConnectTimeout(5000); //*************** connection.setRequestMethod("POST"); connection.setDoOutput(true);//如果设置POST,必须设置可以对外输出数据 connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); connection.setRequestProperty("Content-Length", data.length+""); OutputStream outputStream = connection.getOutputStream(); outputStream.write(data); outputStream.flush(); //************* //GET和POST就这里不一样 if (connection.getResponseCode() == 200) { return true; } return false; } }
6.测试结果
相关文章推荐
- redis快速入门
- C++作业3
- 建站服务器的最优选择之Windows Or Linux
- nginx 并发数问题思考:worker_connections,worker_processes与 max clients
- js 判断一个json里是不是包含某个key
- linux线程同步03
- Android动态换肤(一、应用内置多套皮肤)
- 分享下android中路径动画svg格式的(一)
- 利用StegSolve、Python对.jpg文件进行分析
- Axis2创建WebService实例
- 2008
- this指针指向的彻底理解
- Linux下使用wget命令下载百度云盘文件
- ffmpeg编程基础和一些常见问题的解答
- mysql update使用子查询
- Android动态换肤(一、应用内置多套皮肤)
- LeetCode 102, 103, 107. Binary Tree Level Order Traversal i, ii, Zigzag Level Order
- 52. N-Queens II
- 给Eclipse提速的7个技巧
- Android SDK 下载