com.android.volley.ParseError:org.json.JSONException:

时间:2019-03-18 11:44:26

标签: android

我正在尝试从themovidedb api获取电视节目列表,并且不断出现以下错误,这是我是android新手,这是我第一次使用api,请告诉我是否需要提供更多代码。感谢您的帮助。

请注意,我注意到目前仍在尝试使用此信息填充recyclerview。

错误

com.android.volley.ParseError: org.json.JSONException: Value {"page":1,"total_results":20000,"total_pages":1000,"results":[{"original_name":"Doom Patrol"....

我的代码

@Override
    protected Void doInBackground(Void... voids) {
        popularTvShows = "https://api.themoviedb.org/3/tv/popular?api_key=****my api key goes here****";
        popularList = new ArrayList<>();

        RequestQueue requestQueue = Volley.newRequestQueue(Objects.requireNonNull(getActivity()));

        JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(
                Request.Method.GET,
                popularTvShows,
                null,
                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {

                        try {

                            for (int i = 0; i < response.length(); i++) {
                                JSONObject jsonObject = response.getJSONObject(i);
                                Series series = new Series();
                                series.setId(jsonObject.getInt("id"));
                                series.setVoteAverage(jsonObject.getInt("vote_average"));
                                series.setVoteCount(jsonObject.getInt("vote_count"));
                                series.setOriginalTitle(jsonObject.getString("original_title"));
                                series.setTitle(jsonObject.getString("title"));
                                series.setPopularity(jsonObject.getDouble("popularity"));
                                series.setOverview(jsonObject.getString("overview"));
                                series.setReleaseDate(jsonObject.getString("release_date"));
                                series.setPosterPath(jsonObject.getString("poster_path"));

                                popularList.add(series);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                },

                new Response.ErrorListener() {
                    @SuppressLint("ShowToast")
                    @Override
                    public void onErrorResponse(VolleyError error) {

                        Log.e("gggg", error.toString());

                        if (error instanceof TimeoutError || error instanceof NoConnectionError) {
                            //This indicates that the reuest has either time out or there is no connection
                            Toast.makeText(getActivity(),"Check your internet and try again!", Toast.LENGTH_LONG);

                        } else if (error instanceof AuthFailureError) {
                            //Error indicating that there was an Authentication Failure while performing the request
                            Toast.makeText(getActivity(), "Authentication failure!", Toast.LENGTH_LONG);

                        } else if (error instanceof ServerError) {
                            //Indicates that the server responded with a error response
                            Toast.makeText(getActivity(), "Server error! Try again later", Toast.LENGTH_LONG);

                        } else if (error instanceof NetworkError) {
                            //Indicates that there was network error while performing the request
                            Toast.makeText(getActivity(), "Network error", Toast.LENGTH_LONG);

                        } else if (error instanceof ParseError) {
                            // Indicates that the server response could not be parsed
                            Toast.makeText(getActivity(), "Parse Error", Toast.LENGTH_LONG);
                        }
                    }
                }
        );
        requestQueue.add(jsonArrayRequest);
        return null;
    }

1 个答案:

答案 0 :(得分:0)

根据您的JSONObject,您的回复来自JSONArray,而不是Exception

com.android.volley.ParseError: org.json.JSONException: Value {"page":1,"total_results":20000,"total_pages":1000,"results":[{"original_name":"Doom Patrol"...

执行此操作:

@Override
protected Void doInBackground(Void... voids) {
    popularTvShows = "https://api.themoviedb.org/3/tv/popular?api_key=****my api key goes here****";
    popularList = new ArrayList<>();

    RequestQueue requestQueue = Volley.newRequestQueue(Objects.requireNonNull(getActivity()));

    JSONObjectRequest jsonObjectRequest = new JSONObjectRequest(
            Request.Method.GET,
            popularTvShows,
            null,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {

                  JSONObject json = new JSONObject(response);
                  JSONArray results = json.optJSONArray("results");

                    try {

                        for (int i = 0; i < results.length(); i++) {
                            JSONObject jsonObject = results.getJSONObject(i);
                            Series series = new Series();
                            .
                            .
                            .
                            popularList.add(series);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            },

            new Response.ErrorListener() {
                @SuppressLint("ShowToast")
                @Override
                public void onErrorResponse(VolleyError error) {

                    Log.e("gggg", error.toString());

                    if (error instanceof TimeoutError || error instanceof NoConnectionError) {
                        //This indicates that the reuest has either time out or there is no connection
                        Toast.makeText(getActivity(),"Check your internet and try again!", Toast.LENGTH_LONG);

                    } else if (error instanceof AuthFailureError) {
                        //Error indicating that there was an Authentication Failure while performing the request
                        Toast.makeText(getActivity(), "Authentication failure!", Toast.LENGTH_LONG);

                    } else if (error instanceof ServerError) {
                        //Indicates that the server responded with a error response
                        Toast.makeText(getActivity(), "Server error! Try again later", Toast.LENGTH_LONG);

                    } else if (error instanceof NetworkError) {
                        //Indicates that there was network error while performing the request
                        Toast.makeText(getActivity(), "Network error", Toast.LENGTH_LONG);

                    } else if (error instanceof ParseError) {
                        // Indicates that the server response could not be parsed
                        Toast.makeText(getActivity(), "Parse Error", Toast.LENGTH_LONG);
                    }
                }
            }
    );
    requestQueue.add(jsonObjectRequest);
    return null;
}