gpt4 book ai didi

android - 如何使用 Volley 获取和解析 JSON 对象

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:34:11 24 4
gpt4 key购买 nike

我没能找到这个问题的详细答案,或者至少找不到我能理解的答案。

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

例如,这里是 iTunes JSON 对象 URL

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

所以我在这里设置了我的代码来获取这个对象(当然使用教程)

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 对象?但是我该如何访问和解析它呢?

最佳答案

根据您的 Url,您可以使用以下示例代码:

        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 类:

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 文件中:

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

希望这对您有所帮助!

关于android - 如何使用 Volley 获取和解析 JSON 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33927754/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com