我是靠谱客的博主 称心雨,这篇文章主要介绍Android开发疫情查询app(实例代码),现在分享给大家,希望可以做个参考。

一丶工作原理:

App 通过请求本地tomcat发布的servlet (调用了 HttpURLConnection 方法)获取MySQL数据库当中的数据,获取数据并返回到App 当中,显示给用户。(其中传递的格式为 json)

使用的工具:Android Studio  开发APP  Eclipse 发布Servlet,数据传递

二丶运行代码:

Tomcat 发布的Servlet 类:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package com.Servlet; 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; import com.Bean.worldbean; import com.Dao.Dao; import com.google.gson.Gson; /** * Servlet implementation class Worldservlet */ @WebServlet("/Worldservlet") public class Worldservlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public Worldservlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.setContentType("text/html;charset=UTF-8"); request.setCharacterEncoding("UTF-8"); String s=null; //获取传递过来的参数 String date = request.getParameter("date"); String name =request.getParameter("name"); // Gson 谷歌推出的用于生成和解析JSON 数据格式的工具 使用时需要 导入jar 包 我的是 gson-2.6.2.jar Gson gson=new Gson(); try { worldbean info= Dao.getinfo(date,name); //将数据 转换为 Json 格式 s=gson.toJson(info); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } //System.out.println(s); //方法作用 只能打印输出文本格式的(包括html标签) 不可打印对象 response.getWriter().write(s); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }

As 当中的MainActivity:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package com.example.yiqingdemo; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import org.json.JSONObject; import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; public class MainActivity extends AppCompatActivity { EditText editTextCountry, editTextDate; TextView textView; Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editTextCountry = findViewById(R.id.editText4); editTextDate = findViewById(R.id.editText3); textView = findViewById(R.id.textView2); button = findViewById(R.id.button); button.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { //本机tomcat 发布的网站 其实是一个servlet 类 必须先让本机发布(启动tomcat 运行) 然后才能访问改网站 String url = "http://192.168.0.106:8080/YiQingSearch/Worldservlet?date=" + editTextDate.getText().toString() + "&name=" + editTextCountry.getText().toString(); get(url); } } ); } public void get(final String url) { new Thread(new Runnable() { @Override public void run() { HttpURLConnection connection = null; InputStream is = null; try { //获取url 对象 URL Url = new URL(url); //获取httpURlConnection 对象 connection = (HttpURLConnection) Url.openConnection(); //默认为get方法 or post connection.setRequestMethod("GET"); //默认不使用缓存 connection.setUseCaches(false); //设置连接超时时间 单位毫秒 connection.setConnectTimeout(10000); //设置读取超时时间 connection.setReadTimeout(10000); //设置是否从httpUrlConnection 读入,默认为true connection.setDoInput(true); //相应的码数为 200 if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) { //获取输入流 is = connection.getInputStream(); //将输入流内的数据变为Sting类型数据 String info = getStringFromInputStream(is); //转换为JSON 类型便于读取 JSONObject jsonObject = new JSONObject(info); textView.setText( "更新时间:" + jsonObject.getString("updatetime") + "n确诊人数:" + jsonObject.getString("confirm") + "n死亡人数:" + jsonObject.getString("dead") + "n治愈人数:" + jsonObject.getString("heal") ); /* //获取url 网页的源代码 BufferedReader reader= new BufferedReader(new InputStreamReader(is)); //包装字节流为字符流 StringBuilder response = new StringBuilder(); String line; while((line = reader.readLine())!=null){ response.append(line); } String s = response.toString(); */ } } catch (Exception e) { e.printStackTrace(); } finally { if (connection != null) { connection.disconnect(); } } } }).start(); } private static String getStringFromInputStream(InputStream is) throws Exception { //定义字节数组缓存区 ByteArrayOutputStream by = new ByteArrayOutputStream(); byte[] buff = new byte[1024]; int len = -1; while ((len = is.read(buff)) != -1) { by.write(buff, 0, len); } is.close(); //将缓冲区的数据转换为 String 类型 String html = by.toString(); by.close(); return html; } }

除此之外还需要给APP赋予权限 :

As 的 AndroidMainfest 如下:

添加注释的为自主添加的权限

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.yiqingdemo"> <uses-permission android:name="android.permission.INTERNET" /> <!--联网所需要的权限--> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <!-- 主要用于管理 WIFI 连接的各方面--> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <!--主要用于监视一般网路连接 --> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme" android:usesCleartextTraffic="true"> <!-- 指示应用程序是否打算使用明文网络流量 --> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>

三丶 运行结果:

以上就是Android开发实例(疫情查询app)的详细内容,更多关于Android开发APP的资料请关注靠谱客其它相关文章!

最后

以上就是称心雨最近收集整理的关于Android开发疫情查询app(实例代码)的全部内容,更多相关Android开发疫情查询app(实例代码)内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(257)

评论列表共有 0 条评论

立即
投稿
返回
顶部