将JAXB与Google Android配合使用

Dmy*_*nko 30 android jaxb

我需要解析通过xml传递的java对象.我想为这些目的使用JAXB框架,因为我已预先注释(使用JAXB)java-class.

这原则上可行吗?

InputStream input = entity.getContent();
JAXBContext jc = JAXBContext.newInstance(new Class[] {Response.LoginResponse.class});
Unmarshaller un = jc.createUnmarshaller();
LoginResponse response = (LoginResponse)un.unmarshal(input);
Run Code Online (Sandbox Code Playgroud)

在第4行,我有一个警告:"无法解析静态方法282 JAXBContext ..."然后VM崩​​溃

关于如何解决这个问题的任何想法?

Vla*_*nov 25

我并没有完全解决你的问题,但默认情况下JAXB不包含在android中,而且这个库将花费你9(!)MB的apk.请尝试使用SimpleXML.它具有类似的能力和更轻量级.

  • 我想要注意的是,我已经预先注释了(使用JAXB)java-classes.如果我将使用SimpleXML,我需要重新注释类.这在我的项目中是不可接受的. (7认同)
  • SimpleXML存在很多问题我无法证明权衡,例如强行插入某些属性(某些模式无效).如果我的生产者/消费者都是SimpleXML,那很好,但事实并非如此. (2认同)
  • 不回答问题 - 问题是如何使用PRE-ANNOTATED JAXB类. (2认同)

Sea*_*eau 24

我有同样的问题,想要在解析XML(以及对我来说,JSON)Web服务器响应的过程中使用一堆JAXB类.

在Android上编写了一个解析XML和JSON的wiki页面,它描述了这个问题的解决方案,并指导您逐步完成.

要清楚 - 我建议尽可能在Android上使用JSON而不是XML.JSON具有显着的性能优势.

总结我使用JAXB类进行JSON和XML解析的解决方案:

我最终剥离了所有注释和javax.xml.*从JAXB类导入(比为SimpleXML重新注释类稍微容易一些 - 你也可以解析JSON响应)来创建真正的POJO,然后使用Jackson修改版本的XML库依赖于取决于.

修改XML库是必要的,因为Android不包含平台中所需的XML类,如果您尝试将受保护的javax.xml名称空间中的类作为项目中的库包含,它将会抱怨.

我最初手动剥离了XML注释和导入,但最后通过ant脚本自动完成了该过程.

幸运的是,您可以自动化XML库修改过程.我写了一篇关于修改Android的XML库的详细文章(完整的教程参考),该文章描述了如何使用Jar Jar Links实用程序将相关类移动到新的不受保护的命名空间中.

如果你想用杰克逊在Android,我也创建了修改后的版本jackson-dataformat-android,aalto-xml,stax-api,和stax2-api项目为Android,你可以随意使用.

以下是您将pom.xml通过Maven包含依赖项的内容示例:

<dependencies>              
    <!-- Using Jackson for parsing -->
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.1.2</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.1.2</version>
    </dependency>

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>2.1.2</version>
    </dependency>   
    <!-- XML parsing  -->
    <!-- So many problems with XML and Android...
         Below XML libraries have been modified using JarJar
         and are NOT the same as their J2SE counterparts,
         hence the added "-android" to the artifactId
         See:
         https://github.com/CUTR-at-USF/SiriRestClient/wiki/Modifying-XML-libraries-for-Android
      -->        
    <dependency>
        <groupId>edu.usf.cutr.android.xml</groupId>
        <artifactId>jackson-dataformat-xml-android</artifactId>
        <version>2.1.2</version>
    </dependency>
    <dependency>
        <groupId>edu.usf.cutr.android.xml</groupId>
        <artifactId>stax2-api-android</artifactId>
        <version>3.1.1</version>
    </dependency>
    <dependency>
        <groupId>edu.usf.cutr.android.xml</groupId>
        <artifactId>stax-api-android</artifactId>
        <version>1.0-2</version>
    </dependency>
    <dependency>
        <groupId>edu.usf.cutr.android.xml</groupId>
        <artifactId>aalto-xml-android</artifactId>
        <version>0.9.8</version>
    </dependency>        
</dependencies>    

<repositories>
    <!-- CUTR Android XML libraries Releases -->
    <repository>
      <id>cutr-releases</id>
      <url>https://github.com/CUTR-at-USF/cutr-mvn-repo/raw/master/releases</url>
    </repository>      
</repositories>
Run Code Online (Sandbox Code Playgroud)

有关使用这些库的详细信息,请参阅此页面.

如果要将JAR文件直接包含在项目中,可以在此处从各自的目录中下载JAR文件.

您还可以查看使用这些库的开源"SIRI REST Client"应用程序作为工作示例.

编辑

如果你正在使用Gradle,你build.gradle会看起来像这样:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.9.+'
    }
}
apply plugin: 'android'

repositories {
    mavenCentral()
    maven {
        // CUTR SNAPSHOTs
        url "https://github.com/CUTR-at-USF/cutr-mvn-repo/raw/master/snapshots"
    }
    maven {
        // CUTR Releases
        url "https://github.com/CUTR-at-USF/cutr-mvn-repo/raw/master/releases"
    }
}

android {
    compileSdkVersion 19
    buildToolsVersion "19.0.1"
    ...
}

dependencies {
    ...
    // Normal Jackson libraries
    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'
    ...       
}
Run Code Online (Sandbox Code Playgroud)