XMLStreamException:[行,列] 处的 ParseError:[1,1];开始标签之前只允许有空格内容,而不是 [

XMLStreamException: ParseError at [row,col]:[1,1] ; only whitespace content allowed before start tag and not [

我一直在尝试和搜索,但似乎找不到解决方案。我有这个 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>

由 link 提供。 我在 Android 清单中添加了 INTERNET 权限:

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

我尝试实现的数据class:

@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;
    }

}

改造界面:

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

}

带回调的class:

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();
    }

然后我启动控制器,以便在我的 activity 中开始获取请求,如下所示:

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 [

组成的link应该是http://xx.xxx.xxx.xx:xxxx/api/values/

我知道,我来晚了一点。以防万一,如果有帮助,我来回答这个问题。

需要在请求中传递"Accept","Application/xml"header

我遇到了同样的问题,这对我有用。