简单学习了一下Jsoup,但是发现爬不到所需的数据,偶然中发现了论坛大佬发布的API,于是使用API来获取数据。由于目前还不会制作表格,所以只对河北省的信息进行了可视化管理。
1、数据的获取
1、实体类的创建
根据API所返回的数据,设计如下实体类:
package com.example.fightvirus; import java.util.List; public class MyJson { private String provinceName; private String provinceShortName; private int confirmedCount; private int suspectedCount; private int curedCount; private int deadCount; private String comment; private int locationId; private List<City> cities; @Override public String toString() { return "MyJson{" + "provinceName='" + provinceName + '\'' + ", provinceShortName='" + provinceShortName + '\'' + ", confirmedCount=" + confirmedCount + ", suspectedCount=" + suspectedCount + ", curedCount=" + curedCount + ", deadCount=" + deadCount + ", comment='" + comment + '\'' + ", locationId=" + locationId + ", cities=" + cities + '}'; } public String getProvinceName() { return provinceName; } public void setProvinceName(String provinceName) { this.provinceName = provinceName; } public String getProvinceShortName() { return provinceShortName; } public void setProvinceShortName(String provinceShortName) { this.provinceShortName = provinceShortName; } public int getConfirmedCount() { return confirmedCount; } public void setConfirmedCount(int confirmedCount) { this.confirmedCount = confirmedCount; } public int getSuspectedCount() { return suspectedCount; } public void setSuspectedCount(int suspectedCount) { this.suspectedCount = suspectedCount; } public int getCuredCount() { return curedCount; } public void setCuredCount(int curedCount) { this.curedCount = curedCount; } public int getDeadCount() { return deadCount; } public void setDeadCount(int deadCount) { this.deadCount = deadCount; } public String getComment() { return comment; } public void setComment(String comment) { this.comment = comment; } public int getLocationId() { return locationId; } public void setLocationId(int locationId) { this.locationId = locationId; } public List<City> getCities() { return cities; } public void setCities(List<City> cities) { this.cities = cities; } } class City{ private String cityName; private int confirmedCount; private int suspectedCount; private int curedCount; private int deadCount; private int locationId; @Override public String toString() { return "City{" + "cityName='" + cityName + '\'' + ", confirmedCount=" + confirmedCount + ", suspectedCount=" + suspectedCount + ", curedCount=" + curedCount + ", deadCount=" + deadCount + ", locationId=" + locationId + '}'; } public String getCityName() { return cityName; } public void setCityName(String cityName) { this.cityName = cityName; } public int getConfirmedCount() { return confirmedCount; } public void setConfirmedCount(int confirmedCount) { this.confirmedCount = confirmedCount; } public int getSuspectedCount() { return suspectedCount; } public void setSuspectedCount(int suspectedCount) { this.suspectedCount = suspectedCount; } public int getCuredCount() { return curedCount; } public void setCuredCount(int curedCount) { this.curedCount = curedCount; } public int getDeadCount() { return deadCount; } public void setDeadCount(int deadCount) { this.deadCount = deadCount; } public int getLocationId() { return locationId; } public void setLocationId(int locationId) { this.locationId = locationId; } }
2、volley
使用volley来获取数据,再使用Gson来对数据进行反序列化。
-
依赖:
app中
implementation 'com.google.code.gson:gson:2.8.6' implementation 'com.android.volley:volley:1.1.1'
project中
allprojects { repositories { maven { url 'https://jitpack.io' } } }
-
简单使用
RequestQueue queue = Volley.newRequestQueue(getActivity()); StringRequest stringRequest = new StringRequest(StringRequest.Method.GET, "URL(API接口)", new Response.Listener<String>() { @Override public void onResponse(String response) { Gson gson = new Gson(); Type userListType = new TypeToken<ArrayList<MyJson>>(){}.getType(); List<MyJson> myJsonList =gson.fromJson(response,userListType); myJson = myJsonList.get(0); initMyView(myJson); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Log.e("myTAG", "onErrorResponse: " + error); } }); queue.add(stringRequest);
2、页面的布局
-
上方展示确诊人数、治愈人数、死亡人数。
-
下方将各市的信息通过柱状图的形式展示出来。
3、逻辑代码
package com.example.fightvirus; import android.graphics.Color; import android.os.Bundle; import androidx.annotation.Nullable; import androidx.fragment.app.Fragment; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import com.android.volley.RequestQueue; import com.android.volley.Response; import com.android.volley.VolleyError; import com.android.volley.toolbox.StringRequest; import com.android.volley.toolbox.Volley; import com.github.mikephil.charting.charts.BarChart; import com.github.mikephil.charting.components.Description; import com.github.mikephil.charting.components.XAxis; import com.github.mikephil.charting.components.YAxis; import com.github.mikephil.charting.data.BarData; import com.github.mikephil.charting.data.BarDataSet; import com.github.mikephil.charting.data.BarEntry; import com.github.mikephil.charting.formatter.IndexAxisValueFormatter; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; import java.lang.reflect.Type; import java.util.ArrayList; import java.util.List; /** * A simple {@link Fragment} subclass. */ public class HomeChatFragment extends Fragment { private MyJson myJson; private TextView confirmedCount, curedCount, deadCount; BarChart barChart; public HomeChatFragment() { // Required empty public constructor } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View inflate = inflater.inflate(R.layout.fragment_home_chat, container, false); barChart = inflate.findViewById(R.id.barChart); confirmedCount = inflate.findViewById(R.id.confirmedCount); curedCount = inflate.findViewById(R.id.curedCount); deadCount = inflate.findViewById(R.id.deadCount); return inflate; } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); RequestQueue queue = Volley.newRequestQueue(getActivity()); StringRequest stringRequest = new StringRequest(StringRequest.Method.GET, "http://49.232.173.220:3001/data/getAreaStat/河北", new Response.Listener<String>() { @Override public void onResponse(String response) { Gson gson = new Gson(); Type userListType = new TypeToken<ArrayList<MyJson>>() { }.getType(); List<MyJson> myJsonList = gson.fromJson(response, userListType); myJson = myJsonList.get(0); initMyView(myJson); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Log.e("myTAG", "onErrorResponse: " + error); } }); queue.add(stringRequest); } private void initMyView(MyJson myJson) { confirmedCount.setText(String.valueOf(myJson.getConfirmedCount())); curedCount.setText(String.valueOf(myJson.getCuredCount())); deadCount.setText(String.valueOf(myJson.getDeadCount())); List<City> cities = myJson.getCities(); //chart的设计 Description description = new Description(); description.setText("数据来源:丁香医生"); barChart.setDescription(description); barChart.setDrawBorders(false); barChart.setBackgroundColor(Color.WHITE); barChart.setDrawGridBackground(false); barChart.setDrawBarShadow(false); barChart.setHighlightFullBarEnabled(false); barChart.setDragEnabled(true); barChart.setNoDataText("正在加载数据..."); //X轴数据 ArrayList<BarEntry> entries_all = new ArrayList<>(); ArrayList<BarEntry> entries_cure = new ArrayList<>(); ArrayList<BarEntry> entries_dead = new ArrayList<>(); for (int i = 0; i < cities.size(); i++) { entries_all.add(new BarEntry(i, cities.get(i).getConfirmedCount())); entries_cure.add(new BarEntry(i, cities.get(i).getCuredCount())); entries_dead.add(new BarEntry(i, cities.get(i).getDeadCount())); } List<String> citiesName = new ArrayList<>(); for (int i = 0; i < cities.size(); i++) { citiesName.add(cities.get(i).getCityName()); } //X轴设计 XAxis xAxis = barChart.getXAxis(); xAxis.setAxisMinimum(0f); xAxis.setAxisMaximum(cities.size()); xAxis.setValueFormatter(new IndexAxisValueFormatter(citiesName)); xAxis.setPosition(XAxis.XAxisPosition.BOTTOM); xAxis.setDrawAxisLine(false); xAxis.setLabelCount(cities.size()); xAxis.setCenterAxisLabels(true); //Y轴的设计 YAxis rightY = barChart.getAxisRight(); YAxis leftY = barChart.getAxisLeft(); rightY.setEnabled(false); leftY.setEnabled(false); BarDataSet barDataSet_all = new BarDataSet(entries_all, "确诊人数"); barDataSet_all.setColor(Color.RED); BarDataSet barDataSet_cure = new BarDataSet(entries_cure, "治愈人数"); barDataSet_cure.setColor(Color.GREEN); BarDataSet barDataSet_dead = new BarDataSet(entries_dead, "死亡人数"); barDataSet_dead.setColor(Color.GRAY); BarData barData = new BarData(); barData.addDataSet(barDataSet_all); barData.addDataSet(barDataSet_cure); barData.addDataSet(barDataSet_dead); barData.setBarWidth(0.3f); barData.groupBars(0f, 0.1f, 0f); barChart.setData(barData); } }
4、展示效果
来源:https://www.cnblogs.com/wuren-best/p/12296811.html