使用Retrofit发布soap xml请求

时间:2016-06-15 04:10:41

标签: android android-studio

我正在尝试使用Retrofit发布一个soap xml,但它失败了,我使用Simple XML Framework来建模一个如下所示的SOAP请求:

请求XML

enter code hevasu@vasu-HP-Pavilion-15-Notebook-PC:~$ sudo pip install virtualenv virtualenvwrapper
[sudo] password for vasu: 
The directory '/home/vasu/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/home/vasu/.cache/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Requirement already satisfied (use --upgrade to upgrade): virtualenv in /usr/local/lib/python2.7/dist-packages
Requirement already satisfied (use --upgrade to upgrade): virtualenvwrapper in /usr/local/lib/python2.7/dist-packages
Requirement already satisfied (use --upgrade to upgrade): stevedore in/usr/local/lib/python2.7/dist-packages (from virtualenvwrapper)
Requirement already satisfied (use --upgrade to upgrade): virtualenv-clone in /usr/local/lib/python2.7/dist-packages (from virtualenvwrapper)
Requirement already satisfied (use --upgrade to upgrade): pbr>=1.6 in /usr/local/lib/python2.7/dist-packages (from stevedore->virtualenvwrapper)
Requirement already satisfied (use --upgrade to upgrade): six>=1.9.0 in /usr/local/lib/python2.7/dist-packages (from stevedore->virtualenvwrapper)
/usr/local/lib/python2.7/dist-packages/pip/_vendor/requests/packages/urllib3/util/ssl_.py:318: SNIMissingWarning: An HTTPS request has been made, but the SNI (Subject Name Indication) extension to TLS is not available on this platform. This may cause the server to present an incorrect TLS certificate, which can cause validation failures. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#snimissingwarning.
SNIMissingWarning
/usr/local/lib/python2.7/dist-packages/pip/_vendor/requests/packages/urllib3/util/ssl_.py:122: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.
InsecurePlatformWarningre

预期输出:

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

失败输出

<?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>

EndPoint接口

<--- 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

主类代码

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

1 个答案:

答案 0 :(得分:19)

我为这种网络服务制作了一个样本。 (也以webservicex为例)。您可以在blogmy github

中查看

更新

需要的图书馆:

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

该示例适用于此网络服务:http://www.webservicex.net/New/Home/ServiceDetail/42

首先,您必须创建Retrofit实例:

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

之后,您必须创建API对象:

    public interface UsStatesApi {

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

}

要创建实体,您必须正确地注释它们。这些是请求类(响应类以相同的方式生成):

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

课程: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;
    }
}

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

}