安卓疫情

安卓疫情,第1张

概述07移动端疫情展示要求要求开发一款移动端的全世界疫情实时查询系统。要求将前两周的项目合并为一个完整的项目。采用统一的数据库。(建议MySQL数据库)实现从数据采集、数据存储、数据查询(WEB端和移动端)一体全世界实时疫情查询系统。以本机数据库为服务器端,web端和移动端连接远

07移动端疫情展示

要求

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

要求将前两周的项目合并为一个完整的项目。

采用统一的数据库。(建议MysqL数据库)

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

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

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

 

设计思路

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

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

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

4.使用Gson进行数据转换

 

代码:

import requests

import time, Json

import sys;

import pyMysqL

 

def get_wangyi_request():

    url = 'https://c.m.163.com/ug/API/wuhan/app/data/List-total'

 

    headers = {

        'accept': '*/*',

        'accept-enCoding': 'gzip,deflate,br',

        'accept-language': 'en-US,en;q=0.9,zh-CN;q = 0.8,zh;q = 0.7',

        'origin': 'https://wp.m.163.com',

        'referer': 'https://wp.m.163.com/',

        'sec-fetch-dest': 'empty',

        'sec-fetch-mode': 'cors',

        'sec-fetch-site': 'same-ite',

        'user-agent': 'Mozilla/5.0(windowsNT10.0;Win64;x64) AppleWebKit/37.36 (KHTML, likeGecko) Chrome/82.0.4056.0 Safari/537.36 Edg/82.0.432.3'

    }

 

    result = requests.get(url, headers=headers)

    return result

 

 

def print_mess1(string: str, dict1total: dict):

    sys.stdout.write(string + '确诊: ' + str(dict1total['confirm'] if dict1total['confirm'] != None else 0))

    sys.stdout.write(' ')

    sys.stdout.write(string + '疑似: ' + str(dict1total['SUSPECT'] if dict1total['SUSPECT'] != None else 0))

    sys.stdout.write(' ')

    sys.stdout.write(string + '治愈: ' + str(dict1total['heal'] if dict1total['heal'] != None else 0))

    sys.stdout.write(' ')

    sys.stdout.write(string + '死亡: ' + str(dict1total['dead'] if dict1total['dead'] != None else 0))

 

 

if __name__ == '__main__':

    result = get_wangyi_request()

 

    Json_str = Json.loads(result.text)['data']

    # print(Json_str.keys())

    # dict_keys(['chinaTotal', 'chinaDayList', 'lastUpdateTime', 'areaTree'])

 

    print(Json_str['lastUpdateTime'])

    countryname_List = Json_str['areaTree']

    # 每个省份包含如下的键

    # dict_keys(['today', 'total', 'extData', 'name', 'ID', 'lastUpdateTime', 'children'])

 

    conn = pyMysqL.connect(

        host='localhost',  # 我的IP地址

        port=3306,  # 不是字符串不需要加引号。

        user='root',

        password='123456',

        db='test',

        charset='utf8'

    )

 

    cursor = conn.cursor()  # 获取一个光标

    confirmed_total = 0

    SUSPECTed_total = 0

    dead_total = 0

    healed_total = 0

    ID = 0;

    for dict in countryname_List:

        sql = 'insert into yiqing_world (countryname,confirmed,SUSPECTed,dead,healed,lastUpdateTime,ID) values (%s,%s,%s,%s,%s,%s,%s);'

        countryname = dict['name']

 

        confirmed = dict['total']['confirm']

        confirmed_total += confirmed

        SUSPECTed = dict['total']['SUSPECT']

        SUSPECTed_total += SUSPECTed

        healed = dict['total']['heal']

        dead_total += healed

        dead = dict['total']['dead']

        dead_total += dead

        lastUpdateTime = dict['lastUpdateTime']

        ID=ID+1

        sys.stdout.write( dict['name'] + '  ')

        cursor.execute(sql, [countryname,confirmed,SUSPECTed,dead,healed,lastUpdateTime,ID])

    print()

 

    conn.commit()

 

 

    cursor.close()

conn.close()

import androID.annotation.Suppresslint;

import androID.app.Activity;

import androID.os.Bundle;

import androID.os.Handler;

import androID.os.Message;

import androID.vIEw.VIEw;

import androID.Widget.button;

import androID.Widget.EditText;

import androID.Widget.Radiobutton;

import androID.Widget.RadioGroup;

import androID.Widget.TextVIEw;

 

import java.util.List;

 

public class MainActivity extends Activity {

 

    private EditText et_name;

    private button btn_get_data;

    private TextVIEw tv_data;

    private RadioGroup rg_check;

    private Radiobutton rb_date;

    private Radiobutton rb_country;

    private String condition;

 

    @Suppresslint("HandlerLeak")

    private Handler handler = new Handler(){

        @OverrIDe

        public voID handleMessage(Message msg) {

 

            switch (msg.what){

                case 0x11:

                    String s = (String) msg.obj;

                    tv_data.setText(s);

                    break;

                case 0x12:

                    String ss = (String) msg.obj;

                    tv_data.setText(ss);

                    break;

            }

 

        }

    };

 

    @OverrIDe

    protected voID onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentVIEw(R.layout.activity_main);

 

        // 控件的初始化

        btn_get_data = findVIEwByID(R.ID.btn_get_data);

        tv_data = findVIEwByID(R.ID.tv_data);

        et_name = findVIEwByID(R.ID.et_name);

        rb_date = findVIEwByID(R.ID.rb_date);

        rb_country = findVIEwByID(R.ID.rb_country);

        rg_check = findVIEwByID(R.ID.rg_select);

 

 

        rg_check.setonCheckedchangelistener(new RadioGroup.OnCheckedchangelistener() {

 

            public voID onCheckedChanged(RadioGroup group, int checkedID) {

                //如果‘时间’这个单选按钮被选中了

                if(rb_date.getID()==checkedID){

                    //d出吐司通知

                    //Toast.makeText(MainActivity.this, rb_date.getText().toString(), Toast.LENGTH_LONG).show();

                    //获取选中按钮对应的文本信息

                    condition = rb_date.getText().toString().trim();

                }else if(rb_country.getID()==checkedID){

                    //Toast.makeText(MainActivity.this, rb_country.getText().toString(), Toast.LENGTH_LONG).show();

                    condition = rb_country.getText().toString().trim();

                }

            }

        });

        //如果没有选择默认按时间查询

        if (condition == null){

            condition = rb_date.getText().toString().trim();

        }

        setListener();

    }

 

    /**

     * 设置监听

     */

    private voID setListener() {

 

        // 按钮点击事件

        btn_get_data.setonClickListener(new VIEw.OnClickListener() {

            @OverrIDe

            public voID onClick(VIEw v) {

 

                // 创建一个线程来连接数据库并获取数据库中对应表的数据

                new Thread(new Runnable() {

                    @OverrIDe

                    public voID run() {

                        String name = et_name.getText().toString().trim();

                        //调用数据库帮助类中的方法取数据

                        List<information> List = dbutils.search(condition,name);

                        Message message = handler.obtainMessage();

                        if (List != null) {

                            String s = "";

                            for (int i = 0; i < List.size(); i++) {

                                s += "国家:" + List.get(i).getCountryname() + "\n";

                                s += "最新更新时间:" + List.get(i).getLastUpdateTime() + "\n";

                                s += "确诊人数为:  " + List.get(i).getConfirmed() + "\n";

                                s += "治愈人数为:  " + List.get(i).getHealed() + "\n";

                                s += "死亡人数为:  " + List.get(i).getDead() + "\n" + "\n";

                            }

                            //0x11、0x12消息的定位标志

                            message.what = 0x12;

                            message.obj = s;

                        } else {

                            message.what = 0x11;

                            message.obj = "查询结果为空";

                        }

                        handler.sendMessage(message);

 

                        // 发消息通知主线程更新UI

                    }

                }).start();

            }

        });

 

     }

 

}

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.sqlException;

import java.util.ArrayList;

import java.util.List;

 

/**

 * 数据库工具类:连接数据库用、获取数据库数据用

 * 相关 *** 作数据库的方法均可写在该类

 */

public class dbutils {

 

    private static String driver = "com.MysqL.jdbc.Driver";// MysqL驱动

 

    private static String user = "root";// 用户名

 

    private static String password = "123456";// 密码

 

    private static Connection getConn(String dbname) {

 

        Connection connection = null;

        try {

            Class.forname(driver);// 动态加载类

            String ip = "192.168.1.6";// 写成本机地址,不能写成localhost,同时手机和电脑连接的网络必须是同一个

 

            // 尝试建立到给定数据库URL的连接

            connection = DriverManager.getConnection("jdbc:MysqL://" + ip + ":3306/" + dbname,

                    user, password);

 

        } catch (Exception e) {

            e.printstacktrace();

        }

 

        return connection;

    }

 

    public static List<information>  search(String condition, String country_name){

        List<information> List = new ArrayList<>();

        Connection connection = getConn("test");

        String sql = "";

        //System.out.println(condition);

        //选择条件

        if(condition.equals("国家")){

            //模糊查询

            sql = "select * from yiqing_world where countryname like ?";

        }

        if(condition.equals("时间")){

            sql = "select * from yiqing_world where lastUpdateTime like ?";

        }

 

        System.out.println(country_name);

        if(connection !=null){

            try {

                PreparedStatement ps = connection.prepareStatement(sql);

                if(ps!=null){

                    ps.setString(1,"%"+country_name+"%");

                    ResultSet rs = ps.executequery();

                    if(rs!=null){

                        while(rs.next()){

                            information worldData = new information();

                            worldData.setID(rs.getInt("ID"));

                            worldData.setCountryname(rs.getString("countryname"));

                            worldData.setConfirmed(rs.getString("confirmed"));

                            worldData.setSUSPECTed(rs.getString("SUSPECTed"));

                            worldData.setDead(rs.getString("dead"));

                            worldData.setHealed(rs.getString("healed"));

                            worldData.setLastUpdateTime(rs.getString("lastUpdateTime"));

                            List.add(worldData);

                        }

                        connection.close();

                        ps.close();

                        return List;

                    }else{

                        return null;

                    }

                }else{

                    return null;

                }

            } catch (sqlException e) {

                e.printstacktrace();

                return null;

            }

        }else{

            return null;

        }

 

 

    }

 

}

public class information {

    private int ID;

    private String countryname;

    private String confirmed;

    private String SUSPECTed;

    private String dead;

    private String healed;

    private String lastUpdateTime;

 

 

    public int getID() {

        return ID;

    }

 

    public voID setID(int ID) {

        this.ID = ID;

    }

 

    public String getCountryname() {

        return countryname;

    }

 

    public voID setCountryname(String countryname) {

        this.countryname = countryname;

    }

 

    public String getConfirmed() {

        return confirmed;

    }

 

    public voID setConfirmed(String confirmed) {

        this.confirmed = confirmed;

    }

 

    public String getSUSPECTed() {

        return SUSPECTed;

    }

 

    public voID setSUSPECTed(String SUSPECTed) {

        this.SUSPECTed = SUSPECTed;

    }

 

    public String getDead() {

        return dead;

    }

 

    public voID setDead(String dead) {

        this.dead = dead;

    }

 

    public String getHealed() {

        return healed;

    }

 

    public voID setHealed(String healed) {

        this.healed = healed;

    }

 

    public String getLastUpdateTime() {

        return lastUpdateTime;

    }

 

    public voID setLastUpdateTime(String lastUpdateTime) {

        this.lastUpdateTime = lastUpdateTime;

    }

}

<?xml version="1.0" enCoding="utf-8"?>

<ScrollVIEw xmlns:androID="http://schemas.androID.com/apk/res/androID"

    xmlns:tools="http://schemas.androID.com/tools"

    androID:layout_wIDth="match_parent"

    androID:layout_height="match_parent"

    tools:context=".MainActivity"

    androID:padding="15dp">

 

    <relativeLayout

        androID:layout_wIDth="match_parent"

        androID:layout_height="match_parent">

 

        <RadioGroup

            androID:ID="@+ID/rg_select"

            androID:layout_wIDth="wrap_content"

            androID:layout_height="wrap_content"

            androID:orIEntation="horizontal"

            androID:paddingleft="20dp"

            androID:layout_margintop="20dp">

 

            <Radiobutton

                androID:ID="@+ID/rb_date"

                androID:layout_wIDth="wrap_content"

                androID:layout_height="wrap_content"

                androID:text="时间"

                androID:textSize="20sp"

                androID:checked="true"/>

 

            <Radiobutton

                androID:ID="@+ID/rb_country"

                androID:layout_wIDth="wrap_content"

                androID:layout_height="wrap_content"

                androID:text="国家"

                androID:textSize="20sp"/>

 

        </RadioGroup>

        <EditText

            androID:ID="@+ID/et_name"

            androID:padding="10dp"

            androID:textSize="16sp"

            androID:gravity="center"

            androID:hint="输入要查询的内容"

            androID:layout_below="@ID/rg_select"

            androID:layout_wIDth="match_parent"

            androID:layout_height="wrap_content" />

 

        <button

            androID:ID="@+ID/btn_get_data"

            androID:layout_margin="15dp"

            androID:textSize="16sp"

            androID:text="查询"

            androID:layout_below="@ID/et_name"

            androID:layout_wIDth="match_parent"

            androID:layout_height="wrap_content" />

 

        <TextVIEw

            androID:ID="@+ID/tv_data"

            androID:padding="10dp"

            androID:textSize="16sp"

            androID:gravity="center"

            androID:text="内容"

            androID:layout_below="@+ID/btn_get_data"

            androID:layout_wIDth="match_parent"

            androID:layout_height="wrap_content" />

    </relativeLayout>

 

</ScrollVIEw>

总结

以上是内存溢出为你收集整理的安卓疫情全部内容,希望文章能够帮你解决安卓疫情所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1058634.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存