我如何使用杰克逊:
public class XmlJacksonParser {
URL url;
public void test() throws IOException {
try {
url = new URL("https://www.onliner.by/feed");
} catch (MalformedURLException e) {
e.printStackTrace();
}
JacksonXmlModule module = new JacksonXmlModule();
module.setDefaultUseWrapper(false);
XmlMapper xmlMapper = new XmlMapper(module);
OpenCredentials openCredentials = xmlMapper.readValue(url, OpenCredentials.class);
xmlMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
Log.e("LogTas", "getItemsSize: " + openCredentials.getCredentials().toString());
System.out.println(openCredentials);
}
@JacksonXmlRootElement(localName = "channel")
class OpenCredentials {
@JacksonXmlProperty(localName = "item")
@JacksonXmlElementWrapper(useWrapping = false)
private Credentials[] credentials;
public Credentials[] getCredentials() {
return credentials;
}
public void setCredentials(Credentials[] credentials) {
this.credentials = credentials;
}
//getters, setters, toString
}
class Credentials {
@JacksonXmlProperty(isAttribute = true)
private String priv;
@JacksonXmlProperty(isAttribute = true)
private String type;
private String user;
@JacksonXmlProperty(localName = "title")
private String clientToken;
@JacksonXmlProperty(localName = "description")
private String clientSecret;
}
}
并致电
XmlJacksonParser s = new XmlJacksonParser();
try {
s.test();
} catch (IOException e) {
e.printStackTrace();
}
at edu.usf.cutr.javax.xml.stream.FactoryFinder.newInstance(FactoryFinder.java:72) 在edu.usf.cutr.javax.xml.stream.FactoryFinder.find(FactoryFinder.java:176) 在edu.usf.cutr.javax.xml.stream.FactoryFinder.find(FactoryFinder.java:92) 在edu.usf.cutr.javax.xml.stream.XMLInputFactory.newInstance(XMLInputFactory.java:136) 在com.fasterxml.jackson.dataformat.xml.XmlFactory。(XmlFactory.java:97) 在com.fasterxml.jackson.dataformat.xml.XmlFactory。(XmlFactory.java:85) 在com.fasterxml.jackson.dataformat.xml.XmlFactory。(XmlFactory.java:82) 在com.fasterxml.jackson.dataformat.xml.XmlMapper。(XmlMapper.java:46) 在com.example.onexeor.rssreader.XmlJacksonParser.qweqwe(XmlJacksonParser.java:29) 在com.example.onexeor.rssreader.MainReader.onCreate(MainReader.java:74) 在android.app.Activity.performCreate(Activity.java:5977) 在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1111) 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2415) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2524) 在android.app.ActivityThread.access $ 800(ActivityThread.java:167) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1419) 在android.os.Handler.dispatchMessage(Handler.java:111) 在android.os.Looper.loop(Looper.java:194) 在android.app.ActivityThread.main(ActivityThread.java:5546) at java.lang.reflect.Method.invoke(Native Method) 在java.lang.reflect.Method.invoke(Method.java:372) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:964) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:759)
依赖关系杰克逊
compile 'com.fasterxml.jackson.core:jackson-core:2.1.2'
compile 'com.fasterxml.jackson.core:jackson-annotations:2.1.2'
compile 'com.fasterxml.jackson.core:jackson-databind:2.1.2'
// Repackaged XML-specific libraries
compile 'edu.usf.cutr.android.xml:jackson-dataformat-xml-android:2.1.2'
compile 'edu.usf.cutr.android.xml:stax2-api-android:3.1.1'
compile 'edu.usf.cutr.android.xml:stax-api-android:1.0-2'
compile 'edu.usf.cutr.android.xml:aalto-xml-android:0.9.8'