سؤال

I'm trying to access the web service defined in WSDL from Android activity. The expected output is json formatted string. At the first step my aim is to print this string to a text view in Android. So I used following code :

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;
}
}

But when I'm trying to run the code I can see following error in log cat (And no output in Text view):

java.lang.ClassCastException: java.util.Vector

Here is the complete log cat out put :

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)

What can be the error ? please help me I'm stuck here.

Thank you in advance!

هل كانت مفيدة؟

المحلول

Try the below

Replace this

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

By

     SoapObject response=(SoapObject) envelope.bodyIn;

I tried your code this is the result i got

enter image description here

نصائح أخرى

ClassCastException is thrown when you try to cast invalid types. For example, let's say you have an ArrayList v. If you do String s=(String)v, it will throw an exception because you can't convert an ArrayList to a String. In this case, the only casting you're doing is SoapPrimitive response = (SoapPrimitive) envelope .getResponse(); Which means that envelope.getResponse() does not return a SoapPrimitive. It probably returns a java.util.Vector, based on the error message.

I've never used whatever libraries you're using, so I can't give you a definite answer. However, envelope.getResponse() ClassCastException on google gives me a lot of results. Try that, and good luck!

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top