【问题标题】:Android Retrofit troubles安卓改造烦恼
【发布时间】:2016-12-08 06:47:56
【问题描述】:

所以我试图利用改造从维基百科中提取信息。 这些是具有相关改造代码的类:

https://github.com/smholsen/whatIsThisThing/blob/master/app/src/main/java/com/simon/whatisthisthing/RetrofitBuilder.java

https://github.com/smholsen/whatIsThisThing/blob/master/app/src/main/java/com/simon/whatisthisthing/WikiInfo.java

https://github.com/smholsen/whatIsThisThing/blob/master/app/src/main/java/com/simon/whatisthisthing/WikiService.java

Retrofit 不会抛出任何异常,但是

Log.i("RetrofitResponse", mResponse.toString());

在onResponse回调方法中显示返回的body只包含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 响应中的键相同的名称。

我将非常感谢任何回应!

提前非常感谢。

最好的问候

【问题讨论】:

  • 改造如何知道如何将 json 字段映射到我的 WikiInfo 对象字段——它将其委托给 Gson。你应该相应地注释你的java对象类
  • 您在 mResponse 被填充之前就返回了它。您需要了解调用是异步的,这就是回调的重点
  • 对!不错的收获:) 我明白了。但目前的问题是收到的 response.body() 不包含我所期望的(请参阅接受答案中的 cmets)

标签: java android retrofit


【解决方案1】:

我认为你的 pojo 有问题。这是一个通过改造从维基百科 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;
    }
}

还有页面

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);
}

基本上你需要一个包装类。您需要与Page pojo 匹配的维基百科的 json 响应标签可以更改。并且数字也可以更改。因此,您需要匹配Map 才能获得Retrofit 的成功响应。

这是我的示例 GitHub 项目,您可以看到使用 wikipedia api 和改造的示例实现。

https://github.com/savepopulation/wikilight

【讨论】:

  • 希望没关系 - 我在回答中使用了你的回购。另外,我认为 OP 不需要 RealmObjects
  • 是的,在您的情况下,领域对象是不必要的。你可以随心所欲地使用我的仓库。
  • @cricket_007 每当我在本地数据源中看到realm.copyFromRealm() 时,我仍然会畏缩,但没关系
  • 您可以只复制以 Query 和 Page 命名的 pojos,并删除用于领域的不必要字段。如果你接受我的回答,我会很高兴。
  • @EpicPandaForce 不确定您指的是什么。我不是“使用回购”,只是在我的回答中引用。
【解决方案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"],然后是页面 ID #24768,以及 "title""extract"


所以,解决办法是要么

  1. 不要重新发明轮子。为 Wikipedia 查找合理的 Java API 或查看现有的 Retrofit Wikipedia 示例是否存在。 (例如,参见WikiLight
  2. 继续现有的,但对如何正确实现WikiInfo 类进行一些研究。请参阅 Gson 文档以开始使用它,但带有 Map&lt;String, Page&gt; page 的对象将是一个好的开始。那么Page.java 包含private String title, extract;

【讨论】:

  • 感谢您的精彩回复!什么样的对象会在这里“查询”?它会是一个包含两个数组列表的数组列表吗? (标准化和页面)?您对我在哪里可以找到有关如何正确实现 WikiInfo 类的一些信息有任何提示吗? :)
  • 我认为另一个答案已经涵盖了。
猜你喜欢
  • 1970-01-01
  • 2016-12-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-27
  • 1970-01-01
  • 1970-01-01
  • 2017-02-13
相关资源
最近更新 更多