从Android访问基于SOAP的Web服务时出现ClassCastException

时间:2013-08-06 04:48:29

标签: java android json wsdl android-ksoap2

我正在尝试从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

以下是完整的日志输出:

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)

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

提前谢谢!

2 个答案:

答案 0 :(得分:2)

尝试以下

替换此

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

通过

     SoapObject response=(SoapObject) envelope.bodyIn;

我尝试了你的代码,这是我得到的结果

enter image description here

答案 1 :(得分:1)

尝试转换无效类型时抛出ClassCastException。例如,假设您有一个ArrayList v。如果执行String s=(String)v,它将抛出异常,因为您无法将ArrayList转换为String。在这种情况下,您正在进行的唯一投射是SoapPrimitive response = (SoapPrimitive) envelope .getResponse();这意味着envelope.getResponse()不会返回SoapPrimitive.它可能会根据错误消息返回java.util.Vector

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