2

文字列値 "1" または "-1" 、数値を string で返す Web サービスがあります。

文字列から org.ksoap2.serialization.SoapPrimitive にキャストできない Asunc タスクの使用でエラーが発生しました

完全な 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);

        }
    }

}
}

完全なログ エラー:

     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
4

3 に答える 3

8

これは私に一度起こりました。私は から に変更しSoapPrimitiveObjectキャストしました。試してみてください。これは関係ないかもしれませんが、 dotnetでWeb サービスを記述して いる場合は、必ず;を使用してエンベロープで指定してください。 envelop.dotnet = true

于 2013-10-15T23:22:57.577 に答える
5

私は同じ問題を抱えていて、Jacky Nguyenが言ったように解決しました.
これが例です。お役に立てば幸いです。

交換:

SoapPrimitive resultRequestSOAP = null;

Object resultRequestSOAP = null;

そして置き換えます:

resultRequestSOAP = (SoapPrimitive) envelope.getResponse();

resultRequestSOAP = (Object) envelope.getResponse();
于 2014-05-14T09:53:42.700 に答える