2012-10-29 127 views
0

當使用我的應用程序,它連接到互聯網,如果應用程序崩潰或在此之後被殺害我無法再次登錄,我必須從應用程序 - 停止「我的應用程序 - >停止按鈕重新登錄。onPostExecute方法不叫

當調試應用程序時,我的調試消息到達doInBackground方法的末尾,但我知道Async任務類調用onPostExecute後doInbackground方法,在我的示例中查殺後或崩潰的應用程序這是不行的。

有人可以幫我解決這個問題嗎?

private class doAuthentication extends AsyncTask<String, String, String> { 

    @Override 
    protected String doInBackground(String... params) { 



     Requester req = new Requester(); 
     String hash = req.loginByPIN(params[0]);//,VendingManagerActivity.this); 

     return hash; 
    } 

    @Override 
    protected void onPostExecute(String hash) { 
     Log.d("Vending","In post Ex-> "+hash); 
    // myProgressDialog.dismiss(); 


     if (hash.equals("")) { 

      final EditText kbdInput = (EditText) findViewById(R.id.kbdInput); 

      kbdInput.setText(""); 

      displayMessage(getString(R.string.err_wrong_credentials)); 
      return; 
     } else if (hash.equals("-")) { 

      final EditText kbdInput = (EditText) findViewById(R.id.kbdInput); 

      kbdInput.setText(""); 

      displayMessage(getString(R.string.err_no_permissions)); 
      return; 
     } else { 

      String[] resp = hash.split("#"); 


      Editor edit = mPrefs.edit(); 

      edit.putString("login_hash",resp[0]);// hash); 
      edit.putString("password",resp[1]);//lbp.reset_pass); 
      edit.commit(); 

      Intent mainScreenActivity = new Intent(
        VendingManagerActivity.this, MainScreenActivity.class); 

      startActivity(mainScreenActivity); 

      finish(); 
     } 
    } 
} 

委託類 - >方法loginByPIN - >

public String loginByPIN(String pin_code) 
{ 
    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3); 
    nameValuePairs.add(new BasicNameValuePair("module", "tablet")); 
    nameValuePairs.add(new BasicNameValuePair("method", "loginByPin")); 
    nameValuePairs.add(new BasicNameValuePair("pin", pin_code)); 
    String xmlData = getRequest(API_ADDRESS, nameValuePairs); 

    String hash = ""; 
    String response = ""; 

    try { 

     Serializer serializer = new Persister();   

     Reader reader = new StringReader(xmlData); 
     LoginByPin lbp = 
      serializer.read(LoginByPin.class, reader, false); 

     hash = lbp.hash; 

     Requester.HASH = hash; 

     Log.d("Vending", "HASH received: " + hash); 

     response = hash +"#"+lbp.reset_pass; 



    } catch (Exception e) { 

     Log.d("Vending",e.getMessage().toString()); 
     Serializer serializer = new Persister();   

     Reader reader = new StringReader(xmlData); 

     try { 

      Error err = 
       serializer.read(Error.class, reader, false); 

      Integer errorCode = err.code; 
      String errorText = err.text; 

      Log.e("Vending", "ERROR: " + errorText + "(" + Integer.toString(errorCode) + ")"); 

      if (err.code == 200) 
       return "-"; 

     } catch (Exception e2) { 

      Log.e("Vending", "ERROR: Invalid output returned from API"); 

     } 

    } 
    Log.d("Vending","Before returning-> "+hash); 
    return response;//hash; 
} 

謝謝!

編輯: 這樣

@Override 
    protected String doInBackground(String... params) { 



     Requester req = new Requester(); 
     String hash = req.loginByPIN(params[0]);//,VendingManagerActivity.this); 
     Log.d("Vending","Before doInBackground ends-> "+hash); 
     return hash; 
    } 

    @Override 
    protected void onPostExecute(String hash) { 
     Log.d("Vending","In post Ex-> "+hash); 
     myProgressDialog.dismiss(); 

和logcat的輸出不改變代碼

10-29 10:26:06.278: D/Vending(25714): Before returning-> peq8qkjvee7v4nhci8v3dub293 
10-29 10:26:06.278: D/Vending(25714): Before doInBackground ends-> peq8qkjvee7v4nhci8v3dub293#qwerty123 
+0

哪裏是logcat錯誤? –

+0

你應該爲你的連接設置一個超時時間 – thepoosh

+0

確保你的doInBackGround()執行時沒有錯誤。 – URAndroid

回答

1

如果您的應用崩潰而doInBackground()被執行,然後onPostExecute()不會被調用。

onPostExecute()只有在成功完成doInBackground()方法後纔會調用。

+0

doInBackGround方法執行返回..在此停止之後,不調用onPostExecute方法 – user1106234