我正在编写一个使用KSOAP与web服务通信的Android应用程序。web服务和Android应用程序之间的连接正在工作,因为我可以调用web服务并获得返回值(hello)。但是,如果我试图通过. addproperty从应用程序给web服务一个名称,web服务返回一个空对象。
下面是我的代码:MainActivity:
private final String NAMESPACE_Local = "http://test.com/";
private final String URL_Local = "http://168.185.226.21:7001/myTest/myTestWebServiceService";
private final String SOAP_ACTION_Local = "Hello_Action_Extend";
private final String METHOD_NAME_Local = "hello_extend";
public void LocalServer(View view)
{
TextView text = (TextView) findViewById(R.id.update_text);
SoapObject request = new SoapObject(NAMESPACE_Local, METHOD_NAME_Local);
request.addProperty("name", "Christian");
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL_Local);
try {
androidHttpTransport.call(SOAP_ACTION_Local, envelope);
SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
Log.i("myApp", response.toString());
text.setText(response.toString());
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(this,"Device or service offline",Toast.LENGTH_LONG).show();
}
}
网络服务器:
package com.test;
import javax.jws.*;
@WebService
public class myTestWebService {
@WebMethod(action="Hello_Action") //that method works
public String hello() {
return "hello";
}
@WebMethod(action="Hello_Action_Extend")
public String hello_extend(String name) //that works also, but it is giving back "hello null"
{
return "hello "+name;
}
}
我希望你能帮助我!
尝试替换:
request.addProperty("name", "Christian");
为:request.addProperty("name",ElementType.STRING_CLASS, "Christian");
和
的响应SoapObject reponse=(SoapObject)envelope.getResponse();
response.getProperty("name");
API SoapObject