【问题标题】:retrofit 2 and xml at android在 android 上改造 2 和 xml
【发布时间】:2018-01-17 16:24:32
【问题描述】:

我必须创建什么类来接收一个 XML 元素?

我正在从 API 接收 XML:

<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">cat</string>

我有课:

@Root(strict = false)
public class Translation {

    @Element(name = "string")
    private String string;

    public String getString(){
        return string;
    }
    public Translation() {

    }
}

并发现错误:

org.simpleframework.xml.core.ValueRequiredException: Unable to satisfy @org.simpleframework.xml.Element(data=false, name=string, required=true, type=void) on field 'string' private java.lang.String com.antonioleiva.mvpexample.app.main.Utils.Network.Translation.Translation.string for class com.antonioleiva.mvpexample.app.main.Utils.Network.Translation.Translation at line 1

【问题讨论】:

标签: java android xml retrofit


【解决方案1】:

如果你习惯了改造,那么将 xml 转换为 json。

 public class Main {

public static int PRETTY_PRINT_INDENT_FACTOR = 4;
public static String TEST_XML_STRING =
    "<?xml version=\"1.0\" ?><test attrib=\"moretest\">Turn this to JSON</test>";

public static void main(String[] args) {
    try {
        JSONObject xmlJSONObj = XML.toJSONObject(TEST_XML_STRING);
        String jsonPrettyPrintString = xmlJSONObj.toString(PRETTY_PRINT_INDENT_FACTOR);
        System.out.println(jsonPrettyPrintString);
    } catch (JSONException je) {
        System.out.println(je.toString());
    }
}
}

输出将是

{"test": {
    "attrib": "moretest",
    "content": "Turn this to JSON"
}}

【讨论】:

    【解决方案2】:

    你可以看看这个link。这个示例项目清楚地表明了将 SOAP 与 Retrofit 结合使用

    【讨论】:

      猜你喜欢
      • 2017-04-29
      • 2021-04-21
      • 2017-05-24
      • 1970-01-01
      • 1970-01-01
      • 2017-08-07
      • 2014-05-02
      • 2019-05-06
      • 1970-01-01
      相关资源
      最近更新 更多