安卓和服务器交换数据
来源:互联网 发布:sql查找两列重复的 编辑:程序博客网 时间:2024/05/13 21:27
先在服务器端设置一个select来接收Android端发送过来的数据,具体的数据验证属于jsp内容了,在此不多说。具体代码:
web端:ManageServlet.java
package cn.acm.edu.hpu.servlet;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 ManageServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); String name = request.getParameter("name"); String password = request.getParameter("password"); System.out.println("用户名:"+name+" 密码:"+password); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { }}
Android端:MainActivity.java
public class MainActivity extends Activity { private EditText textname = null; private EditText textpassword = null; private Button button = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textname = (EditText)findViewById(R.id.name); textpassword = (EditText)findViewById(R.id.password); button = (Button)findViewById(R.id.button); button.setOnClickListener(new mybuttonlistener()); } class mybuttonlistener implements OnClickListener{ boolean result=false; public void onClick(View v) { String name = textname.getText().toString(); String password = textpassword.getText().toString(); try { result = NewsService.save(name,password); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } if(result){ Toast.makeText(MainActivity.this, R.string.ok, Toast.LENGTH_SHORT).show(); }else{ Toast.makeText(MainActivity.this, R.string.error, Toast.LENGTH_SHORT).show(); } } } }
import javax.net.ssl.HttpsURLConnection;public class NewsService { /** * 登录验证 * @param name 姓名 * @param password 密码 * @return */ public static boolean save(String name, String password){ String path = "http://10.20.90.3:8080/Register/ManageServlet"; Map<String, String> student = new HashMap<String, String>(); student.put("name", name); student.put("password", password); try { return SendGETRequest(path, student); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return false; } /** * 发送GET请求 * @param path 请求路径 * @param student 请求参数 * @return 请求是否成功 * @throws Exception */ private static boolean SendGETRequest(String path, Map<String, String> student) throws Exception{ // http://10.20.90.3:8080/Register/ManageServlet?name=1233&password=abc StringBuilder url = new StringBuilder(path); url.append("?"); for(Map.Entry<String, String> map : student.entrySet()){ url.append(map.getKey()).append("="); url.append(map.getValue()); url.append("&"); } url.deleteCharAt(url.length()-1); System.out.println(url); HttpsURLConnection conn = (HttpsURLConnection)new URL(url.toString()).openConnection(); conn.setConnectTimeout(100000); conn.setRequestMethod("GET"); if(conn.getResponseCode() == 200){ return true; } return false; }}
0 0
- 安卓和服务器交换数据
- 安卓初学-Fragment之间交换数据
- 安卓Fragment之间数据交换
- 安卓向服务器发送数据和服务器接收数据全过程
- 通讯和数据交换服务器Transceiver 自述文件
- Android客户端和服务器交换数据的一种方案
- login与服务器交换数据
- 安卓向服务器发送List数据
- 安卓服务器获取客户端数据
- 安卓连接服务器,并且向服务器传输JSON数据
- 用json实现服务器servlet和安卓客户端的数据交互
- 安桌文件上传和数据上传到服务器
- 安卓向服务器提交数据,并获取数据
- 安卓使用aidl实现两个程序之间的数据交换
- nodejs客户端与服务器双向数据交换
- iOS 使用NSURLConnection连接服务器和JSON作为数据交换方式
- 对话框数据交换和检验
- 对话框数据交换和验证
- 16. Window close() 方法
- How to download offline javadoc?
- HDU 1166
- Maxwell 16.0 (64-bit) 软件卸载_Win7系统
- webdriver操作iframe标记的编辑器
- 安卓和服务器交换数据
- 排序算法
- DNS服务测试IPv4 IPv6
- java 生成4位数的随机数据 70万条
- iOS应用内付费(IAP)开发步骤列表
- 给label添加不同颜色不同字体的内容
- C# + emgucv显示实时视频并检测人脸
- Vijos 1255 月饼盒 最大子矩阵
- 【HDOJ 5328】 Problem Killer