2013-10-15 78 views
0

我有webservice,返回字符串值「1」或「-1」,但字符串中的數字。字符串不能轉換爲org.ksoap2.serialization.SoapPrimitive

我有錯誤的使用不能從字符串轉換爲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 

回答

7

這發生在我身上一次,我從SoapPrimitive更改爲Object,然後投它,它的工作。所以試試吧。這可能並不相關,但是如果您正在使用dotnet編寫Web服務,請確保使用envelop.dotnet = true;在您的信封中指定該服務。

4

我遇到了同樣的問題,就像Jacky Nguyen說的那樣解決問題。
這裏是例子,希望它有幫助!

替換
// SoapPrimitive resultRequestSOAP = null;
by
Object resultRequestSOAP = null;

和替換

// resultRequestSOAP =(SoapPrimitive)envelope.getResponse();
by
resultRequestSOAP =(Object)envelope.getResponse();

+0

我不得不這樣做正好相反......對象原始...謝謝 – jasonflaherty

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

能否請您詳細闡述更多的答案增加多一點的描述關於你所提供的解決方案? – abarisone

+1

你可以改變你的代碼: resultRequestSOAP =(SoapPrimitive)envelope.getResponse(); return(resultRequestSOAP).toString(); – unalCTR