如何创建新冠肺炎追踪器安卓应用

原文:https://www . geeksforgeeks . org/如何创建-新冠肺炎-追踪器-安卓-app/

先决条件:

世界正面临最严重的流行病之一,新冠肺炎的爆发,你们都知道这一点。因此,在封锁期间,让我们使用 REST API 创建一个新冠肺炎追踪器安卓应用程序,它将只追踪全球统计数据

  1. 第一步:开启新项目
    • 打开一个新项目,只需点击左上角的文件选项。
    • 然后点击新建,用你想要的名字打开一个新项目。
    • 现在我们将使用 Java 语言来处理空活动。保持所有其他选项不变。
    • 您可以根据自己的选择更改项目名称。
    • 默认情况下会有两个文件 activity_main.xmlMainActivity.java
  2. 第二步:在进入编码部分之前,你首先要做一些前置任务。

    • 转到应用->RES->values->colors . XML部分,为您的应用设置颜色。

      ```java <?xml version="1.0" encoding="utf-8"?>     #42C14B     #3BC545     #05af9b

      #fb7268     #ededf2

      ```

    • 转到梯度脚本- >构建.梯度(模块:应用)部分,导入以下依赖项,然后单击上面弹出的“立即同步”。

      build . gradle(:app)

      java // Volley library implementation 'com.android.volley:volley:1.1.1'

    • 转到 app- >清单- > AndroidManifests.xml 部分,允许互联网权限

      java <!--Allow Internet Permission--> <uses-permission android:name="android.permission.INTERNET" />

  3. 第三步:设计用户界面

    • 下面是 xml 文件的代码。

      activity _ main . XML

      ```java <?xml version="1.0" encoding="utf-8"?> ScrollView     xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:app="http://schemas.android.com/apk/res-auto"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:background="@color/color_white"     android:orientation="vertical"     tools:context=".MainActivity"

          

              

                  

                  <TextView                 android:layout_width="match_parent"                 android:layout_height="wrap_content"                 android:text="0"                 android:id="@+id/tvCases"                 android:textAlignment="center"                 android:textSize="25sp"                 android:textColor="@color/color_one"                 android:textStyle="bold"                 android:fontFamily="sans-serif-light" />

              

              <TextView             android:layout_width="match_parent"             android:layout_height="wrap_content"             android:text="0"             android:id="@+id/tvRecovered"             android:textAlignment="center"             android:textSize="25sp"             android:textColor="@color/color_one"             android:textStyle="bold"             android:fontFamily="sans-serif-light" />

              

              <TextView             android:layout_width="match_parent"             android:layout_height="wrap_content"             android:text="0"             android:id="@+id/tvCritical"             android:textAlignment="center"             android:textSize="25sp"             android:textColor="@color/color_one"             android:textStyle="bold"             android:fontFamily="sans-serif-light" />

              

              <TextView             android:layout_width="match_parent"             android:layout_height="wrap_content"             android:text="0"             android:id="@+id/tvActive"             android:textAlignment="center"             android:textSize="25sp"             android:textColor="@color/color_one"             android:textStyle="bold"             android:fontFamily="sans-serif-light" />

              

              <TextView             android:layout_width="match_parent"             android:layout_height="wrap_content"             android:text="0"             android:id="@+id/tvTodayCases"             android:textAlignment="center"             android:textSize="25sp"             android:textColor="@color/color_one"             android:textStyle="bold"             android:fontFamily="sans-serif-light" />

              

              <TextView             android:layout_width="match_parent"             android:layout_height="wrap_content"             android:text="0"             android:id="@+id/tvTotalDeaths"             android:textAlignment="center"             android:textSize="25sp"             android:textColor="@color/color_one"             android:textStyle="bold"             android:fontFamily="sans-serif-light" />

              

              <TextView             android:layout_width="match_parent"             android:layout_height="wrap_content"             android:text="0"             android:id="@+id/tvTodayDeaths"             android:textAlignment="center"             android:textSize="25sp"             android:textColor="@color/color_one"             android:textStyle="bold"             android:fontFamily="sans-serif-light" />

              

              <TextView             android:layout_width="match_parent"             android:layout_height="wrap_content"             android:text="0"             android:id="@+id/tvAffectedCountries"             android:textAlignment="center"             android:textSize="25sp"             android:textColor="@color/color_one"             android:textStyle="bold"             android:fontFamily="sans-serif-light" />

      ```

    • After using this code in .xml file, the UI will be like:

  4. Step4: Working with Java file

    • 打开MainActivity.java文件里面的类,首先创建的对象文本视图类。

      //创建文本视图类的对象 文本查看电视病例、电视恢复、电视关键、电视活动、电视今日活动、电视今日活动、电视今日活动、电视受影响国家/地区

    • 其次,在onCreate()方法中,我们必须将这些对象与我们已经给出的它们各自的 id 联系起来。XML 文件。

      //将这些对象与我们给出的它们各自的身份证明(identification)联系起来 XML .文件 电视病例= findweiwbyid(r . id。电视病例); tvRecovered = findweiwbyid(r . id。tvRecovered); tvCritical = findweiwbyid(r . id。tvCritical); tvActive = findweiwbyid(r . id。tvActive); 今日电视频道= findwiewbyid(r . id。tvtoday ses); tvtotaldeals = findwewebyid(r . id。tvtotaldeals); tvtodaydahts = findwewibyid(r . id。tvtodaydahts); TV effected counters = findweiwbyid(r . id。tveffected 计数器);

    • onCreate()方法之外创建一个private void fetchdata()方法并定义。

    • fetchdata()方法中,最重要的任务是如何从第三方应用编程接口获取数据并在我们的应用程序中实现它。我的要求是请通读安卓的凌空库和 REST API(简介)两篇文章,了解以下概念。
    • 使用截击库创建字符串请求,并用https://corona.lmao.ninja/v2/all链接分配“网址”。

      ```java // Create a String request using Volley Library

      String url = "https:// corona.lmao.ninja/v2/all";

      StringRequest request     = new StringRequest(         Request.Method.GET,         url,         new Response.Listener() {             @Override             public void onResponse(                 String response)             {             }         },         new Response.ErrorListener() {             @Override             public void onErrorResponse(                 VolleyError error)             {             }         });

      RequestQueue requestQueue     = Volley.newRequestQueue(this); requestQueue.add(request); ```

    • Please refer this website to take a look at the requested data are in JSON format.

    • 所以接下来你要做的是,在onResponse()方法内部创建“JSONObject”类的对象,然后在文本视图中设置数据,这些数据在“JSON object”的帮助下以 JSON 格式提供。确保你必须在一个“尝试”区内做这些事情。记住getString()内的参数必须与 JSON 格式给出的名称匹配。

      ```java // Handle the JSON object and handle it inside try and catch

      try {     // Creating object of JSONObject     JSONObject jsonObject         = new JSONObject(             response.toString());

      // Set the data in text view     // which are available in JSON format     // Note that the parameter     // inside the getString() must match     // with the name given in JSON format     tvCases.setText(         jsonObject.getString("cases"));     tvRecovered.setText(         jsonObject.getString("recovered"));     tvCritical.setText(         jsonObject.getString("critical"));     tvActive.setText(         jsonObject.getString("active"));     tvTodayCases.setText(         jsonObject.getString("todayCases"));     tvTotalDeaths.setText(         jsonObject.getString("deaths"));     tvTodayDeaths.setText(         jsonObject.getString("todayDeaths"));     tvAffectedCountries.setText(         jsonObject.getString("affectedCountries")); } catch (JSONException e) {     e.printStackTrace(); } ```

    • onErrorResponse()方法中,如果出现任何错误,您必须显示祝酒信息

      ```java Toast.makeText(MainActivity.this, error.getMessage(), Toast.LENGTH_SHORT) .show();

      ```

    • 最后调用onCreate()方法里面的fetchdata()方法。

    以下是 MainActivity.java 文件的完整代码:

    MainActivity.java

    ```java package com.example.covid_19tracker;

    import androidx.appcompat.app.AppCompatActivity;

    import android.os.Bundle; import android.widget.TextView; import android.widget.Toast;

    import com.android.volley.Request; 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 org.json.JSONException; import org.json.JSONObject;

    public class MainActivity     extends AppCompatActivity {

    // Create the object of TextView     TextView tvCases, tvRecovered,         tvCritical, tvActive,         tvTodayCases, tvTotalDeaths,         tvTodayDeaths,         tvAffectedCountries;

    @Override     protected void onCreate(Bundle savedInstanceState)     {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);

    // Link those objects with their respective id's         // that we have given in .XML file         tvCases             = findViewById(R.id.tvCases);         tvRecovered             = findViewById(R.id.tvRecovered);         tvCritical             = findViewById(R.id.tvCritical);         tvActive             = findViewById(R.id.tvActive);         tvTodayCases             = findViewById(R.id.tvTodayCases);         tvTotalDeaths             = findViewById(R.id.tvTotalDeaths);         tvTodayDeaths             = findViewById(R.id.tvTodayDeaths);         tvAffectedCountries             = findViewById(R.id.tvAffectedCountries);

    // Creating a method fetchdata()         fetchdata();     }

    private void fetchdata()     {

    // Create a String request         // using Volley Library         String url = "https:// corona.lmao.ninja/v2/all";

    StringRequest request             = new StringRequest(                 Request.Method.GET,                 url,                 new Response.Listener() {                     @Override                     public void onResponse(String response)                     {

    // Handle the JSON object and                         // handle it inside try and catch                         try {

    // Creating object of JSONObject                             JSONObject jsonObject                                 = new JSONObject(                                     response.toString());

    // Set the data in text view                             // which are available in JSON format                             // Note that the parameter inside                             // the getString() must match                             // with the name given in JSON format                             tvCases.setText(                                 jsonObject.getString(                                     "cases"));                             tvRecovered.setText(                                 jsonObject.getString(                                     "recovered"));                             tvCritical.setText(                                 jsonObject.getString(                                     "critical"));                             tvActive.setText(                                 jsonObject.getString(                                     "active"));                             tvTodayCases.setText(                                 jsonObject.getString(                                     "todayCases"));                             tvTotalDeaths.setText(                                 jsonObject.getString(                                     "deaths"));                             tvTodayDeaths.setText(                                 jsonObject.getString(                                     "todayDeaths"));                             tvAffectedCountries.setText(                                 jsonObject.getString(                                     "affectedCountries"));                         }                         catch (JSONException e) {                             e.printStackTrace();                         }                     }                 },                 new Response.ErrorListener() {                     @Override                     public void onErrorResponse(VolleyError error)                     {                         Toast.makeText(                                  MainActivity.this,                                  error.getMessage(),                                  Toast.LENGTH_SHORT)                             .show();                     }                 });

    RequestQueue requestQueue             = Volley.newRequestQueue(this);         requestQueue.add(request);     } } ```

输出: