Question

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

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

full AsuncTask class code :

 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$3.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$1.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$2.call(AsyncTask.java:287)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
... 5 more
Était-ce utile?

La solution

This happened to me once, i changed from SoapPrimitive to 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 in dotnet, make sure you specify that in your envelop by using envelop.dotnet = true;

Autres conseils

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

Replace:

SoapPrimitive resultRequestSOAP = null;

by

Object resultRequestSOAP = null;

and replace:

resultRequestSOAP = (SoapPrimitive) envelope.getResponse();

by

resultRequestSOAP = (Object) envelope.getResponse();
SoapPrimitive  response = (SoapPrimitive) envelope.getResponse();
  string result = response.toString();
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top