如何使用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意外元素uri:“”,local:“name”预期元素是< {} request>
网络服务的主要部分是:
<request>
<name>?</name>
</request>
如何编写请求标记并在名称中发送它?
答案 0 :(得分: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;
}
}