0

如何使用 WebService?这是我的代码:

WebServiceParams ws = new WebServiceParams(context);

    SoapObject request = new SoapObject(ws.getWSNameSpace(), ws.getWSHelloMethod());

    SoapSerializationEnvelope soapSerializationEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    soapSerializationEnvelope.setOutputSoapObject(request);

    request.addProperty("name", "Charly");

    try {
        HttpTransportSE httpTransportSE = new HttpTransportSE(ws.getWSUrl());
        httpTransportSE.debug = true;

        httpTransportSE.call(ws.getWSNameSpace() + ws.getWSLoginMethod(), soapSerializationEnvelope);

        String xmlResult = soapSerializationEnvelope.getResponse().toString();

        return null;
    } catch (Exception e) {
        e.printStackTrace();
        return e;
    }

我得到了 soapFault faultcode soapClient faultstring Unmarshalling Error unexpected element uri:"", local:"name" expected elements are <{}request>

Web服务的主要部分是:

<request>
      <name>?</name>
</request>

如何编写请求标签并在名称内发送它?

4

1 回答 1

1

我不知道你是否使用图书馆。我能够使用Ksoap2来实现这一点。

这里有一个例子。希望能帮助到你。

public static int getTotalRows(){
    SoapObject request = new SoapObject(Variables.WEBSERVICE_NAMESPACE,    Variables.METHOD_GETDEALS_TOTAL);
    SoapSerializationEnvelope env = new     SoapSerializationEnvelope(SoapEnvelope.VER11);
    env.dotNet = true;
    env.setOutputSoapObject(request);
    HttpTransportSE androidHttpTransport = new      HttpTransportSE(Variables.WEBSERVICE_URL);
    int totalRows = 0;

    try {
        androidHttpTransport.call(Variables.SOAP_ACTION_GETDEALS_TOTALROWS, env);
        SoapPrimitive response = (SoapPrimitive)env.getResponse();
        totalRows = Integer.valueOf(response.toString());
        Log.i(Variables.TAG, response.toString());
        return totalRows;
    } catch (IOException e) {
        e.printStackTrace();
        return totalRows;
    } catch (XmlPullParserException e) {
        e.printStackTrace();
        return totalRows;
    } catch (NumberFormatException e) {
        e.printStackTrace();
        return totalRows;
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
        return totalRows;
    }
}
于 2012-12-26T22:48:34.993 回答