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

问题描述 投票:1回答:1

我正在尝试从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;
    }
android
1个回答
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;
}
© www.soinside.com 2019 - 2024. All rights reserved.