JAVA Web服务从Android Ksoap接收空参数

时间:2013-07-01 08:08:42

标签: java android web-services ksoap2 android-ksoap2

我正在尝试从Android使用JAVA Web服务。

这是我到目前为止所尝试的内容:

private void CallWebServiceDummy() {
    // TODO Auto-generated method stub
    try {
        if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
                    .permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }

        SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(
                SoapEnvelope.VER10);
        soapEnvelope.dotNet = false;
        SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);

        PropertyInfo pi = new PropertyInfo();       

          StringArraySerializer a = new StringArraySerializer();
          a.add("hello"); a.add("world"); String n0 = NAMESPACE;

          pi = new PropertyInfo(); pi.setName("a"); pi.setValue(a);
          pi.setType(a.getClass()); pi.setNamespace(n0);
          Request.addProperty(pi);

          String b = "my name"; pi = new PropertyInfo(); pi.setName("b");
          pi.setValue(b); Request.addProperty(pi);


        soapEnvelope.setOutputSoapObject(Request);
        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
        androidHttpTransport.debug = true;

        androidHttpTransport.call(SOAP_ACTION, soapEnvelope);
        Log.d("test", "request: " + androidHttpTransport.requestDump);
        Log.d("test", "response: " + androidHttpTransport.responseDump);

        SoapObject resultsRequestSOAP = (SoapObject) soapEnvelope.bodyIn;       

        String c = resultsRequestSOAP.toString();

    } catch (Exception e) {
        Context context = getApplicationContext();
        int duration = Toast.LENGTH_LONG;

        Toast toast = Toast.makeText(context, e.getMessage(), duration);
        toast.show();

        toast.setGravity(Gravity.CENTER | Gravity.CENTER_HORIZONTAL, 0, 0);
    }
}

我的Java Web服务代码:

package MyPackage;

public class WebServiceClass {

public String addnumbers(String[] a, String b) {
    String c = new StringBuilder("This the String1 ").append(a[0]).append(" merged with String2 ").append(b).toString();

    return c;
}
}

My Globals:

private static final String NAMESPACE = "http://MyPackage"; private
  static final String URL =
  "http://10.0.2.2:8080/WebService/services/WebServiceClass?wsdl"; private
  static final String SOAP_ACTION = "urn:addnumbers"; private static final
  String METHOD_NAME = "addnumbers";

问题:

我收到的回复是:

  addnumbersResponse{return=This the String1  merged with String2 my name; }

第一个参数未发送到Web服务。我试图删除这一行:

soapEnvelope.dotNet = false;但它仍无法正常工作。

伙计们请帮帮我。我被困了两天。感谢您提供的任何帮助。

2 个答案:

答案 0 :(得分:0)

您的方法的第一个参数类型是String数组,而不仅仅是String。请尝试手动添加字符串,而不是StringArraySerializer。您应该看到此页面:Adding an array of complex objects to the request

答案 1 :(得分:0)

检查您的命名空间是否正确!

我花了三个小时处理同样的问题,然后才意识到WebService类中声明的命名空间与我在创建SoapObject时所使用的命名空间不同。