java.lang.ClassCastException:org.ksoap2.serialization.SoapPrimitive无法强制转换为org.ksoap2.serialization.SoapObject

时间:2013-09-19 10:38:57

标签: java android android-layout ksoap2

我正在android中进行soap解析。这些是我在解析时采取的以下步骤 1)我下载 ksoap2-android-assembly-2.4-jar-with-dependencies(1).jar 。 2)然后我在这段代码中使用了这个但是我收到了这个错误。

**java.lang.ClassCastException: org.ksoap2.serialization.SoapPrimitive cannot be cast to org.ksoap2.serialization.SoapObject**

这是我的代码。

 package com.example.networkconnectivity;

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

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

public class MainActivity extends Activity {
    private static final String SOAP_ACTION = "http://wsendpoints.bbrailapps.firstgroup.com/getVersion";
    private static final String METHOD_NAME = "getVersion";
    private static final String NAMESPACE = "http://wsendpoints.bbrailapps.firstgroup.com";
    private static final String URL = "http://railapps.firstgroup.com/FirstGroupRailApps/services/RailAppsCAWS?wsdl";
    private SoapObject resultRequestSOAP = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        System.out.println("=====================" + haveNetworkConnection());

        new AsyncTask<Void, Void, String>() {
            ProgressDialog dialog = new ProgressDialog(MainActivity.this);

            protected void onPreExecute() {
                dialog.show();
            };

            protected String doInBackground(Void[] params) {

                resultRequestSOAP = new SoapObject(NAMESPACE, METHOD_NAME);

                SoapSerializationEnvelope envp = new SoapSerializationEnvelope(
                        SoapEnvelope.VER11);
                envp.dotNet = true;
                envp.setOutputSoapObject(resultRequestSOAP);
                AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(
                        URL);
                try {
                    androidHttpTransport.call(SOAP_ACTION, envp);
                    SoapPrimitive resultsString = (SoapPrimitive)envp.getResponse();
                    return resultRequestSOAP.toString();    

                } catch (Exception e) {
                    Log.i("WS Error->", e.toString());
                    return e.toString();
                }


            };

            protected void onPostExecute(String result) {
                dialog.dismiss();
                Toast.makeText(MainActivity.this,
                        "Check Network connectivety" + result,
                        Toast.LENGTH_LONG).show();
            };
        }.execute();

    }

    public boolean haveNetworkConnection() {
        boolean haveConnectedWifi = false;
        boolean haveConnectedMobile = false;

        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo[] netInfo = cm.getAllNetworkInfo();
        for (NetworkInfo ni : netInfo) {
            if (ni.getTypeName().equalsIgnoreCase("WIFI"))
                if (ni.isConnected())
                    haveConnectedWifi = true;
            if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
                if (ni.isConnected())
                    haveConnectedMobile = true;
        }
        return haveConnectedWifi || haveConnectedMobile;
    }

}

3 个答案:

答案 0 :(得分:2)

例外是:

resultRequestSOAP = (SoapObject) envp.getResponse();

因为您无法将回复获取表单SoapSerializationEnvelope转换为SoapObject

答案 1 :(得分:2)

if your web method returns json response as json array then use

SoapObject result= (SoapObject) envelop.getResponse();

if your web method returns json response as primitive data then use

SoapPrimitive results = (SoapPrimitive)envelop.getResponse();

so for your code use

SoapObject result= (SoapObject) envelop.getResponse();

答案 2 :(得分:1)

改变这个: SoapPrimitive resultsString =(SoapPrimitive)envp.getResponse();

SoapObject resultsString =(SoapObject)envp.getResponse();