如何使用 KSOAP2 从 Android 调用 .NET Web 服务?
我在调用网络服务时遇到问题。 我的服务器上有一个 .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());
}
}
}
I have a problem while calling a webservice. I have a .NET web service on the server, and I am using KSOAP2 (ksoap2-j2se-full-2.1.2) in Android. While running the program I got an runtime exception like "org.ksoap2.serialization.SoapPrimitive". What should I do?
Here is my code.
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());
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这很简单。 您将结果放入一个原始对象中。
您的代码:
正确的代码:
我认为这绝对应该有效。
It's very simple. You are getting the result into an
Object
which is a primitive one.Your code:
Correct code:
I think this should definitely work.
您的 .NET Web 服务是什么样的?
我使用 来自 code.google.com 的 ksoap 2.3 获得了相同的效果。 一句,这很棒。)
我遵循了 代码项目 上的教程(顺便说 我曾经
获取 Web 服务的结果(无论类型如何,我都尝试过 Object、String、int),但遇到了 org.ksoap2.serialization.SoapPrimitive 异常。
我找到了一个解决方案(解决方法)。 我要做的第一件事是从我的 webservice 方法中删除“SoapRpcMethod() 属性。
然后我将 Android 代码更改为:
但是,我得到了一个 SoapPrimitive 对象,它有一个私有的“值”文件。幸运的是值是通过 toString() 方法传递的,因此我使用 Integer.parseInt(result.toString()) 来获取我的值,这对我来说已经足够了,因为我没有任何需要从 Web 服务获取的复杂类型,
以下是完整的源代码:
How does your .NET Webservice look like?
I had the same effect using ksoap 2.3 from code.google.com. I followed the tutorial on The Code Project (which is great BTW.)
And everytime I used
to get the result of a my webservice (regardless of the type, I tried Object, String, int) I ran into the
org.ksoap2.serialization.SoapPrimitive
exception.I found a solution (workaround). The first thing I had to do was to remove the "SoapRpcMethod() attribute from my webservice methods.
Then I changed my Android code to:
However, I get a SoapPrimitive object, which has a "value" filed that is private. Luckily the value is passed through the
toString()
method, so I useInteger.parseInt(result.toString())
to get my value, which is enough for me, because I don't have any complex types that I need to get from my Web service.Here is the full source:
将信封类型转换为 SoapPrimitive:
它将起作用。
Typecast the envelope to SoapPrimitive:
and it will work.
您可以使用下面的代码调用 Web 服务并获取响应。确保您的 Web 服务以数据表格式返回响应。如果您使用来自 SQL Server 的数据,此代码可以帮助您 数据库。如果您使用 MYSQL,您需要更改一件事,只需替换句子
obj2=(SoapObject) obj1.getProperty(" 中的单词 NewDataSet ) NewDataSet");
by DocumentElement如果您对此有任何问题,可以给我写信。
You can Use below code to call the web service and get response .Make sure that your Web Service return the response in Data Table Format..This code help you if you using data from SQL Server database .If you you using MYSQL you need to change one thing just replace word NewDataSet from sentence
obj2=(SoapObject) obj1.getProperty("NewDataSet");
by DocumentElementIf you have any problem regarding this you can write me..
如果需要多个结果,则
getResponse()
方法将返回一个包含各种响应的Vector
。在这种情况下,有问题的代码将变为:
基于 ksoap2-android (mosabua fork) 的答案
If more than one result is expected, then the
getResponse()
method will return aVector
containing the various responses.In which case the offending code becomes:
Answer based on the ksoap2-android (mosabua fork)
我认为你不能
在主线程上调用。
网络操作应该在不同的线程上完成。
创建另一个线程或AsyncTask 来调用该方法。
I think you can't call
on main Thread.
Network operations should be done on different Thread.
Create another Thread or AsyncTask to call the method.