所以我试图利用改造来从维基百科中提取信息。 这些是具有相关改造代码的类:
Retrofit不会抛出任何异常,但
Log.i("RetrofitResponse", mResponse.toString());
表明返回的正文只包含null ...
你们有没有看到我做过的任何明显错误?
这是:
interface WikiService {
@GET("?format=json&action=query&prop=extracts&exintro=&explaintext=")
Call<WikiInfo> search(@Query("titles") String search);
}
表示这个uri的正确方法? :https://en.wikipedia.org/w/api.php?&action=query&prop=extracts&exintro=&explaintext=&titles=pizza&format=json
改造如何知道如何将json字段映射到我的WikiInfo对象字段?我给类中的字段命名与json响应中的键相同。
我将非常感谢任何回应!
提前多多谢谢。
祝你好运
答案 0 :(得分:2)
我认为你的pojos有问题。以下是通过改造从维基百科api获取数据的示例pojos。
编辑:主要包装类名为Result
public class Result {
@SerializedName("batchcomplete")
private String result;
@SerializedName("query")
private Query query;
}
查询类:
public class Query {
@SerializedName("pages")
private Map<String, Page> pages;
public Map<String, Page> getPages() {
return pages;
}
public void setPages(Map<String, Page> pages) {
this.pages = pages;
}
}
和Page
public class Page {
@SerializedName("pageid")
private long id;
@SerializedName("title")
private String title;
@SerializedName("extract")
private String content;
}
这是您的服务界面:
interface WikiService {
@GET("?format=json&action=query&prop=extracts&exintro=&explaintext=")
Call<Result> search(@Query("titles") String search);
}
基本上你需要一个包装类。维基百科的json响应标签需要与您的Page
pojo匹配才能更改。而且号码也可以改变。因此,您需要与Map
匹配才能获得Retrofit
的成功回复。
在这里,我的示例GitHub项目可以看到wikipedia api和retrofit的示例实现。
答案 1 :(得分:2)
所以,这是你得到的JSON。
{
"batchcomplete": "",
"query": {
"normalized": [{
"from": "pizza",
"to": "Pizza"
}],
"pages": {
"24768": {
"pageid": 24768,
"ns": 0,
"title": "Pizza",
"extract": "Pizza is a yeasted flatbread generally topped with tomato sauce and cheese and baked in an oven. It is commonly topped with a selection of meats, vegetables and condiments. The term was first recorded in the 10th century, in a Latin manuscript from Gaeta in Central Italy. The modern pizza was invented in Naples, Italy, and the dish and its variants have since become popular and common in many areas of the world.\nIn 2009, upon Italy's request, Neapolitan pizza was safeguarded in the European Union as a Traditional Speciality Guaranteed dish. The Associazione Verace Pizza Napoletana (the True Neapolitan Pizza Association) is a non-profit organization founded in 1984 with headquarters in Naples. It promotes and protects the \"true Neapolitan pizza\".\nPizza is sold fresh or frozen, either whole or in portions, and is a common fast food item in Europe and North America. Various types of ovens are used to cook them and many varieties exist. Several similar dishes are prepared from ingredients commonly used in pizza preparation, such as calzone and stromboli."
}
}
}
}
这是你的(精简的)Java类。
public class WikiInfo {
private String name;
private String extract;
}
Retrofit将JSON处理委托给您在此处设置的Gson。
private Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
在尝试知道您想要的JSON内容时遇到问题,并且它不能简单地知道您想要response["query"]["pages"]
,然后是页面标识#24768
,"title"
和{从那起{1}}。
所以,解决方案是
"extract"
课程进行一些研究。请参阅Gson文档以开始使用它,但具有WikiInfo
的对象将是一个良好的开端。然后Map<String, Page> page
包含Page.java