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

天涯浪子 提交于 2019-12-23 04:18:27

问题


I am doing soap parsing in android .These are the following steps I take while doing parsing 1) I am download ksoap2-android-assembly-2.4-jar-with-dependencies (1).jar . 2) Then I used this in this code But I am getting this error .

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

Here is my code.

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

}

回答1:


The exception is on :

resultRequestSOAP = (SoapObject) envp.getResponse();

Because you can't cast the response get form SoapSerializationEnvelope into SoapObject




回答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();



回答3:


Change this: SoapPrimitive resultsString = (SoapPrimitive)envp.getResponse();

to

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



来源:https://stackoverflow.com/questions/18892434/java-lang-classcastexception-org-ksoap2-serialization-soapprimitive-cannot-be-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!