I've been trying and searching but can't seem to find a solution. I have this xml content:
<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>
provided by a link. I've added the INTERNET permission in Android Manifest:
<uses-permission android:name="android.permission.INTERNET"/>
The class for the data I tried to implement:
@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;
}
}
The interface for Retrofit:
public interface WebAPI {
@GET("api/values")
Call<ArrayOfstring> loadArrayOfstring();
}
The class with callbacks:
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();
}
And I start the Controller so that the get request would begin in my activity, like this:
Controller ctrl = new Controller();
ctrl.start();
But the only result there seems to be
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 [
The link formed should be http://xx.xxx.xxx.xx:xxxx/api/values/
I know, I am a little late to the party. In case, if it helps, I am answering the question.
You need to pass "Accept", "Application/xml" header in the request.
I was facing the same issue, this worked for me.