[英]KSoap2 + Android + .net Ws = Null
我在一個Android項目中使用Ksoap2時會遇到一些問題,同時連接到.net Webservice。 只要我調用Ws witouth參數一切正常,但是當我嘗試添加參數時,服務器永遠不會得到它們。 這是我的代碼
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的參數永遠不會被服務器接收。
Allready嘗試: - )從Webservice上的命名空間中刪除“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 。 如果刪除該行,它應該可以工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.