0

我正在尝试从 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 网络服务代码:

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

我的全局:

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 = 假;但它仍然无法正常工作。

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

4

2 回答 2

0

您的方法的第一个参数的类型是字符串数组,而不仅仅是字符串。StringArraySerializer请尝试手动添加字符串,而不是。您应该会看到此页面:向请求添加复杂对象数组

于 2013-07-01T12:07:18.267 回答
0

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

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

于 2016-03-30T03:12:55.447 回答