为KSOAP SOAP对象设置参数(android)

时间:2014-03-20 15:09:59

标签: android web-services soap

我的webservice方法似乎无法识别Property Info对象上的set参数。

我这样做:

public class WebService {
//Namespace of the Webservice - can be found in WSDL
private static String NAMESPACE = "http://service.marcusjacobsson.com/";
//Webservice URL - WSDL File location    
private static String URL = "http://192.168.1.139:8080/WebServiceProject/HelloWebService?wsdl";
//SOAP Action URI again Namespace + Web method name
private static String SOAP_ACTION = "http://service.marcusjacobsson.com/";

public static String invokeHelloWorldWS(String name, String webMethName) {
    String resTxt = null;
    // Create request
    SoapObject request = new SoapObject(NAMESPACE, webMethName);
    // Property which holds input parameters
    PropertyInfo sayHelloPI = new PropertyInfo();
    // Set Name
    sayHelloPI.setName("name");
    // Set Value
    sayHelloPI.setValue(name);       
    // Set dataType
    sayHelloPI.setType(String.class);
    // Add the property to request object
    request.addProperty(sayHelloPI);
    // Create envelope
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
            SoapEnvelope.VER11);
    // Set output SOAP object
    envelope.setOutputSoapObject(request);
    // Create HTTP call object
    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

    try {
        // Invoke web service
        androidHttpTransport.call(SOAP_ACTION+webMethName, envelope);
        // Get the response
        SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
        // Assign it to resTxt variable static variable
        resTxt = response.toString();

    } catch (Exception e) {
        //Print error
        e.printStackTrace();
        //Assign error message to resTxt
        resTxt = "Error occured";
    } 
    //Return resTxt to calling object
    return resTxt;
}   

}

因此,当调用invokeHelloWorldWS()时,我已经定义了应用程序设置的名称和Web方法名称。

web服务:

@WebService
public class HelloWeb {

@WebMethod
public String sayGreeting(String name) {

    return "Greeting " + name + "!";

}

@WebMethod
public String testMethod(String parameter){
    return "Your parameter was: " + parameter;
}

}

这是来自网络服务的回复:

问候无效!

为什么我的参数不能与我的肥皂一起传递?

1 个答案:

答案 0 :(得分:0)

通过在我的网络服务中添加@WebParam(name =" name")来解决这个问题:

@WebService
public class HelloWeb {

    @WebMethod
    public String sayGreeting(@WebParam(name = "name")String name) {

    return "Greeting " + name + "!";

    }
}