gpt4 book ai didi

android - 如何使用 KSOAP2 从 Android 调用 .NET Web 服务?

转载 作者:IT王子 更新时间:2023-10-28 23:46:05 27 4
gpt4 key购买 nike

我在调用网络服务时遇到问题。我在服务器上有一个 .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());
}
}
}

最佳答案

这很简单。您将结果放入一个原始的 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.

我认为这绝对可以。

关于android - 如何使用 KSOAP2 从 Android 调用 .NET Web 服务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1052300/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com