我正在使用SimpleXml改装2.0.0-beta1.我想从REST服务中检索一个简单(XML)资源.使用SimpleXML编组/解组Simple对象可以正常工作.
使用此代码时(转换后的2.0.0代码形式):
final Retrofit rest = new Retrofit.Builder()
.addConverterFactory(SimpleXmlConverterFactory.create())
.baseUrl(endpoint)
.build();
SimpleService service = rest.create(SimpleService.class);
LOG.info(service.getSimple("572642"));
Run Code Online (Sandbox Code Playgroud)
服务:
public interface SimpleService {
@GET("/simple/{id}")
Simple getSimple(@Path("id") String id);
}
Run Code Online (Sandbox Code Playgroud)
我得到这个例外:
Exception in thread "main" java.lang.IllegalArgumentException: Unable to create call adapter for class example.Simple
for method SimpleService.getSimple
at retrofit.Utils.methodError(Utils.java:201)
at retrofit.MethodHandler.createCallAdapter(MethodHandler.java:51)
at retrofit.MethodHandler.create(MethodHandler.java:30)
at retrofit.Retrofit.loadMethodHandler(Retrofit.java:138)
at retrofit.Retrofit$1.invoke(Retrofit.java:127)
at com.sun.proxy.$Proxy0.getSimple(Unknown Source)
Run Code Online (Sandbox Code Playgroud)
我错过了什么?我知道通过Call作品包装返回类型.但是我希望服务将业务对象作为类型返回(并在同步模式下工作).
UPDATE
添加额外的依赖项后,并.addCallAdapterFactory(RxJavaCallAdapterFactory.create())根据不同的答案建议,我仍然会收到此错误:
Caused by: java.lang.IllegalArgumentException: Could not locate call adapter for class simple.Simple. Tried:
* retrofit.RxJavaCallAdapterFactory
* retrofit.DefaultCallAdapter$1
Run Code Online (Sandbox Code Playgroud) 我在使用SimpleXML尝试使用Gradle 0.5.+和Android Studio编译Android应用程序时遇到了麻烦.
这是错误:
Gradle: Execution failed for task ':MyApplication:dexDebug'.
> Failed to run command:
/Applications/Android Studio.app/sdk/build-tools/android-4.2.2/dx --dex --output <REALLY_LONG_STRING.....>
Error Code:
1
Output:
trouble processing "javax/xml/stream/events/StartElement.class":
Ill-advised or mistaken usage of a core class (java.* or javax.*)
when not building a core library.
This is often due to inadvertently including a core library file
in your application's project, when using an IDE (such as
Eclipse). If you are sure you're not intentionally defining a
core class, then this is …Run Code Online (Sandbox Code Playgroud) 我正在使用SimpleXml来(de)序列化POJO.现在,我有一个大的XML,它有一些不需要的元素.例如,使用此XML:
<Root>
<Element>Used</Element>
<Another>Not used</Another>
<Root>
Run Code Online (Sandbox Code Playgroud)
我想创建一个POJO,它看起来像:
@Root
class Root{
@Element
private String element;
}
Run Code Online (Sandbox Code Playgroud)
问题是我得到了这个例外:
simpleframework.xml.core.ElementException: Element 'Another' does not have a
match in class blah.blah.Blah at line 1
Run Code Online (Sandbox Code Playgroud)
那么......我应该如何配置POJO以便我能正确解析XML?
好的,所以我搜索了stackoverflow并找到了关于这个问题的未回答的子主题,这就是为什么我想明确指出这个问题:
是否有任何工具可以从xsd模式生成Simple Xml库的带注释的java类?
我收到了几个xsd文件,描述了其他人开发的一个安静的Web服务返回的对象,目前我已经将这些模式转换为simple-xml带注释的类.这些将用于我的Android应用程序.最好只是自动同步对模式的任何更改并从中重新生成类.ws们有一个针对这些模式的Jackson兼容类的存储库,但是我不想使用Jackson - 我使用Spring Android,所以我更喜欢使用首选的simple-xml.必须有像JAXB的xjc工具那样的技巧.
如果没有这样的工具 - 你能想到实现一个脚本,从模式中为simple-xml生成一个.java文件吗?也许任何值得扩展的工具的提示只需要定义要生成的注释以及何时生成?
提前 - 非常感谢您的回答!
我正在使用简单的xml库:http://simple.sourceforge.net/home.php
我有@ElementList注释的问题:如果我使用这样的注释:
@ElementList
protected List<Element> elements;
Run Code Online (Sandbox Code Playgroud)
我的XML文件还有一个属性:
<elements class="java.util.ArrayList">
Run Code Online (Sandbox Code Playgroud)
如何删除属性class="....."?
我想从API中获取XML数据,并使用Kotlin中的Retrofit2 + SimpleXML将其映射到Kotlin模型对象.
但是,我收到了来自SimpleXML的以下错误消息.
org.simpleframework.xml.core.MethodException:注释@ org.simpleframework.xml.Element(data = false,name =,required = true,type = void)必须标记set或get方法
这是获取的XML数据
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<response>
<result code="0">Success</result>
<token>XXXXXXXXXXXXXXXXXXXX</token>
<uid>4294967295</uid>
</response>
Run Code Online (Sandbox Code Playgroud)
Kotlin模型对象如下
@Root(name = "response")
public class User() {
@Element public var result: String? = null
@Element public var token: String? = null
@Element public var uid: String? = null
}
Run Code Online (Sandbox Code Playgroud)
和APIClient如下.
interface MyService {
@GET("/testLogin.xml")
fun getUser(): Call<User>
}
val retrofit = Retrofit.Builder()
.baseUrl(baseURL)
.addConverterFactory(SimpleXmlConverterFactory.create())
.build()
val call = retrofit.create(MyService::class.java).getUser()
call.enqueue(object: Callback<User> {
override fun onResponse(p0: …Run Code Online (Sandbox Code Playgroud) 我在我的android项目中使用simplexml,一切正常,直到我混淆代码.然后,错误开始涌入.
部分XML如下:
<categories success="true">
<category id="102" caption="Magazin" parent="0" num_mags="114" >
<category id="15" caption="Kunst" parent="102" num_mags="13" >
<category id="17" caption="Design" parent="15" num_mags="10" ></category>
<category id="18" caption="Haute+Couture" parent="15" num_mags="2" >
...
Run Code Online (Sandbox Code Playgroud)
我有两个类:CategoryItemList:
@Root(name = "categories")
public class CategoryItemList {
private final List<CategoryItem> mCategoryItems;
/**
* Create a new category items list.
*
* @param categoryItems the list of category items
*/
public CategoryItemList(@ElementList(name = "category", inline = true) final List<CategoryItem> categoryItems) {
mCategoryItems = categoryItems;
}
@ElementList(name = "category", inline = true) …Run Code Online (Sandbox Code Playgroud) 我刚开始使用Retrofit.我正在开发一个使用SimpleXML的项目.有人可以为我提供一个示例,其中一个从站点获取XML,例如http://www.w3schools.com/xml/simple.xml "并将其读出来?
我决定使用Simple XML序列化,并且遇到了基本问题.我想java.util.UUID在这个小类中将类实例序列化为final字段:
@Root
public class Identity {
@Attribute
private final UUID id;
public Identity(@Attribute UUID id) {
this.id = id;
}
}
Run Code Online (Sandbox Code Playgroud)
教程显示了如何通过注册转换器来序列化第三方对象:
Registry registry = new Registry();
registry.bind(UUID.class, UUIDConverter.class);
Strategy strategy = new RegistryStrategy(registry);
Serializer serializer = new Persister(strategy);
serializer.write( object, stream );
Run Code Online (Sandbox Code Playgroud)
适用于UUID的转换器非常简单:
public class UUIDConverter implements Converter<UUID> {
@Override
public UUID read(InputNode node) throws Exception {
return new UUID.fromString(node.getValue());
}
@Override
public void write(OutputNode node, UUID value) throws Exception {
node.setValue(value.toString());
} …Run Code Online (Sandbox Code Playgroud) 我必须在Android上阅读大约约4000行的XML文件.首先,我尝试使用SimpleXML库,因为它最简单,我的HTC Desire花了大约2分钟.所以我想也许SimpleXML因为反射和这个库使用的所有其他魔法而变得如此之慢.我重写了我的解析器并使用了内置的DOM解析方法,并对性能有一些特别的关注.这有点帮助,但它仍然需要大约60秒,这仍然是完全不可接受的.经过一番研究后,我在developer.com上发现了这篇文章.有一些图表显示其他两种可用方法 - SAX解析器和Android的XML Pull-Parser - 同样很慢.在文章的最后,您将找到以下声明:
我遇到的第一个惊喜是这三种方法的速度有多慢.用户不希望在移动电话上等待很长时间,因此解析超过几十条记录的任何内容都可能需要采用不同的方法.
什么可能是"不同的方法"?如果你有超过"几十条记录"怎么办?