50

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

6 回答 6

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.

我认为这绝对应该有效。

于 2010-01-12T13:26:49.157 回答
14

您的 .NET Web 服务看起来如何?

我使用来自 code.google.com 的 ksoap 2.3也有同样的效果。我遵循了The Code Project上的教程(顺便说一句,这很棒。)

每次我使用

    Integer result = (Integer)envelope.getResponse();

为了获得我的网络服务的结果(无论类型如何,我都尝试过 Object、String、int),我遇到了org.ksoap2.serialization.SoapPrimitive异常。

我找到了解决方案(解决方法)。我必须做的第一件事是从我的 web 服务方法中删除“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());
}
于 2009-10-05T17:15:09.167 回答
7

将信封类型转换为 SoapPrimitive:

SoapPrimitive result = (SoapPrimitive)envelope.getResponse();
String strRes = result.toString();

它会起作用。

于 2009-12-17T18:22:31.563 回答
4

您可以使用以下代码调用 Web 服务并获得响应。确保您的 Web 服务以数据表格式返回响应。如果您使用SQL Server数据库中的数据,此代码可以帮助您。如果您使用MYSQL,则需要改变一件事只需用 DocumentElement句子中替换单词NewDataSetobj2=(SoapObject) obj1.getProperty("NewDataSet");

private static final String NAMESPACE = "http://tempuri.org/";
private static final String URL = "http://localhost/Web_Service.asmx?"; // you can use   IP address instead of localhost
private static final String METHOD_NAME = "Function_Name";
private static final String SOAP_ACTION = NAMESPACE + METHOD_NAME;

SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("parm_name", prm_value); // Parameter for Method
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

try {
    androidHttpTransport.call(SOAP_ACTION, envelope); //call the eb service Method
} catch (Exception e) {
    e.printStackTrace();
} //Next task is to get Response and format that response

SoapObject obj, obj1, obj2, obj3;
obj = (SoapObject) envelope.getResponse();
obj1 = (SoapObject) obj.getProperty("diffgram");
obj2 = (SoapObject) obj1.getProperty("NewDataSet");

for (int i = 0; i < obj2.getPropertyCount(); i++) //the method getPropertyCount() return the number of rows
{
    obj3 = (SoapObject) obj2.getProperty(i);
    obj3.getProperty(0).toString(); //value of column 1
    obj3.getProperty(1).toString(); //value of column 2
    //like that you will get value from each column
}

如果您对此有任何问题,可以写信给我。

于 2012-05-11T12:14:07.237 回答
0

我觉得你不能打电话

 androidHttpTransport.call(SOAP_ACTION, envelope);

在主线程上。

网络操作应该在不同的线程上完成。

创建另一个 Thread 或 AsyncTask 来调用该方法。

于 2013-08-26T15:36:32.077 回答
0

If more than one result is expected, then the getResponse() method will return a Vector containing the various responses.

In which case the offending code becomes:

Object result = envelope.getResponse();

// treat result as a vector
String resultText = null;
if (result instanceof Vector)
{
    SoapPrimitive element0 = (SoapPrimitive)((Vector) result).elementAt(0);
    resultText = element0.toString();
}

tv.setText(resultText);

Answer based on the ksoap2-android (mosabua fork)

于 2011-09-27T12:45:42.227 回答