I have made one sample for this kind of webservices. (using webservicex as example, too). You can see it at my blog or in my github
UPDATE
Needed libraries:
...
compile 'com.squareup.okhttp3:logging-interceptor:3.3.1'
compile 'com.squareup.okhttp3:okhttp:3.3.1'
compile 'com.squareup.okhttp3:okhttp-urlconnection:3.3.1'
compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile ('com.squareup.retrofit2:converter-simplexml:2.1.0'){
exclude group: 'stax', module: 'stax-api'
exclude group: 'stax', module: 'stax'
exclude group: 'xpp3', module: 'xpp3'
}
...
The sample is for this webservice: http://www.webservicex.net/New/Home/ServiceDetail/42
First of all, you have to create Retrofit instance:
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
Strategy strategy = new AnnotationStrategy();
Serializer serializer = new Persister(strategy);
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.addInterceptor(interceptor)
.connectTimeout(2, TimeUnit.MINUTES)
.writeTimeout(2, TimeUnit.MINUTES)
.readTimeout(2, TimeUnit.MINUTES)
.build();
Retrofit retrofit = new Retrofit.Builder()
.addConverterFactory(SimpleXmlConverterFactory.create(serializer))
.baseUrl("http://www.webservicex.net/")
.client(okHttpClient)
.build();
After that, you have to create the API object:
public interface UsStatesApi {
@Headers({
"Content-Type: text/xml",
"Accept-Charset: utf-8"
})
@POST("/uszip.asmx")
Call<UsStatesResponseEnvelope> requestStateInfo(@Body UsStatesRequestEnvelope body);
}
For creating your entities, you have to annotate them correctly. These are the request classes (response classes are made in the same way):
class: UsStatesRequestEnvelope
@Root(name = "soap12:Envelope")
@NamespaceList({
@Namespace( prefix = "xsi", reference = "http://www.w3.org/2001/XMLSchema-instance"),
@Namespace( prefix = "xsd", reference = "http://www.w3.org/2001/XMLSchema"),
@Namespace( prefix = "soap12", reference = "http://www.w3.org/2003/05/soap-envelope")
})
public class UsStatesRequestEnvelope {
@Element(name = "soap12:Body", required = false)
private UsStatesRequestBody body;
public UsStatesRequestBody getBody() {
return body;
}
public void setBody(UsStatesRequestBody body) {
this.body = body;
}
}
class : UsStatesRequestBody
@Root(name = "soap12:Body", strict = false)
public class UsStatesRequestBody {
@Element(name = "GetInfoByState",required = false)
private UsStatesRequestData usStatesRequestData;
public UsStatesRequestData getUsStatesRequestData() {
return usStatesRequestData;
}
public void setUsStatesRequestData(UsStatesRequestData usStatesRequestData) {
this.usStatesRequestData = usStatesRequestData;
}
}
class: UsStatesRequestData
@Root(name = "GetInfoByState", strict = false)
@Namespace(reference = "http://www.webserviceX.NET")
public class UsStatesRequestData {
@Element(name = "USCity", required = false)
private String city;
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
}