疫情查询APP开发

一、目标

二、思路

  一开始认为只要用之前查出来的东西放到网上,然后将APP联网就可以查取了,后来发现自己想多了。中间的连接触及了我的知识盲区,感谢大佬们的帮忙!

三、源程序代码

package com.example.cov_2;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Type;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    Button select_btn;
    TextView response_tv;
    private static final String TAG = "MainActivity";
    EditText where_et;
    DatePicker datePicker;
    Spinner type_sp;
    private String result;
    String date;
    TextView show;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        select_btn = findViewById(R.id.bt_select);
        response_tv = findViewById(R.id.tv_show);
        where_et = findViewById(R.id.et_where);
        datePicker = findViewById(R.id.datepicker);
        type_sp = findViewById(R.id.sp_type);
        show = findViewById(R.id.tv_show);
        select_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        date = datePicker.getYear()+"-0"+(datePicker.getMonth()+1)+"-"+datePicker.getDayOfMonth();
                        Log.d(TAG, "onClick: "+date);
                        requestDate();
                    }
                }).start();

//                Log.d(TAG, "onClick: "+datePicker.getYear()+"  "+datePicker.getMonth()+"  "+datePicker.getDayOfMonth());
//                Log.d(TAG, "onClick: 1"+where_et.getText().toString());
//                Log.d(TAG, "onClick: "+type_sp.getSelectedItem().toString());

            }
        });
    }

    private void requestDate() {
        if (where_et.getText() != null) {
            try {
                String ul="";
                try {

                    if(type_sp.getSelectedItem().toString().equals("国家")){
                        ul = "/" + where_et.getText().toString() + "/" + date;
                    }else if (type_sp.getSelectedItem().toString().equals("省份")||type_sp.getSelectedItem().toString().equals("城市")){
                        ul = "" + where_et.getText().toString() + "/" + date;
                    }
//                    switch (where_et.getText().toString()) {
//                        case "国家":
//                            ul = "" + where_et.getText().toString() + "/" + date;
//                            break;
//                        case "省份":
//                        case "城市":
//                            ul = "" + where_et.getText().toString() + "/" + date;
//                            break;
//                        default:
//                            throw new IllegalStateException("Unexpected value: " + where_et.getText().toString());
//                    }
                    Log.d(TAG, "requestDate: *"+ul);
                    URL url = new URL(ul);
                    HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                    httpURLConnection.setConnectTimeout(30 * 1000);
                    httpURLConnection.setRequestMethod("GET");
                    httpURLConnection.setRequestProperty("Content-Type", "application/json");
                    httpURLConnection.setRequestProperty("Charset", "UTF-8");
                    httpURLConnection.setRequestProperty("Accept-Charset", "UTF-8");
                    httpURLConnection.connect();//发起连接
                    int responseCode = httpURLConnection.getResponseCode();
                    String responseMessage = httpURLConnection.getResponseMessage();

                    if (responseCode == HttpURLConnection.HTTP_OK) {
                        InputStream inputStream = httpURLConnection.getInputStream();
                        result = streamToString(inputStream);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                List<bean> list = new ArrayList<bean>();
                                Gson gson = new Gson();
                                Type cityType = new TypeToken<ArrayList<bean>>() {
                                }.getType();

                                list = gson.fromJson(result, cityType);
                                String ziz = "";
                                for (bean yiqi : list) {
                                    ziz = yiqi.getName() + "\n确诊:" + yiqi.getConfirm() + "\n治愈:" + yiqi.getHeal() +
                                            "\n死亡:" + yiqi.getDead() + "\n";
                                    break;

                                }
                                show.setText(ziz);
                            }
                        });


                    }
                } catch (ProtocolException e) {
                    e.printStackTrace();
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } finally {

            }
        }


    }




    public String streamToString(InputStream is) {
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int len;
            while ((len = is.read(buffer)) != -1) {
                baos.write(buffer, 0, len);
            }
            baos.close();
            is.close();
            byte[] byteArray = baos.toByteArray();
            return new String(byteArray);
        } catch (Exception e) {
            Log.e(TAG, e.toString());
            return null;
        }
    }
}
MainActivity

  在Androidmanifest里面打开网络权限

 

 

   之后还是连接不上 报错

 

 

   然后在网上查了只有发现是Android9.0给禁用了,所以又加了一句标签

  之后再接受输入的数据然后通过服务器的接口查询出来的数据就可以显示出来

三、效果图

 

 

 

四、PSP

 

 

 

 

 

 

 

五、心得体会

  这次的任务拖得有些晚,除去新报网课和作业的原因我觉得更多的是自己的懒惰,玩一会,一玩一天就过去了。今天要是不早起的话又不知道要拖到什么时候了哦T_T。不过自己的东西好赖连上网了,还是有点成就感的,希望早日可以拥有一个自己的服务器,自己可以玩转的服务器!

posted @ 2020-03-21 10:57  酸奶面包  阅读(519)  评论(0编辑  收藏  举报