疫情信息可视化app

这次做的可真是太糟糕了,主要是寒假没有好好学android的相关知识,感觉自己不过关。

1、mainactivity

package com.eb.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.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.URL;
import java.util.LinkedList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
        private TextView textView;
        private Button mbutton;
        private Button mparseDetaButton;
        private  static  final  String TAG="MainActivity";
        private String mresult;
    private String result;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }
    public void initView()
    {
        mbutton=findViewById(R.id.button);
        mparseDetaButton=findViewById(R.id.button2);
        textView=findViewById(R.id.textView);
        mbutton.setOnClickListener(this);
        mparseDetaButton.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.button:

              new Thread(new Runnable() {
                  @Override
                  public void run() {
                      requestDataByGet();

                  }
              }).start();
                break;
            case R.id.button2:
                handleJsonData(result);
                break;
        }
    }

    private void handleJsonData(String result) {

        Type listType = new TypeToken<LinkedList<yiqingclass>>(){}.getType();
        Gson gson = new Gson();
        LinkedList<yiqingclass> users = gson.fromJson(result, listType);

    textView.setText(users.toString());}

        public static String decode(String unicodeStr) {
        if (unicodeStr == null) {
            return null;
        }
        StringBuilder retBuf = new StringBuilder();
        int maxLoop = unicodeStr.length();
        for (int i = 0; i < maxLoop; i++) {
            if (unicodeStr.charAt(i) == '\\') {
                if ((i < maxLoop - 5)
                        && ((unicodeStr.charAt(i + 1) == 'u') || (unicodeStr
                        .charAt(i + 1) == 'U')))
                    try {
                        retBuf.append((char) Integer.parseInt(unicodeStr.substring(i + 2, i + 6), 16));
                        i += 5;
                    } catch (NumberFormatException localNumberFormatException) {
                        retBuf.append(unicodeStr.charAt(i));
                    }
                else {
                    retBuf.append(unicodeStr.charAt(i));
                }
            } else {
                retBuf.append(unicodeStr.charAt(i));
            }
        }
        return retBuf.toString();
    }
    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;
        }
    }
    public void requestDataByGet()
    {
        try {
            URL url=new URL("http://117.50.96.227/EpidemicInfo/city/getAllInfo/2020-03-17");
            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();
            Log.d(TAG,"132"+responseCode);
            if(responseCode==HttpURLConnection.HTTP_OK) {
                InputStream inputStream=httpURLConnection.getInputStream();
                result = streamToString(inputStream);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        result=decode(result);
                        textView.setText(result);
                    }
                });


            }

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

    }

}
View Code

2、mainactivity1

package com.eb.yiqingdemo;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;
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.URL;
import java.util.ArrayList;
import java.util.List;

public class Main2Activity extends AppCompatActivity {

    private EditText editText1;
    private EditText editText2;
    private Button button;
   private String result;
   private WebView webView;
   private TextView textView;
    private  static  final  String TAG="MainActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
    editText1=findViewById(R.id.editText);
    editText2=findViewById(R.id.editText2);
    button=findViewById(R.id.button);
    textView=findViewById(R.id.textView);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        requestDataByGet();

                    }
                }).start();
            }
        });

    }

    public void requestDataByGet()
    {  if (editText1.getText().toString() != null && editText2.getText().toString() != null)
    {   try {

                URL url = new URL("http://117.50.96.227/EpidemicInfo/city/getInfoByCountry/" +editText2.getText().toString()+"/"+editText1.getText().toString());
                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<yiqingclass> list=new ArrayList<yiqingclass>();
                    Gson gson=new Gson();
                    Type cityType = new TypeToken<ArrayList<yiqingclass>>() {}.getType();

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

                    }
                    textView.setText(ziz);
                }
            });


                }

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

    }
    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;
        }
    }
}
View Code

3、yiqingclass

package com.eb.yiqingdemo;

public class yiqingclass {
    private int id;
    private String name;
    private String confirm;
    private String suspect;
    private String dead;

    public String getDead() {
        return dead;
    }

    public void setDead(String dead) {
        this.dead = dead;
    }

    private String heal;
    private String severe;
    private String idcode;
    private  String lastupdatetime;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getConfirm() {
        return confirm;
    }

    public yiqingclass(int id, String name, String confirm, String suspect, String heal, String severe, String idcode, String lastupdatetime) {
        this.id = id;
        this.name = name;
        this.confirm = confirm;
        this.suspect = suspect;

        this.heal = heal;
        this.severe = severe;
        this.idcode = idcode;
        this.lastupdatetime = lastupdatetime;
    }

    public void setConfirm(String confirm) {
        this.confirm = confirm;
    }

    @Override
    public String toString() {
        return "yiqingclass{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", confirm='" + confirm + '\'' +
                ", suspect='" + suspect + '\'' +
                ", heal='" + heal + '\'' +
                ", severe='" + severe + '\'' +
                ", idcode='" + idcode + '\'' +
                ", lastupdatetime='" + lastupdatetime + '\'' +
                '}';
    }

    public String getSuspect() {
        return suspect;
    }

    public void setSuspect(String suspect) {
        this.suspect = suspect;
    }

    public String getHeal() {
        return heal;
    }

    public void setHeal(String heal) {
        this.heal = heal;
    }

    public String getSevere() {
        return severe;
    }

    public void setSevere(String severe) {
        this.severe = severe;
    }

    public String getIdcode() {
        return idcode;
    }

    public void setIdcode(String idcode) {
        this.idcode = idcode;
    }

    public String getLastupdatetime() {
        return lastupdatetime;
    }

    public void setLastupdatetime(String lastupdatetime) {
        this.lastupdatetime = lastupdatetime;
    }
}
View Code

 

posted @ 2020-03-18 14:15  Mac_13  阅读(135)  评论(0编辑  收藏  举报