Java 字符串不能转换为 org.ksoap2.serialization.SoapPrimitive

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19392541/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 16:46:00  来源:igfitidea点击:

string can not cast to org.ksoap2.serialization.SoapPrimitive

javaandroidandroid-asynctaskandroid-ksoap2

提问by user2884425

i have webservice that return string value "1" or "-1" , numbers but in string .

我有返回字符串值“1”或“-1”的网络服务,数字但在字符串中。

i have error in using Asunc task that can't cast from string to org.ksoap2.serialization.SoapPrimitive

我在使用无法从字符串转换为 org.ksoap2.serialization.SoapPrimitive 的 Asunc 任务时出错

full AsuncTask class code :

完整的 AsuncTask 类代码:

 public class LoginAsync extends AsyncTask<Void, Void, String> {

Activity mActivity;
Context context;

ProgressDialog progressDialog;

public LoginAsync(Activity activity, ProgressDialog progressDialog,
        Context context) {
    super();
    this.progressDialog = progressDialog;
    this.mActivity = activity;
    this.context = context;
}

@Override
protected String doInBackground(Void... voids) {
    SoapPrimitive resultRequestSOAP = null;

    try {
        final String NAMESPACE = "http://ws.sams.com";
        final String URL = "http://sams-app.com:8080/webService/services/LoginActvityWs?WSDL"; // usint
                                                                                                // //
                                                                                                // //
                                                                                                // //
                                                                                                // localhost
        final String METHOD_NAME = "login";
        final String SOAP_ACTION = "http://ws.sams.com/login";

        SharedPreferences prefs = PreferenceManager
                .getDefaultSharedPreferences(context);
        String user = prefs.getString("login1", "0");
        String pass = prefs.getString("password1", "0");
        // Calling Login Method
        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

        // First Reques for USER NAME .
        PropertyInfo pi = new PropertyInfo();
        pi.setName("username");
        pi.setValue(user);
        pi.setType(String.class);
        request.addProperty(pi);

        // Second Reques for USER NAME .
        PropertyInfo pi2 = new PropertyInfo();
        pi2.setName("password");
        pi2.setValue(pass);
        pi2.setType(String.class);
        request.addProperty(pi2);

        // Getting Request Result , Will get TID .
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                SoapEnvelope.VER11);

        envelope.setOutputSoapObject(request);

        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

        androidHttpTransport.call(SOAP_ACTION, envelope);
        resultRequestSOAP = (SoapPrimitive) envelope.getResponse();

        return String.valueOf(resultRequestSOAP);

    }

    catch (SocketTimeoutException e) {
        return "error";

    }

    catch (ConnectTimeoutException e) {
        return "error";

    }

    catch (XmlPullParserException e) {

        return "error";

    } catch (IOException e) {

        return "error";
    }

    catch (NullPointerException e) {

        return "error";

    }

}

@Override
protected void onPostExecute(String result) {

    // If any error oceeared duaring get TID
    if (result.equalsIgnoreCase("error")) {
        final AlertDialog.Builder alertDialog = new AlertDialog.Builder(
                mActivity);

        alertDialog.setTitle("???? ????? ???????? ?? ???????");
        alertDialog.setMessage("?? ??? ???????? ????? ? ");
        // Retry Button Action
        alertDialog.setPositiveButton("???",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        LoginAsync asynTask = new LoginAsync(mActivity,
                                progressDialog, mActivity
                                        .getApplicationContext());
                        asynTask.execute();
                    }
                });

        // No Button Action
        alertDialog.setNegativeButton("??",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        progressDialog.dismiss();
                    }
                });

        alertDialog.show();

    }
    // IF pass or user Filed .
    else if (Integer.parseInt(result.toString()) == -1) {

        final AlertDialog.Builder alertDialog = new AlertDialog.Builder(
                mActivity);

        alertDialog.setTitle("??? ???????? ?? ???? ?????? ?????");
        alertDialog.setMessage("?? ??? ????? ????? ?????? ");

        alertDialog.setPositiveButton("???",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {

                        progressDialog.dismiss();

                    }
                });
        alertDialog.setNegativeButton("??",
                new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        progressDialog.dismiss();
                        Toast.makeText(mActivity.getApplicationContext(),
                                "thank you for using SAMS app",
                                Toast.LENGTH_LONG).show();
                        mActivity.finish();
                    }
                });

        alertDialog.show();

    }

    else if (Integer.parseInt(result.toString()) == 0) {

        SharedPreferences prefs = PreferenceManager
                .getDefaultSharedPreferences(context);
        prefs.edit().putInt("TID", Integer.parseInt(result.toString()))
                .commit();

        Toast.makeText(mActivity.getApplicationContext(),
                result.toString(), Toast.LENGTH_LONG).show();

        GetClassesListAsync asynTask = new GetClassesListAsync(mActivity,
                progressDialog, context);
        asynTask.execute();

    }

    // For correct Login !
    else {
        progressDialog.dismiss();
        if (Integer.parseInt(result.toString()) >= 1) {

            SharedPreferences prefs = PreferenceManager
                    .getDefaultSharedPreferences(context);
            prefs.edit().putInt("TID", Integer.parseInt(result.toString()))
                    .commit();

            Intent intent1 = new Intent(context, DashBoard.class);
            mActivity.startActivity(intent1);

        }
    }

}
}

Full Log Error :

完整日志错误:

     FATAL EXCEPTION: AsyncTask #1
 java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask.done(AsyncTask.java:299)
at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at android.os.AsyncTask$SerialExecutor.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at java.lang.Thread.run(Thread.java:856)
 Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to     org.ksoap2.serialization.SoapPrimitive
at com.app.sams.LoginAsync.doInBackground(LoginAsync.java:82)
at com.app.sams.LoginAsync.doInBackground(LoginAsync.java:1)
at android.os.AsyncTask.call(AsyncTask.java:287)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
... 5 more

采纳答案by ngunha02

This happened to me once, i changed from SoapPrimitiveto Object, then cast it, and it worked. So give it a try. This is may not be relevant but if you are writing your web service indotnet, make sure you specify that in your envelop by usingenvelop.dotnet = true;

这发生在我身上一次,我从SoapPrimitive改为Object,然后投射它,它起作用了。所以试试吧。这可能无关紧要,但如果您在dotnet中编写 Web 服务,请确保在信封中使用envelop.dotnet = true;

回答by paponi

I had the same problem and solve it like Hymany Nguyen said.
Here is the example, hope it helps!

我遇到了同样的问题,并像 Hymany Nguyen 所说的那样解决了它。
下面是例子,希望对你有帮助!

Replace:

代替:

SoapPrimitive resultRequestSOAP = null;

by

经过

Object resultRequestSOAP = null;

and replace:

并替换:

resultRequestSOAP = (SoapPrimitive) envelope.getResponse();

by

经过

resultRequestSOAP = (Object) envelope.getResponse();

回答by unalCTR

SoapPrimitive  response = (SoapPrimitive) envelope.getResponse();
  string result = response.toString();