如何使用Volley获取和解析JSON对象 [英] How to get and parse a JSON-object with Volley

查看:132
本文介绍了如何使用Volley获取和解析JSON对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法找到该问题的详细答案,或者至少找不到我能理解的答案.

I haven't been able to find a detailed answer to this question, or at least not one that I can understand.

我正在尝试设置Volley以从iTunes中提取JSON对象.然后,我想解析对象,以获取它们的图像URL.

I'm trying to set up Volley to pull down JSON-objects from iTunes. I then want to parse the objects, to get their image URLs.

例如,这是iTunes JSON对象URL

So for example, here is am iTunes JSON object URL

String url = "https://itunes.apple.com/search?term=michael+jackson";

因此,在这里,我已经设置了获取该对象的代码(使用教程)

So here I've set up my code to get this object (using a tutorial of course)

String url = "https://itunes.apple.com/search?term=michael+jackson";

JsonObjectRequest jsonRequest = new JsonObjectRequest
        (Request.Method.GET, url, null, new Downloader.Response.Listener // Cannot resolve symbol Listener
                <JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                // the response is already constructed as a JSONObject!
                try {
                    response = response.getJSONObject("args");
                    String site = response.getString("site"),
                            network = response.getString("network");
                    System.out.println("Site: "+site+"\nNetwork: "+network);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Downloader.Response.ErrorListener // Cannot resolve symbol ErrorListener
                () {

            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        });

Volley.newRequestQueue(this).add(jsonRequest);

最后一个语句是

Volley.newRequestQueue(this).add(jsonRequest);

大概,我现在有了JSON对象?但是我该如何访问和解析呢?

Presumably, I now have the JSON-object? But how can I access and parse it?

推荐答案

使用您的网址,您可以使用以下示例代码:

With your Url, you can use the following sample code:

        RequestQueue requestQueue = Volley.newRequestQueue(this);
        String url = "https://itunes.apple.com/search?term=michael+jackson";
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(url, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                if (response != null) {
                    int resultCount = response.optInt("resultCount");
                    if (resultCount > 0) {
                        Gson gson = new Gson();
                        JSONArray jsonArray = response.optJSONArray("results");
                        if (jsonArray != null) {
                            SongInfo[] songs = gson.fromJson(jsonArray.toString(), SongInfo[].class);
                            if (songs != null && songs.length > 0) {
                                for (SongInfo song : songs) {
                                    Log.i("LOG", song.trackViewUrl);
                                }
                            }
                        }
                    }
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("LOG", error.toString());
            }
        });
        requestQueue.add(jsonObjectRequest);

SongInfo类:

The SongInfo class:

public class SongInfo {
    public String wrapperType;
    public String kind;
    public Integer artistId;
    public Integer collectionId;
    public Integer trackId;
    public String artistName;
    public String collectionName;
    public String trackName;
    public String collectionCensoredName;
    public String trackCensoredName;
    public String artistViewUrl;
    public String collectionViewUrl;
    public String trackViewUrl;
    public String previewUrl;
    public String artworkUrl30;
    public String artworkUrl60;
    public String artworkUrl100;
    public Float collectionPrice;
    public Float trackPrice;
    public String releaseDate;
    public String collectionExplicitness;
    public String trackExplicitness;
    public Integer discCount;
    public Integer discNumber;
    public Integer trackCount;
    public Integer trackNumber;
    public Integer trackTimeMillis;
    public String country;
    public String currency;
    public String primaryGenreName;
    public String radioStationUrl;
    public Boolean isStreamable;
}

内部build.gradle文件:

Inside build.gradle file:

compile 'com.mcxiaoke.volley:library:1.0.19'
compile 'com.google.code.gson:gson:2.5'

希望这会有所帮助!

这篇关于如何使用Volley获取和解析JSON对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆