XMLStreamException:[row,col]处的ParseError:[1,1];只有在开始标记之前允许的空白内容而不是[

时间:2017-11-26 20:30:25

标签: android xml retrofit simple-xml-converter

我一直在尝试和搜索,但似乎无法找到解决方案。我有这个xml内容:

<ArrayOfstring xmlns:i="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
    <string>C201711241121.png</string>
    <string>G201711241121.png</string>
    <string>I201711241121.png</string>
    <string>I201711241121.png</string>
 </ArrayOfstring>

由链接提供。 我在Android Manifest中添加了INTERNET权限:

<uses-permission  android:name="android.permission.INTERNET"/>

我尝试实施的数据类:

@Root
@NamespaceList({
        @Namespace(reference="http://www.w3.org/2001/XMLSchema-instance", prefix="i"),
        @Namespace(reference="http://schemas.microsoft.com/2003/10/Serialization/Arrays")
})
public class ArrayOfstring {

    @ElementList
    private List<String> string;

    public void setString(List<String> string) {
        this.string = string;
    }

    public List<String> getString(){
        return string;
    }

}

Retrofit的界面:

public interface WebAPI {
    @GET("api/values")
    Call<ArrayOfstring> loadArrayOfstring();

}

带回调的课程:

public class Controller implements Callback<ArrayOfstring> {

    static final String BASE_URL = "http://xx.xxx.xxx.xx:xxxx/";

    public void start() {
        Retrofit retrofit = new Retrofit.Builder().baseUrl(BASE_URL)
                .addConverterFactory(SimpleXmlConverterFactory.create()).build();

        WebAPI vogellaAPI = retrofit.create(WebAPI.class);

        Call<ArrayOfstring> call = vogellaAPI.loadArrayOfstring();
        call.enqueue(this);
    }

    @Override
    public void onResponse(Call<ArrayOfstring> call, Response<ArrayOfstring> response) {
        if (response.isSuccessful()) {
            ArrayOfstring resp = response.body();
            if (resp == null){
                Log.v("resp","is null");
                return;
            }
           // System.out.println("Channel title: " + rss.getChannelTitle());
            Log.v("response",resp.getString().size()+" size");
            for (String str: resp.getString()){
                Log.v("response", str+" string");
            }
        } else {
            //System.out.println(response.errorBody());
            Log.v("response", "error "+response.code());
        }
    }

    @Override
    public void onFailure(Call<ArrayOfstring> call, Throwable t) {
        t.printStackTrace();
    }

我启动Controller以便get请求将在我的活动中开始,如下所示:

Controller ctrl = new Controller();
ctrl.start();

但唯一的结果似乎是

W/System.err: java.lang.RuntimeException: javax.xml.stream.XMLStreamException: ParseError at [row,col]:[1,1]
W/System.err: Message: only whitespace content allowed before start tag and not [

形成的链接应为http://xx.xxx.xxx.xx:xxxx/api/values/

1 个答案:

答案 0 :(得分:1)

我知道,我参加聚会有点晚了。如果有帮助,我在回答问题。

您需要在请求中传递 “ Accept”,“ Application / xml” 标头。

我面临着同样的问题,这对我有用。