2

我正在 android 中进行肥皂解析。这些是我在解析时采取的以下步骤 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;
    }

}
4

3 回答 3

3
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();
于 2015-07-07T08:38:56.513 回答
2

例外是:

resultRequestSOAP = (SoapObject) envp.getResponse();

因为您不能将响应获取形式SoapSerializationEnvelope转换为SoapObject

于 2013-09-19T10:48:36.443 回答
1

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

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

于 2014-11-05T20:06:59.737 回答