简体   繁体   English

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

[英]JAVA web service receiving null parameters from Android Ksoap

I am trying to consume a JAVA web service from android. 我正在尝试从android使用Java Web服务。

Here is what I have tried so far: 到目前为止,这是我尝试过的:

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);
    }
}

My Java web service Code: 我的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";

Issue: 问题:

The response I receive is: 我收到的回复是:

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

The first parameter is not being sent to web service. 第一个参数未发送到Web服务。 I have tried to remove this line: 我试图删除此行:

soapEnvelope.dotNet = false; soapEnvelope.dotNet = false; but its still not working. 但它仍然无法正常工作。

Guys please help me out. 伙计们请帮帮我。 I am stuck for two days. 我被困了两天。 Thanks for any help provided. 感谢您提供的任何帮助。

Your method's first argument's type is String array, not only String. 方法的第一个参数的类型是String数组,而不仅仅是String。 Instead of StringArraySerializer , please try adding strings manually. 代替StringArraySerializer ,请尝试手动添加字符串。 You should see this page for this: Adding an array of complex objects to the request . 您应该为此查看该页面: 向请求添加复杂对象数组

Check if your namespace is right! 检查您的名称空间是否正确!

I spent three hours with the same problem and only then realized that the namespace that was declared in the WebService class was different from what I put in the creation of SoapObject. 我花了三个小时解决相同的问题,然后才意识到WebService类中声明的名称空间与我创建SoapObject时使用的名称空间不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM