java - KSoap2 + Android + .net Ws = Null

标签 java android ksoap2 android-ksoap2

我在连接到 .net Web 服务时在 android 项目中使用 Ksoap2 时遇到了一些问题。 只要我调用 Ws withouth 参数一切正常,但是当我尝试添加参数时,服务器永远不会获取它们。 这是我的代码

import java.util.Vector;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.AndroidHttpTransport;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class ServicioWeb extends Activity {

SoapObject response;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
   // String NAMESPACE = "http://tempuri.org/";
    String NAMESPACE = "IDBN.WS";
    String METHOD_NAME = "getClientesByName";
    //String SOAP_ACTION = "http://tempuri.org/getClientesByName";
    String SOAP_ACTION = "IDBN.WS/getClientesByName";
    //String URL = "http://www.ws.idbnar2.com.ar/wsClientes.asmx";
    String URL = "http://www.ws.idbnar2.com.ar/wsClientes.asmx";
    SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);
   PropertyInfo pi = new PropertyInfo();
    pi.setName("Nombre");
    pi.setValue("RIQUELME");
    pi.setType(int.class);
    Request.addProperty(pi);

    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    envelope.dotNet = true;
    envelope.setOutputSoapObject(Request);
    AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);
    ListView Lista = (ListView)findViewById(R.id.Lista);

    try
    {
        androidHttpTransport.call(SOAP_ACTION, envelope);
        response = (SoapObject)envelope.getResponse();
        String[] Clientes = getStringArrayResponse(response, null);
        Lista.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,Clientes));
    }
    catch(Exception e)
    {
        Toast toast = Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG);
        toast.show();
    }
}

public String[] getStringArrayResponse(SoapObject node, Vector<String> strings) {
    boolean isFirstCall = false;
    if (strings == null) {
        isFirstCall = true;
        strings = new Vector<String>();
    }
    int count = response.getPropertyCount();
    for (int i = 0; i < count; i++) {
        Object obj1 = node.getProperty(i);
        if (obj1 instanceof SoapObject) {
            // don't recurse empty objects
            if (((SoapObject)obj1).getPropertyCount() > 0) {
                // recurse into another node to process its nodes/primitives
                getStringArrayResponse((SoapObject)obj1, strings);
            }
        } else if (obj1 instanceof SoapPrimitive) {
            strings.add(((SoapPrimitive)obj1).toString());
        }
    }
    // only make this for the original caller
    if (isFirstCall) {
        return (String[])strings.toArray(new String[strings.size()]);
    }
    return null;
}
}

我对服务器端进行了硬编码,以返回一个字符串 + 我发送给它的参数。现在我得到的只是硬编码部分,似乎我添加到 soap objets 的参数从未被服务器接收到。

已经尝试: -) 从 Web 服务的命名空间中删除“http://” -) 不使用“envelope.dotNet = true;” -) 将属性直接添加到请求中

知道哪里出了问题吗???

最佳答案

这些行让我感到困惑:

PropertyInfo pi = new PropertyInfo();
pi.setName("Nombre");
pi.setValue("RIQUELME");
pi.setType(int.class);
Request.addProperty(pi);

为什么使用字符串(“RIQUELME”)作为整数的值?

运行代码时会发生什么?尝试设置 envelope.debug = true。尝试设置断点并检查 envelope.requestDumpenvelope.responseDump 的值。这样您就可以看到发送的内容和接收的内容。

关于java - KSoap2 + Android + .net Ws = Null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4952016/

相关文章:

java - 与Spring集成测试: Cannot convert value of type error

java - 将大量二进制数据加载到原始 JVM 字节码中的 `[B` 中

java - 如何使用 android-ksoap2 构建正确的 XML 请求?

android - 空值也插入到 android 中的 mysql 中

android - 返回错误响应的 SOAP Web 服务

java - 如何编译Rust代码使其功能可以通过Java调用

java - 数值未输入微调器中

android - viewPager 背景颜色对我的图像的影响

java - 私有(private)字符串还是公共(public)静态字符串?

java - 无法将 HttpURLConnection 方法切换为 POST