首页 > 解决方案 > 执行 JSONArray JSONObject 后调试停止,无法执行 for 循环?

问题描述

我是新来的api,我刚刚尝试了一个简单的api调用示例http并尝试将其放入ListView,但它正在显示NULLPointerExecption,我调试代码并在它到达JSONArray对象创建后停止调试?为什么我真的不知道,我也尝试在gradle文件中设置 Shinking 选项 true/false 但什么也没发生......

我只将我的整个项目包含在一个项目中mainactivity。这是代码...

                JSONArray ja = new JSONArray(data);

                for (int i = 0; i < ja.length(); i++) {
                    JSONObject jo = (JSONObject) ja.get(i);

                    singleParsed = "\n" + "\n" + "NAME : " + jo.get("name") + "\n" +
                            "PASSWORD : " + jo.get("password") + "\n" +
                            "CONTACT : " + jo.get("contact") + "\n" +
                            "COUNTRY : " + jo.get("country") + "\n";

                    dataParsed = dataParsed + singleParsed + "\n";
                }

            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            }
            try {
                return ParseMoviesJsonString(dataParsed);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return null;
        }




        private ArrayList<User> ParseMoviesJsonString(String dataParsed) throws JSONException {

            final String json_name = "name";
            final String json_password = "password";
            final String json_contact = "contact";
            final String json_country = "country";

            ArrayList<User> list = new ArrayList<>();

            JSONObject MovieJson = new JSONObject(dataParsed);//MovieJson got the data and then skipps the followinglines and directly go to onPostExecute
            JSONArray MovieArray = MovieJson.getJSONArray(dataParsed);

            for (int i = 0; i < MovieArray.length(); i++) {

                JSONObject result_movie = MovieArray.getJSONObject(i);
                User movie = new User();
                movie.setU_name(result_movie.getString(json_name));
                movie.setU_password(result_movie.getString(json_password));
                movie.setU_contact(result_movie.getString(json_contact));
                movie.setU_country(result_movie.getString(json_country));

                list.add(movie);
            }
            return list;
        }

        @Override
        protected void onPostExecute(ArrayList<User> list) {// list : null
            super.onPostExecute(list);

            adapter.updateList(list);
            adapter.notifyDataSetChanged();
        }
    }
}

标签: androidarraysjsonfor-loopdebugging

解决方案


只需按照下面给出的编辑您的异步任务

   @Override
    protected ArrayList<User> doInBackground(String[] params) {

        try {
            URL url = new URL("https://api.myjson.com/bins/1422e4");

            HttpURLConnection hc = (HttpURLConnection) url.openConnection();
            InputStream is = hc.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(is));

            while (line != null) {
                line = br.readLine();
                data = data + line;
            }

        /*      JSONArray ja = new JSONArray(data);

          for (int i = 0; i < ja.length(); i++) {
                JSONObject jo = (JSONObject) ja.get(i);

                singleParsed = "\n" + "\n" + "NAME : " + jo.get("name") + "\n" +
                        "PASSWORD : " + jo.get("password") + "\n" +
                        "CONTACT : " + jo.get("contact") + "\n" +
                        "COUNTRY : " + jo.get("country") + "\n";

                dataParsed = dataParsed + singleParsed + "\n";
            }*/

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            return ParseMoviesJsonString(data);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }


    private ArrayList<User> ParseMoviesJsonString(String dataParsed) throws JSONException {

        final String json_name = "name";
        final String json_password = "password";
        final String json_contact = "contact";
        final String json_country = "country";

        ArrayList<User> list = new ArrayList<>();

        JSONArray MovieArray = new JSONArray(dataParsed);

        for (int i = 0; i < MovieArray.length(); i++) {

            JSONObject result_movie = MovieArray.getJSONObject(i);
            User movie = new User();
            movie.setU_name(result_movie.getString(json_name));
            movie.setU_password(result_movie.getString(json_password));
            movie.setU_contact(result_movie.getString(json_contact));
            movie.setU_country(result_movie.getString(json_country));

            list.add(movie);
        }
        return list;
    }

根据上述解决方案,您必须在 doinbackground 中注释代码,并在解析时进行一些更改。您也可以尝试使用 GSON 库,这是更好的方法。


推荐阅读