我一直无法找到这个问题的详细答案,或者至少没有一个我能理解的答案。
我正在尝试设置 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'
希望这可以帮助!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)