Как передавать данные после синтаксического анализа json объектов - PullRequest
0 голосов
/ 02 августа 2020

Я пытаюсь создать приложение, которое будет систематизировать информацию о COVID-19 (#death, #inspection, #patiens ... et c). Я нашел веб-сайт с открытым исходным кодом, который позволяет мне использовать данные JSON. Итак, я их проанализировал; ОДНАКО, я не могу передавать данные в mainActivity, поэтому я могу создавать графики и производить вычисления вне метода private jsonParseRequest().

Это весь метод jsonParseRequest, поэтому я могу запросить json и проанализировать его. .

Что я пробовал:

  • Я пытался заставить метод возвращать данные json в виде строки, используя Integer.toString() (int ndeaths, int ninspections, int npatients)

  • Создание arrayList и добавление этих данных, но когда я пытаюсь получить доступ, появляется сообщение об ошибке IndexOutOfBounds.

    private void jsonParseRequest(){
        final TextView textView = (TextView) findViewById(R.id.textView);
        String url = "https://www.stopcovid19.jp/data/covid19japan.json";
        RequestQueue queue = Volley.newRequestQueue(this);
    
        final JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                try {
                    //Input the jsonObject to String called data
                    String data = response.toString();
                    //Create jsonObject and input the string data inside, so now the string data is converted to jsonObject?
    
                    JSONObject jsonObject = new JSONObject(data);
                    //Use json data to extract the data using the key such as "ndeaths"
                    int ndeaths = jsonObject.getInt("ndeaths");
                    int ninspections = jsonObject.getInt("ninspections");
                    int npatients = jsonObject.getInt("npatients");
                    String lastUpdate = jsonObject.getString("lastUpdate");
    
                    //the textView has to be converted to String ot be able to display!!
                    textView.setText("Last Update: " + lastUpdate + "\n" + "Number of deaths: " + Integer.toString(ndeaths) + "\n" + "Number of Inspection: " + Integer.toString(ninspections) + "\n" + "Number of Positives: " + Integer.toString(npatients));
    
    
    
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        });
        queue.add(jsonObjectRequest);
    }
    
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...