Android Studio使用kso​​ap Library错误调用Argos WebService

时间:2015-07-21 15:55:27

标签: java android web-services wsdl ksoap

我是新手。 我试图从Android Studio调用Argos Web服务。

网址:http://ws-argos.cls.fr/argosDws/services/DixService?wsdl

但是当我想调用该函数时,我会抛出xml

<soap:Envelope 
xmlns:soap="http://www.w3.org/2003/05/soap-envelope"
xmlns:typ="http://service.dataxmldistribution.argos.cls.fr/types">
  <soap:Body>
  <typ:xmlRequest>
     <typ:username>*****</typ:username>
     <typ:password>*****</typ:password>
     <typ:platformId>149343</typ:platformId>
     <typ:nbDaysFromNow>10</typ:nbDaysFromNow>
  </typ:xmlRequest>
 </soap:Body>
</soap:Envelope>

http://ws-argos.cls.fr/argosDws/services/DixService?getXml以获取Web服务响应。 我不知道为什么但是如果我将Xml扔到http://ws-argos.cls.fr/argosDws/services/DixService?wsdl

我将获得Web服务模式而不是响应

在我的Android Studio中,我的代码将是

private static final String SOAP_ACTION = "getXml";
    private static final String NAMESPACE = "http://service.dataxmldistribution.argos.cls.fr/types";
    private static final String MAIN_REQUEST_URL = "http://ws-argos.cls.fr/argosDws/services/DixService?wsdl";

public String getXml(String username, String password, String PlatformId, String nbDaysFromNow) {
        String data = null;
        String methodname = "xmlRequest";

        SoapObject request = new SoapObject(NAMESPACE, methodname);
        request.addProperty("username", username);
        request.addProperty("password", password);
        request.addProperty("platformId", PlatformId);
        request.addProperty("nbDaysFromNow", nbDaysFromNow);

        SoapSerializationEnvelope envelope = getSoapSerializationEnvelope(request);

        HttpTransportSE ht = getHttpTransportSE();
        try {
            ht.call(SOAP_ACTION, envelope);
            SoapPrimitive resultsString = (SoapPrimitive)envelope.getResponse();

            data = resultsString.toString();

        } catch (SocketTimeoutException t) {
            t.printStackTrace();
        } catch (IOException i) {
            i.printStackTrace();
        } catch (Exception q) {
            q.printStackTrace();
        }
        return data;
    }

    private final SoapSerializationEnvelope getSoapSerializationEnvelope(SoapObject request) {
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.implicitTypes = true;
        envelope.setAddAdornments(false);
        envelope.setOutputSoapObject(request);

        return envelope;
    }

    private final HttpTransportSE getHttpTransportSE() {
        HttpTransportSE ht = new HttpTransportSE(Proxy.NO_PROXY,MAIN_REQUEST_URL,60000);
        ht.debug = true;
        return ht;
    }

但我总是在ht.call上遇到错误(SOAP_ACTION,信封);

07-22 14:02:43.562    2846-2846/com.ecocean.ecoceansharkrace W/System.err﹕ android.os.NetworkOnMainThreadException
07-22 14:02:43.562    2846-2846/com.ecocean.ecoceansharkrace W/System.err﹕ at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1147)
07-22 14:02:43.562    2846-2846/com.ecocean.ecoceansharkrace W/System.err﹕ at java.net.InetAddress.lookupHostByName(InetAddress.java:418)
07-22 14:02:43.562    2846-2846/com.ecocean.ecoceansharkrace W/System.err﹕ at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
07-22 14:02:43.562    2846-2846/com.ecocean.ecoceansharkrace W/System.err﹕ at java.net.InetAddress.getAllByName(InetAddress.java:215)
07-22 14:02:43.563    2846-2846/com.ecocean.ecoceansharkrace W/System.err﹕ at com.android.okhttp.HostResolver$1.getAllByName(HostResolver.java:29)
07-22 14:02:43.563    2846-2846/com.ecocean.ecoceansharkrace W/System.err﹕ at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:232)
07-22 14:02:43.563    2846-2846/com.ecocean.ecoceansharkrace W/System.err﹕ at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:124)
07-22 14:02:43.563    2846-2846/com.ecocean.ecoceansharkrace W/System.err﹕ at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:272)
07-22 14:02:43.563    2846-2846/com.ecocean.ecoceansharkrace W/System.err﹕ at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:211)
07-22 14:02:43.563    2846-2846/com.ecocean.ecoceansharkrace W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:382)
07-22 14:02:43.563    2846-2846/com.ecocean.ecoceansharkrace W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:106)
07-22 14:02:43.563    2846-2846/com.ecocean.ecoceansharkrace W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:217)
07-22 14:02:43.563    2846-2846/com.ecocean.ecoceansharkrace W/System.err﹕ at org.ksoap2.transport.ServiceConnectionSE.openOutputStream(ServiceConnectionSE.java:126)
07-22 14:02:43.563    2846-2846/com.ecocean.ecoceansharkrace W/System.err﹕ at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:185)
07-22 14:02:43.563    2846-2846/com.ecocean.ecoceansharkrace W/System.err﹕ at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:118)
07-22 14:02:43.564    2846-2846/com.ecocean.ecoceansharkrace W/System.err﹕ at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:113)

命名空间,主网址,soap_action和方法名称是否已正确? 对不起我的英语不好。我不是英国人。

2 个答案:

答案 0 :(得分:1)

完成添加

if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }

另一种选择是使用AsyncTask

答案 1 :(得分:0)

你应该尝试传递如下参数

String methodname =&#34; typ:xmlRequest&#34 ;;

    SoapObject request = new SoapObject(NAMESPACE, methodname);
    request.addProperty("typ:username", username);
    request.addProperty("typ:password", password);
    request.addProperty("typ:platformId", PlatformId);
    request.addProperty("typ:nbDaysFromNow", nbDaysFromNow);