Java KSoap2和x2B;安卓&x2B。net Ws=Null
在连接到.net Web服务时,我在android项目中使用Ksoap2时遇到了一些问题。 只要我调用Ws-witouth参数,一切都正常,但是当我尝试添加参数时,服务器永远不会得到它们。 这是我的密码Java KSoap2和x2B;安卓&x2B。net Ws=Null,java,android,ksoap2,android-ksoap2,Java,Android,Ksoap2,Android Ksoap2,在连接到.net Web服务时,我在android项目中使用Ksoap2时遇到了一些问题。 只要我调用Ws-witouth参数,一切都正常,但是当我尝试添加参数时,服务器永远不会得到它们。 这是我的密码 import java.util.Vector; import org.ksoap2.SoapEnvelope; import org.ksoap2.serialization.PropertyInfo; import org.ksoap2.serialization.SoapObject;
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;
}
}
import java.util.Vector;
导入org.ksoap2.SoapEnvelope;
导入org.ksoap2.serialization.PropertyInfo;
导入org.ksoap2.serialization.SoapObject;
导入org.ksoap2.serialization.SoapPrimitive;
导入org.ksoap2.serialization.SoapSerializationEnvelope;
导入org.ksoap2.transport.AndroidHttpTransport;
导入android.app.Activity;
导入android.os.Bundle;
导入android.widget.ArrayAdapter;
导入android.widget.ListView;
导入android.widget.TextView;
导入android.widget.Toast;
公共类服务Web扩展活动{
SoapObject响应;
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//字符串命名空间=”http://tempuri.org/";
String NAMESPACE=“IDBN.WS”;
字符串方法\u NAME=“getClientesByName”;
//字符串SOAP_ACTION=”http://tempuri.org/getClientesByName";
字符串SOAP_ACTION=“IDBN.WS/getClientesByName”;
//字符串URL=”http://www.ws.idbnar2.com.ar/wsClientes.asmx";
字符串URL=”http://www.ws.idbnar2.com.ar/wsClientes.asmx";
SoapObject请求=新的SoapObject(名称空间、方法名称);
PropertyInfo pi=新的PropertyInfo();
pi.集合名(“Nombre”);
pi.设定值(“RIQUELME”);
pi.setType(int.class);
请求。添加属性(pi);
SoapSerializationEnvelope=新的SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet=true;
envelope.setOutputSoapObject(请求);
AndroidHttpTransport AndroidHttpTransport=新AndroidHttpTransport(URL);
ListView Lista=(ListView)findViewById(R.id.Lista);
尝试
{
调用(SOAP_操作,信封);
response=(SoapObject)envelope.getResponse();
String[]Clientes=getStringArrayResponse(响应,null);
setAdapter(新的ArrayAdapter(这个,android.R.layout.simple_list_item_1,clients));
}
捕获(例外e)
{
Toast Toast=Toast.makeText(例如,getMessage(),Toast.LENGTH\u LONG);
toast.show();
}
}
公共字符串[]getStringArrayResponse(SoapObject节点,向量字符串){
布尔值isFirstCall=false;
if(strings==null){
isFirstCall=true;
字符串=新向量();
}
int count=response.getPropertyCount();
for(int i=0;i0){
//递归到另一个节点以处理其节点/原语
getStringArrayResponse((SoapObject)obj1,字符串);
}
}else if(SoapPrimitive的obj1实例){
add(((SoapPrimitive)obj1.toString());
}
}
//仅为原始呼叫者设置此选项
如果(isFirstCall){
返回(String[])strings.toArray(新字符串[strings.size()]);
}
返回null;
}
}
我对服务器端进行了harcode,以返回字符串+我发送的参数。。现在我得到的只是硬编码部分,似乎我添加到soap对象中的参数永远不会被服务器接收到
Allready尝试:
-)从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.requestDump
和envelope.responseDump
的值。这样,您可以看到发送的内容和接收的内容
pi.setName("Nombre");
pi.setValue("RIQUELME");
pi.setType(int.class);
Request.addProperty(pi);
pi.setValue应为整数(1、2等)
或
pi.setType应为string.class
或者,您不需要pi.setType。如果删除该行,它应该可以工作