用Volley提取整数

时间:2019-03-26 17:14:02

标签: java android json rest android-volley

Json image link我正在尝试使用Volley从Json Object中提取一个整数,但似乎无法返回该值。我试图将其提取为字符串(适用于双精度值),但不适用于我的int值。 Json响应和我的代码如下。这是我正在尝试检索的“ id”,并将其用作构建另一个网络调用的URL的一部分。

[json image link][1]

// parse Json using volley to make network call
private String parseMovieJSON(final String url) {

    JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {

                    try {
                        JSONArray jsonArray = response.getJSONArray("results");

                        mMoviesList.clear();


                        for (int i = 0; i < jsonArray.length(); i++) {
                            JSONObject movie = jsonArray.getJSONObject(i);

                            //Get json data as strings
                            int id = movie.optInt("id");
                            String posterPath = movie.optString("poster_path");
                            String originalTitle = movie.optString("title");
                            String overview = movie.getString("overview");
                            String releaseDate = movie.optString("release_date");
                            String voteAverage = movie.optString("vote_average");


                            mMoviesList.add(new Movies(id, posterPath, originalTitle, overview, releaseDate, voteAverage));

                        }


                        myMoviesAdapter = new MyMoviesAdapter(MainActivity.this, mMoviesList);
                        myMoviesAdapter.setOnItemClickListener(MainActivity.this);
                        mRecyclerView.setAdapter(myMoviesAdapter);
                        myMoviesAdapter.notifyDataSetChanged();


                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError volleyError) {
            if (volleyError instanceof NetworkError) {

                if (checkConnection()) {
                    parseMovieJSON(url);

                } else if (!checkConnection()) {

                    Toast.makeText(MainActivity.this, "Check Network Connection", Toast.LENGTH_LONG).show();
                    mMoviesList = null;

                }


            }

        }
    });

    mRequestQueue.add(request);
    checkConnection();

    return url;
}


  // network call for Trailers
private void extractTrailer(String s) {


    JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, uriBuilder(), null,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {


                    try {
                        JSONArray jsonArray = response.getJSONArray("results");


                        for (int i = 0; i < jsonArray.length(); i++) {
                            JSONObject results = jsonArray.getJSONObject(i);

                            //Get json data as strings
                            int idNumber = results.optInt(KEY_ID);
                            String movieKey = results.optString(KEY_URL);
                            String movieName = results.optString(KEY_NAME);

                        }


                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError volleyError) {

        }
    });

    mRequestQueue.add(request);
}

private String uriBuilder() {

    // http://api.themoviedb.org/3/movie/157336/videos?api_key=### (template example)

    Uri.Builder builder = Uri.parse(Constants.BASE_URL).buildUpon();
    builder.appendPath("movie").
            appendPath(String.valueOf(Constants.MOVIE_ID)).
            appendPath("videos").
            appendQueryParameter("api_key", BuildConfig.ApiKey);

    return builder.build().toString();

}

1 个答案:

答案 0 :(得分:0)

我环顾四周,发现了一些我不知道的关于id属性的东西。这里有一些链接:

SO id

json id

我不知道您是控制还是仅使用json数据,但是这些链接可能解释了我们所看到的。如果使用,则该字段为字符串。