通过GET方式传递数据给服务器
来源:互联网 发布:什么软件识别歌曲 编辑:程序博客网 时间:2024/06/10 09:12
1.web服务端的建立
建立Dynamic Web Project,名称为ServerGET
2.新建servlet
自动生成的ServerGET.java中有
@WebServlet(“/ServerGET”)
所以web.xml就不需要配置了
3.编写ServerGET.java
package com.example.servletGet;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 ServerGET */@WebServlet("/ServerGET")public class ServerGET extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public ServerGET() { 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(name + " " + age); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse * response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { }}
4.测试服务端
5.建立android客户端
布局文件main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LinearLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.userinformation.MainActivity" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="name:" /> <EditText android:id="@+id/name" android:layout_width="match_parent" android:layout_height="wrap_content" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="age:" /> <EditText android:id="@+id/age" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="save" /></LinearLayout>
MainActivity.java
package com.example.userinformation;import android.app.Activity;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;public class MainActivity extends Activity { private EditText nameText, ageText; private Button button; private Handler myHandler = new Handler() { public void handleMessage(Message msg) { String text = ""; if (msg.what == 1) { text = "success"; } else if (msg.what == 2){ text = "fail"; } Toast.makeText(MainActivity.this, text, Toast.LENGTH_SHORT).show(); }; }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); nameText = (EditText) findViewById(R.id.name); ageText = (EditText) findViewById(R.id.age); button = (Button) findViewById(R.id.button1); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { final String name = nameText.getText().toString(); final String age = ageText.getText().toString(); new Thread(new Runnable() { @Override public void run() { try { //将EditText中的数据传到服务器 boolean result = UserInfoService.save(name, age); if (result == true) { myHandler.sendEmptyMessage(1); } else { myHandler.sendEmptyMessage(2); } } catch (Exception e) { e.printStackTrace(); } } }).start(); } }); }}
UserInfoService.java
package com.example.userinformation;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 UserInfoService { public static boolean save(String name, String age) throws Exception { String path = "http://10.107.56.64:8080/ServerGET/ServerGET"; Map<String, String> params = new HashMap<String, String>(); params.put("name", name); params.put("age", age); return sendGETRequest(path, params, "UTF-8"); } private static boolean sendGETRequest(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); } // 上面的sb用来构造url HttpURLConnection connection = (HttpURLConnection) new URL(sb.toString()) .openConnection(); connection.setConnectTimeout(5000); connection.setRequestMethod("GET"); if (connection.getResponseCode() == 200) { return true; } return false; }}
测试效果:
0 0
- 通过GET方式传递数据给服务器
- 通过GET方式传递 数据到服务器
- 通过POST方式传递数据给服务器
- Android网络编程get方式传递数据给服务器
- android 通过get,post方式提交数据给服务器
- android中的http通信---(3)通过get、post方式传数据给服务器
- Android通过Get,Post,HttpClient方式提交参数给服务器
- Android通过Get,Post,HttpClient方式提交参数给服务器
- Android通过POST方法传递数据给服务器
- 使用GET和POST方式将数据提交给服务器
- c# POST和GET方式通过服务器地址提交数据
- Android通过POST方式传递数据到服务器简单demo
- 通过超链接get方式传递id信息
- php 通过get方式传递json
- 数据采集-关于post生成ticket传递参数给另一个页面用get方式验证登录
- IOS 通过GET方式与服务器通信
- Java通过Json传递数据给JavaScript
- 通过ajax引擎对象向服务器发送数据的2中方式:get、post
- Rotate Array 带测试版
- python小模块---zipfile
- 【Linux】java jdk安装路径查看和设置环境变量
- Bookmarks
- 可用的hostapd.conf
- 通过GET方式传递数据给服务器
- Ubuntu系统下的CAJ文件阅读
- IOS-使用XCODE自带的单元测试UnitTest
- bzoj 2502: 清理雪道
- Ant通配符
- 图形面积计算
- 实践--应用广播收发短信
- LeetCode *** 217. Contains Duplicate
- Factorial Trailing Zeroes 带测试版