如何在ksoap2中使用参数?

时间:2012-04-08 19:10:59

标签: android web-services ksoap2

我创建了一个Web服务来通过ksoap2测试传递参数。我认为这似乎是一个非常简单的过程,但我显然错过了一些简单的事情。 Web服务只执行返回传递给它的整数。当我执行下面的代码时,它返回数字0而不是1。

    private static String SOAP_ACTION = "http://tempuri.org/TestParams";
private static String NAMESPACE = "http://tempuri.org/";
private static String METHOD_NAME = "TestParams";
private static String URL = "http://services.lockrem.com/WebService.asmx?WSDL";

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
    request.addProperty("RoundId", 1);
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    envelope.setOutputSoapObject(request);

    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
    try {
        androidHttpTransport.call(SOAP_ACTION, envelope);
    } catch (Exception e) {
        e.printStackTrace();
    }

    SoapObject result = (SoapObject)envelope.bodyIn;

注意:我使用.net网页对其进行了测试,并按预期返回数字1。这里的Web服务不是问题。

这是网络服务。

    [WebMethod]
public int TestParams(int RoundId)
{
    return RoundId;
}

感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

免责声明:每当我尝试使用kSoap2与.NET网络服务交谈时,我通常会做很多 fiddling 。我的意思是字面意思:我设置了一个带有Fiddler2的Windows机器,并将其用作失败请求和工作引用请求的代理,因此我实际上可以看出出现了什么问题。一旦你尝试做一些非平凡的事情,我强烈建议做类似的事情。

那就是说,在你的情况下,它可能就足够了

envelope.dotNet = true;

答案 1 :(得分:0)

尝试在最后删除WSDL

private static String URL = "http://services.lockrem.com/WebService.asmx";

另外尝试使用它来获得响应:

Object response = envelope.getResponse();

而不是SoapObject result = (SoapObject)envelope.bodyIn;

<强> EDITED

这有效:

public class MainActivity extends Activity {

private static String NAMESPACE = "http://tempuri.org/";
private static String METHOD_NAME = "TestParams";
private static String URL = "http://services.lockrem.com/WebService.asmx";
private TextView tv;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    tv = (TextView)findViewById(R.id.tv);
    HttpTransportSE transport = new HttpTransportSE(URL);
    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
    request.addProperty("RoundId", 1);
    SoapSerializationEnvelope envelope =
        new SoapSerializationEnvelope(SoapEnvelope.VER11);
    envelope.dotNet = true;
    envelope.setOutputSoapObject(request);
    try {
        transport.call(NAMESPACE + METHOD_NAME, envelope);
        Object response = envelope.getResponse();
        tv.setText(response.toString());
    } catch (Exception e) {
        e.printStackTrace();
    }
}
}

希望它有所帮助。