在android上改造2和xml

时间:2017-08-09 16:47:27

标签: java android xml retrofit

我必须创建哪个类来接收一个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

2 个答案:

答案 0 :(得分:0)

如果您习惯于改造,则将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"
}}

答案 1 :(得分:0)

您可以查看此link。此示例项目清楚地表明了使用SOAP和Retrofit