我在调用webservice时遇到问题.我在服务器上有一个.NET Web服务,我在Android中使用KSOAP2(ksoap2-j2se-full-2.1.2).在运行程序时,我得到了一个运行时异常,如"org.ksoap2.serialization.SoapPrimitive".我该怎么办?
这是我的代码.
package projects.ksoap2sample; import org.ksoap2.SoapEnvelope; import org.ksoap2.serialization.SoapObject; import org.ksoap2.serialization.SoapSerializationEnvelope; import org.ksoap2.transport.HttpTransportSE; import android.app.*; import android.os.*; import android.widget.TextView; public class ksoap2sample extends Activity { /** Called when the activity is first created. */ private static final String SOAP_ACTION = "http://tempuri.org/HelloWorld"; private static final String METHOD_NAME = "HelloWorld"; private static final String NAMESPACE = "http://tempuri.org/"; private static final String URL = "http://192.168.1.19/TestWeb/WebService.asmx"; TextView tv; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); tv=(TextView)findViewById(R.id.text1); try { SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); //request.addProperty("prop1", "myprop"); SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); envelope.dotNet=true; envelope.setOutputSoapObject(request); HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); androidHttpTransport.call(SOAP_ACTION, envelope); Object result = (Object)envelope.getResponse(); String[] results = (String[]) result; tv.setText( ""+results[0]); } catch (Exception e) { tv.setText(e.getMessage()); } } }
1HaKr.. 24
这很简单.你得到的结果Object
是一个原始的结果.
你的代码:
Object result = (Object)envelope.getResponse();
正确的代码:
SoapObject result=(SoapObject)envelope.getResponse(); //To get the data. String resultData=result.getProperty(0).toString(); // 0 is the first object of data.
我认为这绝对有用.
这很简单.你得到的结果Object
是一个原始的结果.
你的代码:
Object result = (Object)envelope.getResponse();
正确的代码:
SoapObject result=(SoapObject)envelope.getResponse(); //To get the data. String resultData=result.getProperty(0).toString(); // 0 is the first object of data.
我认为这绝对有用.
您的.NET Webservice如何?
我使用code.google.com中的ksoap 2.3获得了相同的效果.我按照代码项目的教程(这是很棒的BTW).
每次我使用
Integer result = (Integer)envelope.getResponse();
得到我的webservice的结果(无论类型,我尝试了Object,String,int)我遇到了org.ksoap2.serialization.SoapPrimitive
异常.
我找到了解决方案(解决方法).我要做的第一件事是从我的webservice方法中删除"SoapRpcMethod()属性.
[SoapRpcMethod(), WebMethod] public Object GetInteger1(int i) { // android device will throw exception return 0; } [WebMethod] public Object GetInteger2(int i) { // android device will get the value return 0; }
然后我将我的Android代码更改为:
SoapPrimitive result = (SoapPrimitive)envelope.getResponse();
但是,我得到一个SoapPrimitive对象,它有一个私有的"值"字段.幸运的是,值通过该toString()
方法传递,因此我Integer.parseInt(result.toString())
用来获取我的值,这对我来说已经足够了,因为我没有任何需要从我的Web服务获得的复杂类型.
这是完整的来源:
private static final String SOAP_ACTION = "http://tempuri.org/GetInteger2"; private static final String METHOD_NAME = "GetInteger2"; private static final String NAMESPACE = "http://tempuri.org/"; private static final String URL = "http://10.0.2.2:4711/Service1.asmx"; public int GetInteger2() throws IOException, XmlPullParserException { SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); PropertyInfo pi = new PropertyInfo(); pi.setName("i"); pi.setValue(123); request.addProperty(pi); SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); envelope.dotNet = true; envelope.setOutputSoapObject(request); AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL); androidHttpTransport.call(SOAP_ACTION, envelope); SoapPrimitive result = (SoapPrimitive)envelope.getResponse(); return Integer.parseInt(result.toString()); }
Typecast SoapPrimitive的信封:
SoapPrimitive result = (SoapPrimitive)envelope.getResponse(); String strRes = result.toString();
它会起作用.