1

我正在尝试从 Android 活动访问在 WSDL 中定义的 Web 服务。预期的输出是 json 格式的字符串。第一步,我的目标是将此字符串打印到 Android 中的文本视图。所以我使用了以下代码:

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {
private final String NAMESPACE = "XXXXXXXXXXXXXXX";
private final String URL = "XXXXXXXXXXXXXXXXX";
private final String SOAP_ACTION = "XXXXXXXXXXXX";
private final String METHOD_NAME = "XXXXXXXXXXXXXXXXX";

private String webResponse = "";
private TextView textView;
private Thread thread;
private Handler handler = new Handler();

/** Called when the activity is first created. */

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    textView = (TextView) findViewById(R.id.textView1);
    startWebAccess();
}

public void startWebAccess() {
    thread = new Thread() {
        public void run() {
            try {
                SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
                SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                        SoapEnvelope.VER11);
                envelope.setOutputSoapObject(request);
                HttpTransportSE androidHttpTransport = new HttpTransportSE(
                        URL);

                androidHttpTransport.call(SOAP_ACTION, envelope);
                SoapPrimitive response = (SoapPrimitive) envelope
                        .getResponse();
                webResponse = response.toString();
                Log.d("TAG", webResponse);
            }

            catch (Exception e) {
                e.printStackTrace();
            }

            handler.post(createUI);
        }
    };

    thread.start();
}

final Runnable createUI = new Runnable() {

    public void run() {
        Log.d("TAG", webResponse);
        textView.setText(webResponse);
    }
};

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
}

但是当我尝试运行代码时,我可以在 log cat 中看到以下错误(并且在 Text 视图中没有输出):

java.lang.ClassCastException: java.util.Vector

这是完整的 log cat 输出:

08-06 09:57:11.792: D/dalvikvm(953): GC_FOR_MALLOC freed 771 objects / 322464 bytes in 130ms
08-06 09:57:13.252: D/dalvikvm(953): GC_FOR_MALLOC freed 3182 objects / 473904 bytes in 41ms
08-06 09:57:13.252: D/NativeCrypto(953): Freeing OpenSSL session
08-06 09:57:13.293: W/System.err(953): java.lang.ClassCastException: java.util.Vector
08-06 09:57:13.293: W/System.err(953):  at org.sahana.peoplelocator.MainActivity$2.run(MainActivity.java:56)

可能是什么错误?请帮助我,我被困在这里。

先感谢您!

4

2 回答 2

2

试试下面

替换这个

      SoapPrimitive response = (SoapPrimitive) envelope
                    .getResponse();

经过

     SoapObject response=(SoapObject) envelope.bodyIn;

我试过你的代码这是我得到的结果

在此处输入图像描述

于 2013-08-06T05:04:01.027 回答
1

当您尝试强制转换无效类型时,会引发 ClassCastException。例如,假设您有一个 ArrayList v。如果您这样做String s=(String)v,它将引发异常,因为您无法将 ArrayList 转换为 String。在这种情况下,您正在做的唯一转换是SoapPrimitive response = (SoapPrimitive) envelope .getResponse();这意味着envelope.getResponse()不返回 aSoapPrimitive.它可能会返回 a java.util.Vector,基于错误消息。

我从来没有使用过你正在使用的任何库,所以我不能给你一个明确的答案。然而,envelope.getResponse() ClassCastException在谷歌上给了我很多结果。试试看,祝你好运!

于 2013-08-06T04:57:49.733 回答