服务端:

  1. package com.sxt.day05;
  2.  
  3. import java.io.IOException;
  4. import java.util.ArrayList;
  5.  
  6. import javax.servlet.ServletException;
  7. import javax.servlet.annotation.WebServlet;
  8. import javax.servlet.http.HttpServlet;
  9. import javax.servlet.http.HttpServletRequest;
  10. import javax.servlet.http.HttpServletResponse;
  11.  
  12. import org.codehaus.jackson.map.ObjectMapper;
  13.  
  14. import com.sxt.day05.entity.User;
  15.  
  16. @WebServlet("/Login")
  17. public class Login extends HttpServlet {
  18. private static final long serialVersionUID = 1L;
  19. ArrayList<User> users;
  20.  
  21. public Login() {
  22. super();
  23. User user=new User("张飞", "1234", "13377889966", "zf@qq.com");
  24. users=new ArrayList<>();
  25. users.add(user);
  26. user=new User("王菲", "1234", "13577889966", "wf@qq.com");
  27. users.add(user);
  28. user=new User("刘亦菲", "1234", "133077889966", "lyf@qq.com");
  29. users.add(user);
  30. user=new User("咖啡", "1234", "13877889966", "kf@qq.com");
  31. users.add(user);
  32. }
  33.  
  34. /**
  35. * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
  36. */
  37. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  38. String name=request.getParameter("name");//获取客户端发过来的名字
  39. if(name==null){
  40. return ;
  41. }
  42. name=new String(name.getBytes("iso8859-1"),"utf-8");
  43. String pwd=request.getParameter("pwd");
  44. for(int i=0;i<users.size();i++){
  45. User user=users.get(i);
  46. if(name.equals(user.getName())&&pwd.equals(user.getPwd())){
  47. ObjectMapper om=new ObjectMapper();//jackson-core-asl.jar
  48. om.writeValue(response.getOutputStream(), user);//将user对象以输出流发送给客户端
  49. System.out.println(user.toString());
  50. break;
  51. }
  52. }
  53. }
  54.  
  55. /**
  56. * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
  57. */
  58. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  59. ArrayList<User> list=new ArrayList<>();
  60. request.setCharacterEncoding("utf-8");
  61. String name=request.getParameter("name");
  62. for(int i=0;i<users.size();i++){
  63. User user=users.get(i);
  64. if(user.getName().indexOf(name)>=0){//name在user的名字中
  65. list.add(user);
  66. }
  67. }
  68. ObjectMapper om=new ObjectMapper();
  69. om.writeValue(response.getOutputStream(), list);
  70. }
  71.  
  72. }

客户端:

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="match_parent"
  3. android:layout_height="match_parent"
  4. android:orientation="vertical">
  5. <EditText
  6. android:id="@+id/etName"
  7. android:layout_width="match_parent"
  8. android:layout_height="wrap_content"
  9. android:hint="输入姓名"
  10. android:text="菲"/>
  11. <EditText
  12. android:id="@+id/etPassword"
  13. android:layout_width="match_parent"
  14. android:layout_height="wrap_content"
  15. android:hint="输入登陆密码"
  16. android:text="123456"
  17. android:password="true"/>
  18.  
  19. <Button
  20. android:id="@+id/btnLogin"
  21. android:layout_width="match_parent"
  22. android:layout_height="wrap_content"
  23. android:text="GET方式登陆" />
  24. <Button
  25. android:id="@+id/btnQuery"
  26. android:layout_width="match_parent"
  27. android:layout_height="wrap_content"
  28. android:text="POST方式查询" />
  29.  
  30. </LinearLayout>

客户端Activity:

  1. package com.sxt.day06_03;
  2.  
  3. import java.io.IOException;
  4. import java.io.OutputStream;
  5. import java.io.UnsupportedEncodingException;
  6. import java.net.HttpURLConnection;
  7. import java.net.MalformedURLException;
  8. import java.net.URL;
  9. import java.net.URLEncoder;
  10. import java.util.List;
  11.  
  12. import org.apache.http.client.utils.URLEncodedUtils;
  13. import org.codehaus.jackson.map.ObjectMapper;
  14.  
  15. import com.sxt.day06.entity.User;
  16.  
  17. import android.app.Activity;
  18. import android.os.Bundle;
  19. import android.util.Log;
  20. import android.view.View;
  21. import android.view.View.OnClickListener;
  22. import android.widget.EditText;
  23. import android.widget.Toast;
  24.  
  25. public class MainActivity extends Activity {
  26. private static final String PATH="http://10.0.2.2:8080/Day06_Servlet/Login_Servlet";//服务端地址
  27.  
  28. EditText metName,metPwd;
  29.  
  30. @Override
  31. protected void onCreate(Bundle savedInstanceState) {
  32. super.onCreate(savedInstanceState);
  33. setContentView(R.layout.activity_main);
  34. initView();
  35. setListener();
  36. }
  37.  
  38. private void setListener() {
  39. setLoginClickListener();
  40. setQueryClickListener();
  41. }
  42.  
  43. private void setQueryClickListener() {//查询,POST方式
  44. findViewById(R.id.btnQuery).setOnClickListener(new OnClickListener() {
  45. @Override
  46. public void onClick(View v) {
  47. new Thread(){//访问网络要在工作线程搞
  48. public void run() {
  49. String name=metName.getText().toString();
  50. name="name="+name;
  51. try {
  52. byte[] data=name.getBytes("utf-8");
  53. URL url=new URL(PATH);
  54. HttpURLConnection conn=(HttpURLConnection) url.openConnection();
  55. conn.setConnectTimeout(5000);
  56. conn.setReadTimeout(5000);
  57. conn.setRequestMethod("POST");
  58. conn.setDoOutput(true);//像服务端发送数据
  59. OutputStream out = conn.getOutputStream();
  60. out.write(data);
  61. out.flush();
  62. if(conn.getResponseCode()!=200){
  63. return ;
  64. }
  65. ObjectMapper om=new ObjectMapper();
  66. List<User> users=om.readValue(conn.getInputStream(), List.class);
  67. Log.i("main",users.toString());
  68. } catch (UnsupportedEncodingException e) {
  69. e.printStackTrace();
  70. } catch (MalformedURLException e) {
  71. e.printStackTrace();
  72. } catch (IOException e) {
  73. e.printStackTrace();
  74. }
  75. };
  76. }.start();
  77. }
  78. });
  79. }
  80.  
  81. private void setLoginClickListener() {
  82. findViewById(R.id.btnLogin).setOnClickListener(new OnClickListener() {
  83. @Override
  84. public void onClick(View v) {
  85. new Thread(){//访问网络要在工作线程搞
  86. public void run() {
  87. String name=metName.getText().toString();
  88. String password=metPwd.getText().toString();
  89. StringBuilder sb=new StringBuilder(PATH);//PATH地址
  90. try {
  91. sb.append("?name=").append(URLEncoder.encode(name, "utf-8"))
  92. .append("&password=").append(password);
  93. URL url=new URL(sb.toString());//
  94. HttpURLConnection conn=(HttpURLConnection)url.openConnection();
  95. conn.setReadTimeout(5000);//读取权限5秒,否则断开
  96. conn.setConnectTimeout(5000);//连接时间5秒,否则断开
  97. conn.setRequestMethod("GET");//
  98. if(conn.getResponseCode()!=200){
  99. Toast.makeText(MainActivity.this, "连接服务端失败", 2000).show();
  100. return ;
  101. }
  102. ObjectMapper om=new ObjectMapper();
  103. User user = om.readValue(conn.getInputStream(), User.class);
  104. Log.i("main",user.toString());
  105. } catch (MalformedURLException e) {
  106. e.printStackTrace();
  107. } catch (IOException e) {
  108. e.printStackTrace();
  109. }
  110. };
  111. }.start();
  112. }
  113. });
  114. }
  115.  
  116. private void initView() {
  117. metName=(EditText) findViewById(R.id.etName);
  118. metPwd=(EditText) findViewById(R.id.etPassword);
  119. }
  120.  
  121. }

客户端项目描述文件:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.sxt.day06_03"
  4. android:versionCode="1"
  5. android:versionName="1.0" >
  6.  
  7. <uses-sdk
  8. android:minSdkVersion="8"
  9. android:targetSdkVersion="18" />
  10. <uses-permission android:name="android.permission.INTERNET"/> 安卓想访问网络要申请网络权限
  11. <application
  12. android:allowBackup="true"
  13. android:icon="@drawable/ic_launcher"
  14. android:label="@string/app_name"
  15. android:theme="@style/AppTheme" >
  16. <activity
  17. android:name="com.sxt.day06_03.MainActivity"
  18. android:label="@string/app_name" >
  19. <intent-filter>
  20. <action android:name="android.intent.action.MAIN" />
  21.  
  22. <category android:name="android.intent.category.LAUNCHER" />
  23. </intent-filter>
  24. </activity>
  25. </application>
  26.  
  27. </manifest>

android 38 Abdroid客户端和服务端交互的更多相关文章

  1. Android客户端与服务端交互之登陆示例

    Android客户端与服务端交互之登陆示例 今天了解了一下android客户端与服务端是怎样交互的,发现其实跟web有点类似吧,然后网上找了大神的登陆示例,是基于IntentService的 1.后台 ...

  2. java客户端与服务端交互通用处理 框架解析

    一.综述 java 客户端与服务端交互过程中,采用NIO通讯是异步的,客户端基本采用同一处理范式,来进行同异步的调用处理. 处理模型有以下几个要素: 1. NIO发送消息后返回的Future 2. 每 ...

  3. c++ 网络编程(一)TCP/UDP windows/linux 下入门级socket通信 客户端与服务端交互代码

    原文作者:aircraft 原文地址:https://www.cnblogs.com/DOMLX/p/9601511.html c++ 网络编程(一)TCP/UDP  入门级客户端与服务端交互代码 网 ...

  4. Fresco 源码分析(二) Fresco客户端与服务端交互(3) 前后台打通

    4.2.1.2.4 PipelineDraweeControllerBuilder.obtainController()源码分析 续 上节中我们提到两个核心的步骤 obtainDataSourceSu ...

  5. Fresco 源码分析(二) Fresco客户端与服务端交互(1) 解决遗留的Q1问题

    4.2 Fresco客户端与服务端的交互(一) 解决Q1问题 从这篇博客开始,我们开始讨论客户端与服务端是如何交互的,这个交互的入口,我们从Q1问题入手(博客按照这样的问题入手,是因为当时我也是从这里 ...

  6. UDP网络程序,客户端和服务端交互原理

    创建一个udp客户端程序的流程是简单,具体步骤如下: 创建客户端套接字 发送/接收数据 关闭套接字 UDP是面向无连接的通讯协议,UDP数据包括目的端口号和源端口号信息,由于通讯不需要连接,所以可以实 ...

  7. spring-oauth-server实践:使用授权方式四:client_credentials 模式的客户端和服务端交互

    spring-oauth-server入门(1-11)使用授权方式四:client_credentials 模式的客戶端 一.客户端逻辑 1.界面入口(credentials_access_token ...

  8. Fresco 源码分析(二) Fresco客户端与服务端交互(2) Fresco.initializeDrawee()分析 续

    4.2.1.2 Fresco.initializeDrawee()的过程 续 继续上篇博客的分析Fresco.initializeDrawee() sDraweeControllerBuilderSu ...

  9. IOS开发系列之阿堂教程:玩转IPhone客户端和Web服务端交互(客户端)实践

    说到ios的应用开发,我们不能不提到web server服务端,如果没有服务端的支持,ios应用开发就没有多大意义了,因为从事过手机开发的朋友都知道(Android也一样),大量复杂业务的处理和数据库 ...

随机推荐

  1. Uninstall office15 click-to-run extensibility Component

    Summary : Uninstall office15 click-to-run extensibility Component,How to resolve Uninstall office15 ...

  2. PM【terminal】

    More Knowledge More Performance More Time 资料模组化 以知识管理为基础的项目管理 规范:ethic

  3. python调用java

    这么个标题多少有点蛋疼的感觉,两个都是互联网时代的语言,学习成本和执行效率也差不多,之所以会产生这种需求,多半是想在python中引用java的类,例如安卓和hadoop的生态圈,基本是java代码的 ...

  4. Test for open live write

    this is test document. this is test document. this is test document. this is test document. this is ...

  5. JDK的帮助文档

    1.JDK1.8在线api,英文版 https://docs.oracle.com/javase/8/docs/api/

  6. 【关于php】Appserv中关于DW配置站点问题

    用DW运行的话,还要配置下站点.或者你直接在浏览器地址栏上输入:http://localhost:8080/p5-1.php  或者是http://localhost/p5-1.php dreamwe ...

  7. vim自动补全

    Vim 中使用 OmniComplete 为 C/C++ 自动补全 OmniComplete 并不是插件的名字,而是 Vim 众多补全方式中的一种(全能补全).说白了 OmniComplete 其实就 ...

  8. Jsp中获得集合List或Set的长度

    首先要引入<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%> ...

  9. 安装drupal练习网站遇到的问题

    1 Skip #conjunction key in __clone() method of core/includes/database/query.inc 解决方案:https://www.dru ...

  10. .rdp 文件参数详解

    Overview of .rdp file settings Setting Type Default value Description and possible values Settable f ...