2016-06-15 19 views
8

Sto cercando di inviare un XML sapone con Retrofit ma è mancanza, sto usando un quadro XML semplice per modellare una richiesta SOAP che assomiglia a questo:Inserisci richiesta sapone XML con Retrofit

richiesta XML

@Root(name = "soap12:Envelope") 
@NamespaceList({ 
     @Namespace(reference = "http://www.w3.org/2001/XMLSchema-instance", prefix = "xsi"), 
     @Namespace(reference = "http://www.w3.org/2001/XMLSchema", prefix = "xsd"), 
     @Namespace(prefix = "soap12", reference = "http://www.w3.org/2003/05/soap-envelope") 
}) 
@Element(name = "soap12:Body") 
public class GeoIP { 

    @Namespace(reference = "http://www.webservicex.net/") 
    @Element(name="GetGeoIP") 
    private GetGeoIP GetGeoIP; 

    public GetGeoIP getGetGeoIP() { 
     return GetGeoIP; 
    } 

    public void setGetGeoIP(GetGeoIP getGeoIP) { 
     this.GetGeoIP = getGeoIP; 
    } 

    @Namespace(reference = "http://www.webservicex.net/") 
    public static class GetGeoIP{ 
     @Element(name = "IPAddress") 
     private String IPAddress; 

     public String getIP() { 
      return IPAddress; 
     } 

     public void setIP(String IP) { 
      this.IPAddress = IP; 
     } 
    } 
} 

uscita prevista:

<?xml version="1.0" encoding="utf-8"?> 
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"> 
    <soap12:Body> 
    <GetGeoIPResponse xmlns="http://www.webservicex.net/"> 
     <GetGeoIPResult> 
     <ReturnCode>int</ReturnCode> 
     <IP>string</IP> 
     <ReturnCodeDetails>string</ReturnCodeDetails> 
     <CountryName>string</CountryName> 
     <CountryCode>string</CountryCode> 
     </GetGeoIPResult> 
    </GetGeoIPResponse> 
    </soap12:Body> 
</soap12:Envelope> 

Fail ure uscita

<--- HTTP 500 http://www.webservicex.net/geoipservice.asmx (2240ms) 
06-15 08:22:09.567 26327-26359/com. D/Retrofit: : HTTP/1.1 500 Internal Server Error 
06-15 08:22:09.567 26327-26359/com.a D/Retrofit: Cache-Control: private 
06-15 08:22:09.567 26327-26359/com.a.mfmpay D/Retrofit: Content-Length: 1188 
06-15 08:22:09.567 26327-26359/com.a D/Retrofit: Content-Type: text/xml; charset=utf-8 
06-15 08:22:09.567 26327-26359/com.a D/Retrofit: Date: Wed, 15 Jun 2016 03:22:00 GMT 
06-15 08:22:09.567 26327-26359/com.a D/Retrofit: OkHttp-Received-Millis: 1465960929564 
06-15 08:22:09.567 26327-26359/com.a D/Retrofit: OkHttp-Response-Source: NETWORK 500 
06-15 08:22:09.567 26327-26359/com.a D/Retrofit: OkHttp-Selected-Protocol: http/1.1 
06-15 08:22:09.567 26327-26359/com.a D/Retrofit: OkHttp-Sent-Millis: 1465960928849 
06-15 08:22:09.567 26327-26359/com.a D/Retrofit: Server: Microsoft-IIS/7.0 
06-15 08:22:09.567 26327-26359/com.a D/Retrofit: X-AspNet-Version: 4.0.30319 
06-15 08:22:09.567 26327-26359/com.a D/Retrofit: X-Powered-By: ASP.NET 
                    at System.Web.Services.Protocols.ServerProtocolFactory.Create(Type type, HttpContext context, HttpRequest request, HttpResponse response, Boolean&amp; abortProcessing)</faultstring><detail /></soap:Fault></soap:Body></soap:Envelope> 
06-15 08:22:09.570 26327-26359/com.a D/Retrofit: <--- END HTTP (1188-byte body) 
06-15 08:22:09.571 26327-26327/com.a V/TAG: 500 Internal Server Error 

EndPoint interfaccia

public interface MagentoApi { 
@Headers({"Content-Type: application/soap+xml; charset=utf-8"}) 
    @POST("/geoipservice.asmx") 
    public void requestGeo(@Body GeoIP.GetGeoIP body,Callback<GeoIPResponse> cb); 
} 

Main Class Codice

GeoIP.GetGeoIP request = new GeoIP.GetGeoIP(); 
     request.setIP("192.168.1.1"); 

     RestAdapter restAdapter = getRestAdapter(); 
     MagentoApi api = restAdapter.create(MagentoApi.class); 

     api.requestGeo(request, cb); 

retrofit.Callback<GeoIPResponse> cb = new retrofit.Callback<GeoIPResponse>() { 
    @Override 
    public void success(GeoIPResponse geoIPResponse, retrofit.client.Response response) { 
     Log.v("TAG", String.valueOf(response.getStatus())); 
    } 

    @Override 
    public void failure(RetrofitError error) { 
Log.v("TAG",error.getLocalizedMessage()); 
    } 
}; 
    public static RestAdapter getRestAdapter() { 
     Strategy strategy = new AnnotationStrategy(); 
     Serializer serializer = new Persister(strategy); 

     OkHttpClient okHttpClient = new OkHttpClient(); 
     RestAdapter restAdapter = new RestAdapter.Builder() 
       .setEndpoint("http://www.webservicex.net") 
       .setClient(new OkClient(okHttpClient)) 
       .setConverter(new SimpleXMLConverter(serializer)) 
       .setLogLevel(RestAdapter.LogLevel.FULL) 
       .build(); 

     return restAdapter; 
    } 
+0

può spunta questo link http://geekcalledk.blogspot.com/2014/08/use-simple-xml-w ith-retrofit-for-making.html e https://snow.dog/blog/android-and-magento-soap-api-part-ii/ –

+0

@ Md.SajedulKarim già seguito ma non pieno di frutta per me. – AKSiddique

risposta

15

ho fatto un campione per questo tipo di webservices. (usando anche webservicex come esempio). Lo si può vedere nel mio blog o in my github

UPDATE

librerie necessarie:

... 
    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' 
    } 
... 

Il campione è per questo webservice: http://www.webservicex.net/New/Home/ServiceDetail/42

Prima di tutto, è necessario creare Retrofit istanza:

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

Dopo di che, è necessario creare l'oggetto API:

public interface UsStatesApi { 

    @Headers({ 
      "Content-Type: text/xml", 
      "Accept-Charset: utf-8" 
    }) 
    @POST("/uszip.asmx") 
    Call<UsStatesResponseEnvelope> requestStateInfo(@Body UsStatesRequestEnvelope body); 

} 

per la creazione di entità, si deve annotare correttamente. Queste sono le classi di richiesta (classi di risposta sono realizzati nello stesso modo):

classe: 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; 
    } 
} 

classe: UsStatesRequestBody

@Root(name = "soap12:Body", strict = false) 
public class UsStatesRequestBody { 

    @Element(name = "GetInfoByCity",required = false) 
    private UsStatesRequestData usStatesRequestData; 

    public UsStatesRequestData getUsStatesRequestData() { 
     return usStatesRequestData; 
    } 

    public void setUsStatesRequestData(UsStatesRequestData usStatesRequestData) { 
     this.usStatesRequestData = usStatesRequestData; 
    } 
} 

classe: 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; 
    } 

} 
+0

Ciao, l'ho implementato come soluzione e ha funzionato. Ma ho qualche problema che quando il @Body contiene un campo che ha valore come stringa XML (il mio è rawData con valore è stringa XML), il valore di quel campo è stato codificato come: "rawData> <![CDATA [< ws: login > < userName Come impedire che codificato? Ho alcuni problemi simili sullo stack: http://stackoverflow.com/questions/41378269/how-to-disable-a-body-data-getting-encoded-in-soap-retrofit-request Puoi fornire ho qualche idea se abbia? – Dolphin

Problemi correlati