【发布时间】:2016-10-01 16:34:01
【问题描述】:
我能够使用 OkHttp3 获取 JSON 响应,并且我想使用 Retrofit 解析响应以从中获取名称和图像。我查看了 Retrofit 网站和一些教程,但过程仍然不清楚。
这是我获取 JSON 响应的 OkHttp3 代码:
Request request = new Request.Builder().url(url).build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
e.printStackTrace();
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
Headers responseHeaders = response.headers();
for (int i = 0, size = responseHeaders.size(); i < size; i++) {
System.out.println(responseHeaders.name(i) + responseHeaders.value(i));
}
System.out.println(response.body().string());
String jData = response.body().string();// I want to parse jData using Retrofit
}
});
JSON 响应如下所示:
我想获取每个艺术家的姓名、ID 和图像,非常感谢任何帮助。
更新
我添加了 Pojo 类,其中之一是 Item 类:
public class Item {
@SerializedName("external_urls")
@Expose
private ExternalUrls externalUrls;
@SerializedName("followers")
@Expose
private Followers followers;
@SerializedName("genres")
@Expose
private List<Object> genres = new ArrayList<Object>();
@SerializedName("href")
@Expose
private String href;
@SerializedName("id")
@Expose
private String id;
@SerializedName("images")
@Expose
private List<Object> images = new ArrayList<Object>();
@SerializedName("name")
@Expose
private String name;
@SerializedName("popularity")
@Expose
private Integer popularity;
@SerializedName("type")
@Expose
private String type;
@SerializedName("uri")
@Expose
private String uri;
/**
*
* @return
* The externalUrls
*/
public ExternalUrls getExternalUrls() {
return externalUrls;
}
/**
*
* @param externalUrls
* The external_urls
*/
public void setExternalUrls(ExternalUrls externalUrls) {
this.externalUrls = externalUrls;
}
/**
*
* @return
* The followers
*/
public Followers getFollowers() {
return followers;
}
/**
*
* @param followers
* The followers
*/
public void setFollowers(Followers followers) {
this.followers = followers;
}
/**
*
* @return
* The genres
*/
public List<Object> getGenres() {
return genres;
}
/**
*
* @param genres
* The genres
*/
public void setGenres(List<Object> genres) {
this.genres = genres;
}
/**
*
* @return
* The href
*/
public String getHref() {
return href;
}
/**
*
* @param href
* The href
*/
public void setHref(String href) {
this.href = href;
}
/**
*
* @return
* The id
*/
public String getId() {
return id;
}
/**
*
* @param id
* The id
*/
public void setId(String id) {
this.id = id;
}
/**
*
* @return
* The images
*/
public List<Object> getImages() {
return images;
}
/**
*
* @param images
* The images
*/
public void setImages(List<Object> images) {
this.images = images;
}
/**
*
* @return
* The name
*/
public String getName() {
return name;
}
/**
*
* @param name
* The name
*/
public void setName(String name) {
this.name = name;
}
/**
*
* @return
* The popularity
*/
public Integer getPopularity() {
return popularity;
}
/**
*
* @param popularity
* The popularity
*/
public void setPopularity(Integer popularity) {
this.popularity = popularity;
}
/**
*
* @return
* The type
*/
public String getType() {
return type;
}
/**
*
* @param type
* The type
*/
public void setType(String type) {
this.type = type;
}
/**
*
* @return
* The uri
*/
public String getUri() {
return uri;
}
/**
*
* @param uri
* The uri
*/
public void setUri(String uri) {
this.uri = uri;
}
}
以下是我在活动中使用 Retrofit 的方式:
private void loadJSON() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://api.spotify.com")
.addConverterFactory(GsonConverterFactory.create())
.build();
final Artists_Interface request = retrofit.create(Artists_Interface.class);
Call<Item> call = request.getArtists();
call.enqueue(new Callback<Item>() {
@Override
public void onResponse(Call<Item> call, Response<Item> response) {
if(response.isSuccessful()){
Item artist = response.body();
System.out.println("THE NAME::::. : " + artist.getName());
}
else{
System.out.println(" :::. NOO RESPONSE .::: " );
}
}
@Override
public void onFailure(Call<Item> call, Throwable t) {
System.out.println("onFAIL::: " + t);
}
});
这里是改造界面的样子:
public interface Artists_Interface {
@GET("/v1/search?q=Beyonce&type=artist")
Call<Item> getArtists();
}
我得到 artist.getName() 等于 null。我需要进入 JSON 正文中“项目”内的名称、ID 和图像,并将它们传递给 listView 或 recyclerView 适配器
【问题讨论】:
-
Response类是否属于改造包? -
这是一个 OkHttp3 响应
-
那么问题出在哪里?你有 JSON,只需手动解析或使用 GSON 解析即可。
-
我需要使用 Retrofit 来提取数据
标签: android json retrofit okhttp3