Retrofit2 + YII2 Rest API(org.xmlpull.v1.XmlPullParserException:意外的令牌)

时间:2016-12-09 21:11:28

标签: android parsing yii2 retrofit2

帮助了解在哪里查看,似乎对象描述正确,xml输入正确,如何修复错误并获取数据?

服务器端YII2 rest API,客户端android应用程序。在下面 - 输入XML,模型类,retrofit2使用代码。

/ API / V1 /书籍

<?xml version="1.0" encoding="UTF-8"?>
<response>
   <item>
      <id>1</id>
      <title>Test</title>
      <description>test</description>
      <thumbnail>1</thumbnail>
      <path>1</path>
   </item>
   <item>
      <id>2</id>
      <title>Test 2</title>
      <description>2</description>
      <thumbnail>2</thumbnail>
      <path>2</path>
   </item>
</response>

Books.java

@Root (name = "response")
public class Books {

    @ElementList (name = "item")
    List<Book> items;
}

Book.java

@Root(name = "item")
public class Book {

    @Element(name = "id")
    private int id;

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

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

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

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

}

改造用法

Retrofit retrofit = new Retrofit.Builder().baseUrl(API_PATH).client(new OkHttpClient()).addConverterFactory(SimpleXmlConverterFactory.create()) .build();

API sAPI = retrofit.create(API.class);

Call<Books> call = sAPI.loadBooks(); call.enqueue(b);

错误堆栈

W/System.err: java.lang.RuntimeException: org.xmlpull.v1.XmlPullParserException: Unexpected token (position:TEXT [{"id":1,"title"...@1:145 in java.io.InputStreamReader@422af58) 
     at retrofit2.converter.simplexml.SimpleXmlResponseBodyConverter.convert(SimpleXmlResponseBodyConverter.java:44)
     at retrofit2.converter.simplexml.SimpleXmlResponseBodyConverter.convert(SimpleXmlResponseBodyConverter.java:23)
     at retrofit2.ServiceMethod.toResponse(ServiceMethod.java:117)
     at retrofit2.OkHttpCall.parseResponse(OkHttpCall.java:211)
     at retrofit2.OkHttpCall$1.onResponse(OkHttpCall.java:106)
     at okhttp3.RealCall$AsyncCall.execute(RealCall.java:133)
     at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
     at java.lang.Thread.run(Thread.java:818)
 Caused by: org.xmlpull.v1.XmlPullParserException: Unexpected token (position:TEXT [{"id":1,"title"...@1:145 in java.io.InputStreamReader@422af58) 
     at org.kxml2.io.KXmlParser.next(KXmlParser.java:432)
     at org.kxml2.io.KXmlParser.next(KXmlParser.java:313)
     at org.simpleframework.xml.stream.PullReader.read(PullReader.java:105)
     at org.simpleframework.xml.stream.PullReader.next(PullReader.java:89)
     at org.simpleframework.xml.stream.NodeReader.readElement(NodeReader.java:111)
     at org.simpleframework.xml.stream.NodeReader.readRoot(NodeReader.java:85)
     at org.simpleframework.xml.stream.NodeBuilder.read(NodeBuilder.java:84)
     at org.simpleframework.xml.stream.NodeBuilder.read(NodeBuilder.java:71)
     at org.simpleframework.xml.core.Persister.read(Persister.java:562)
     at retrofit2.converter.simplexml.SimpleXmlResponseBodyConverter.convert(SimpleXmlResponseBodyConverter.java:36)
    ... 9 more

2 个答案:

答案 0 :(得分:0)

我很抱歉。它只是返回json响应而不是xml(参见 position:TEXT [{“id”:1,“title )。所以只需要使用GsonConverterFactory,而不是SimpleXmlConverterFactory。

我必须更仔细地阅读有关stackoverflow的其他问题

答案 1 :(得分:0)

您应该在Header中指定Response内容类型(xml或json),因此您的接口或多或少会像这样

public interface ApiClient {

@GET("/books")
@Headers({"Content-Type: application/xml", "Accept: application/xml"})
Call<Books> loadBooks();

}