我正在开发一个将调用soap web服务的android应用程序。 SOAP Web服务是在.Net中开发的。我发现在android中,我们可以调用SOAP(基于wsdl)的web服务。我为我的应用程序做了同样的事情,但我的反应与网络不同。我正在详细阐述这个问题......
我的问题是: 我使用下面的代码进行代码Web服务调用:
SOAP_ACTION = Constants.ACTION + "o1001";
METHOD_NAME = "o1001";
SoapObject request = new SoapObject(Constants.NAMESPACE, METHOD_NAME);
request.addProperty (
"p1",email);
request.addProperty (
"p2",pass);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject (request);
envelope.dotNet = true;
HttpTransportSE androidHttpTransport = new HttpTransportSE(Constants.URL);
androidHttpTransport.debug = true;
String s = "";
try {
androidHttpTransport.call(SOAP_ACTION, envelope);
s = androidHttpTransport.responseDump;
Log.v("Soap Webservice", s);
}
catch (Exception e
) {
e.printStackTrace();
}
return s ;
对于同一个Web服务,当我从在线SOAP客户端调用它时,它给了我完美的响应,但当我在Android上使用上面的代码时,它给了我错误的响应。
请建议我,我的代码中是否有任何错误,或者我是否需要添加一些内容以获得准确的响应。
感谢。
答案 0 :(得分:0)
我正在使用ksoap2,代码如下所示:
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject (request);
envelope.dotNet = true;
HttpTransportSE ht = new HttpTransportSE(url);
request.addProperty ("userName", etUserName.getText().toString());
ht.call (SOAP_ACTION, envelope);
SoapPrimitive result = (SoapPrimitive) envelope.getResponse();
在这里,您可以使用envelope.getResponse()
或envelope.bodyIn()
获得回复。您可以根据您的响应获得Object或Primitive类型。
答案 1 :(得分:0)
尝试这样可行,在异步任务Doinbackground中调用此方法,
public void getdata(String SearchValue)
{
// Create request
SoapObject request = new SoapObject(NAMESPACE2, METHOD_NAME2);
PropertyInfo pi4 = new PropertyInfo();
pi4.setName("City");
pi4.setValue(SearchValue);// get the string that is to be sent to the webservice
pi4.setType(String.class);
request.addProperty(pi4);
// Create envelope
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
// Set output SOAP object
envelope.setOutputSoapObject(request);
// Create HTTP call object
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL2);
try {
// Invole web service
androidHttpTransport.call(SOAP_ACTION2, envelope);
// Get the response
SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
//Converting string to Array list
ArrayList<String> Servciecityname_arr= new ArrayList<String>();
if ((response.toString()).contains("{"))
{
SoapObject rep = (SoapObject) envelope.bodyIn;
JSONArray jr = new JSONArray(rep.getPropertyAsString(0));
for (int i = 0; i < jr.length(); i++) {
JSONObject jb = (JSONObject) jr.get(i);
Cityname = jb.getString("CityName123");
Servciecityname_arr.add(Cityname);
}
CITYNAME = new String[Servciecityname_arr.size()];
CITYNAME = Servciecityname_arr.toArray(CITYNAME);
}
else
{
Status_Response = response.toString();
}
} catch (Exception e) {
Log.i(TAG2, "Error in catch");
e.printStackTrace();
}
}