zoukankan      html  css  js  c++  java
  • 疫情信息爬取及可视化 app

    要求

    1.要求开发一款移动端的全世界疫情实时查询系统。

    2.要求将前两周的项目合并为一个完整的项目。 采用统一的数据库。(建议MySQL数据库)

    3.实现从数据采集、数据存储、数据查询(WEB端和移动端)一体全世界实时疫情查询系统。

    4.以本机数据库为服务器端,web端和移动端连接远程数据库实现数据共享,要求数据库表格式统一化。

    5.查询显示当前最新时间的数据,可以查询任一时间任一地点的数据。该系统要求在服务器端发布,可以通过IP地址访问。

    设计思路

    1.数据准备,使用python从网上爬取世界疫情的数据,并存入mysql数据库

    2.数据展示,从Android发送http请求到web端web端在从数据库获取数据

    3.使用volley的stringrequest向web请求数据(json)

    4.使用Gson进行数据转换

    psp表

     

    主要代码

    Tomcat 发布的Servlet

    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 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

    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.1.4: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") + " 确诊人数:" + jsonObject.getString("confirm") + " 死亡人数:" + jsonObject.getString("dead") + " 治愈人数:" + jsonObject.getString("heal") ); } } 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; } }
  • 相关阅读:
    Android获取视频音频的时长的方法
    Android动画效果之Frame Animation(逐帧动画)
    去除自定义Toolbar中左边距
    Android Toolbar样式定制详解
    Android 5.x Theme 与 ToolBar 实战
    Android ToolBar 使用完全解析
    Android开发:最详细的 Toolbar 开发实践总结
    SpannableString 转换局部字体大小,但在EditText测量之前设置内容,测量高度为,字体变小之前的高度
    android在Service中弹出Dialog对话框,即全局性对话框
    Could not find com.android.tools.build:gradle:3.0.0-alpha3
  • 原文地址:https://www.cnblogs.com/best-hym/p/12554544.html
Copyright © 2011-2022 走看看