【问题标题】:Json Parser with volley library带有凌空库的 Json 解析器
【发布时间】:2023-03-26 09:25:01
【问题描述】:

我正在寻找解决方案,但无法解决。

那是我的 JSON

{“错误”:“”,“电影”:[{ "title": "人猿星球的黎明", "图像": "http://api.androidhive.info/json/movies/1.jpg", “评分”:8.3, “发布年份”:2014 年, “类型”:[“动作”、“戏剧”、“科幻”] }, { "title": "第 9 区", "图像": "http://api.androidhive.info/json/movies/2.jpg", “评分”:8, “发布年份”:2009 年, “类型”:[“动作”、“科幻”、“惊悚”] }, ... ... }]}

这是我的代码

// Movies json url
private static final String url = "https://d....url....";
private ProgressDialog pDialog;
private List<Movie> movieList = new ArrayList<Movie>();
private HorizontalListView listView;
private CustomListAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    listView  = (HorizontalListView) findViewById(R.id.list);

    adapter = new CustomListAdapter(this, movieList);
    listView.setAdapter(adapter);

    pDialog = new ProgressDialog(this);
    // Showing progress dialog before making http request
    pDialog.setMessage("Loading...");
    pDialog.show();

    // changing action bar color
    getActionBar().setBackgroundDrawable(
            new ColorDrawable(Color.parseColor("#1b1b1b")));

    // Creating volley request obj
    JsonArrayRequest movieReq = new JsonArrayRequest(url ,new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {
                    Log.d(TAG, response.toString());
                    hidePDialog();


                    // Parsing json
                    for (int i = 0; i < response.length(); i++) {
                        try {

                            JSONObject obj = response.getJSONObject(i).getJSONObject(TAG_MOVIE);
                            Movie movie = new Movie();
                            movie.setTitle(obj.getString("title"));
                            movie.setThumbnailUrl(obj.getString("image"));
                            movie.setRating(((Number) obj.get("rating"))
                                .doubleValue());
                            movie.setYear(obj.getInt("releaseYear"));

                             //Genre is json array
                            JSONArray genreArry = obj.getJSONArray("genre");
                            ArrayList<String> genre = new ArrayList<String>();
                            for (int j = 0; j < genreArry.length(); j++) {
                                genre.add((String) genreArry.get(j));
                            }
                            movie.setGenre(genre);

                            // adding movie to movies array
                            movieList.add(movie);

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }

                    // notifying list adapter about data changes
                    // so that it renders the list view with updated data
                    adapter.notifyDataSetChanged();
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    VolleyLog.d(TAG, "Error: " + error.getMessage());
                    hidePDialog();

                }
            });

    // Adding request to request queue
    AppController.getInstance().addToRequestQueue(movieReq);





}

@Override
public void onDestroy() {
    super.onDestroy();
    hidePDialog();
}

private void hidePDialog() {
    if (pDialog != null) {
        pDialog.dismiss();
        pDialog = null;
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

但结果只是一个白色的布局。我认为这是因为我的代码在 JSON 中找不到“Filme”变量,但我不知道如何修复它。

有什么想法吗??

谢谢!!

【问题讨论】:

  • 我已经使用了 JsonArrayRequest。

标签: android json android-volley


【解决方案1】:

“电影”不是 JSONObject,它实际上是一个 JSONArray。只需将其解析为 JSONArray :-)

编辑:

首先,使用 StringRequest,而不是 JSONArray,因为您收到的根不是 JSONArray。

然后,一旦你得到一个简单的字符串作为结果,做这样的事情:

        JSONObject root = new JSONObject(result);
        JSONArray films = root.getJSONArray("Filme");
        JSONObject obj;
        for (int i = 0; i < films.length(); i++) {
            obj = films.getJSONObject(i);

            //Put all the parsing for a single Movie JSONObject here
        }

【讨论】:

  • 感谢您的回答。我怎样才能用凌空抽射呢?
  • JSONArray jsonArrayFilme = (你的 json).getJSONArray("Filme");
  • 你的意思是像这样?? // 解析 json for (int i = 0; i Filme = new ArrayList(); for (int j = 0; j
  • 再想一想。如果我的 JSON 是这样的:{"Error":"","Filme":[{ "title": "Dawn of the Planet of the Apes", "image": "api.androidhive.info/json/movies/1.jpg", "rating": 8.3, "releaseYear": 2014, "Langtext": "fdfdfd

    fdfggfg", ... ... }]} "Langtext" 有点像 HTML。我能做什么?
【解决方案2】:

这是我在“Kelevandos”帮助下提出的解决方案。 也许它可以帮助某人。

// Request a string response from the provided URL.
StringRequest movieReq = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
//Log.d(TAG, response.toString());
hidePDialog();

JSONObject root;
try {
root = new JSONObject(response);
JSONArray films = root.getJSONArray("Filme");
JSONObject obj;
for (int i = 0; i < films.length(); i++) {
obj = films.getJSONObject(i);

//Put all the parsing for a single Movie JSONObject here

Movie movie = new Movie();
movie.setTitle(obj.getString("title"));
movie.setThumbnailUrl(obj.getString("image"));
movie.setRating(((Number) obj.get("rating"))
.doubleValue());
movie.setYear(obj.getInt("releaseYear"));

// Genre is json array
 JSONArray genreArry = obj.getJSONArray("genre");
ArrayList<String> genre = new ArrayList<String>();
for (int j = 0; j < genreArry.length(); j++) {
genre.add((String) genreArry.get(j));
}
movie.setGenre(genre);

// adding movie to movies array
movieList.add(movie);

【讨论】:

  • 再想一想。如果我的 JSON 是这样的: {"Error":"","Filme":[{ "title": "Dawn of the Planet of the Apes", "image": "api.androidhive.info/json/movies /1.jpg";, "rating": 8.3, "releaseYear": 2014, "Langtext": "fdfdfd

    fdfggfg", ... ... }]} "Langtext" 有有些像 HTML。我能做什么?
猜你喜欢
  • 2018-08-06
  • 2023-03-04
  • 2017-02-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多