2016-08-17 27 views
2

我在Android中創建一個新項目時創建了一個默認的登錄活動。 默認情況下,創建一個可正常登錄並正確顯示的progressBar。所有這些都通過AsyncTask完成。改造2 - 如何顯示進度欄異步?

現在我使用Retrofit 2進行異步登錄。

當我按下按鈕登錄推出了內容的方法:

.... 
    .... 
    showProgress(true); 
    call.enqueue(new Callback<User>() { 

     @Override 
     public void onResponse(Call<User> call, retrofit2.Response<User> response) { 

      try { 
       Thread.sleep(3000); 
      } catch (InterruptedException e) { 
       e.printStackTrace(); 
      } 

      User userdatos = response.body(); 

       if(userdatos != null){ 
        // redirect to Main Activity page 
        showProgress(false); 
        Intent i = new Intent(LoginActivity.this, MainActivity.class); 
        startActivity(i); 
       } 
       else{ 
        showProgress(false); 
        mPasswordView.setError(getString(R.string.error_incorrect_password)); 
        mPasswordView.requestFocus(); 
       } 
     } 
     @Override 
     public void onFailure(Call<User> call, Throwable t) { 
      call.cancel(); 
      showProgress(false); 
      Toast.makeText(LoginActivity.this, "Please check your network connection and internet permission", Toast.LENGTH_LONG).show(); 
     } 
    }); 

我被迫一個的Thread.sleep(3000)來檢查。 沒有進度條出現,調試顯示: 跳過180幀!該應用程序可能在IT​​S主線程上做了太多工作。

這怎麼解決?

我會有很多異步調用,我總是會顯示一個進度條來等待響應完成。

謝謝

+0

您不應該在UI線程上調用'Thread.sleep()'。 – EpicPandaForce

+0

mmmm .... oki ...但: 從那時起我模擬請求需要幾秒鐘?爲確保顯示進度條? 此外,我認爲使用Retrofit 2進行調用以使Async,AsyncTask。 – daicon

+1

我認爲問題出在你的showProgress實現上,或者那個調用執行的很快,而且你無法看到進度條,因爲它是立即顯示/隱藏的。如果添加延遲而不是睡眠(處理程序帖子),您將看到一個進度條。 – Beloo

回答

0

讓我與大家分享了由Android Studio和我的改造實現創建的默認登錄活動礦代碼實現。默認登錄活動,包括AsyncTask,其中調用UserLoginTask,在裏面,我做了我所有的登錄邏輯的東西。我使用.execute代替.enqueue因爲它同步,我們並不需要新的線程內AsyncTask

public class UserLoginTask extends AsyncTask<Void, Void, Boolean> { 

     private final String mEmail; 
     private final String mPassword; 

     UserLoginTask(String email, String password) { 
      mEmail = email; 
      mPassword = password; 
     } 

     @Override 
     protected Boolean doInBackground(Void... params) { 
      Retrofit restAdapter = new Retrofit.Builder() 
        .baseUrl(Constants.ROOT_API_URL) 
        .addConverterFactory(GsonConverterFactory.create()) 
        .build(); 
      IConstructSecureAPI service = restAdapter.create(IConstructSecureAPI.class); 
      Call<JsonElement> result = service.getToken(mEmail, mPassword, "password"); 
      try { 
       //using sync request and get response 
       JsonElement element = result.execute().body(); 
       if(element!=null) { 
        //do whatever you want with your response 
       } 
      } catch (IOException e) { 
       e.printStackTrace(); 
       return false; 
      } 
      return true; 
     } 

     @Override 
     protected void onPostExecute(final Boolean success) { 
      mAuthTask = null; 
      showProgress(false); 

      if (success) { 
       if(clientType != 2){ 
        mPasswordView.setError(getString(R.string.error_incorrect_password)); 
        mPasswordView.requestFocus(); 
        clientType = 0; 
       } 
       else { 
        Intent intent = new Intent(getApplicationContext(), MainActivity.class); 
        startActivityForResult(intent, 1); 
       } 
//    finish(); 
      } else { 
       mPasswordView.setError(getString(R.string.error_incorrect_password)); 
       mPasswordView.requestFocus(); 
      } 
     } 

     @Override 
     protected void onCancelled() { 
      mAuthTask = null; 
      showProgress(false); 
     } 

    } 
+0

感謝您的代碼。 在我的一個測試中,我做了。 但我有一個問題: 什麼可以是我們想要使用Rynctrit 2 Sync與AsyncTask的原因,可以使用Retrofit 2 Async? 謝謝 – daicon

+0

@daicon當然可以,但在您的問題中,我沒有看到您的所有活動代碼。我不明白你寫的睡眠。 一般來說,你的問題對我來說不是很清楚,你是否有改裝或用戶界面出現異常的問題,或只看到警告「ITS主線程工作太多」? – neustart47

0

無恥促進

我爲創建RxLoading library,就可以做到這一點,多少更多的,

你可以做這樣的事情:

networkCall().compose(RxLoading.<>create(loadingLayout)).subscribe(...); 

它由2個類組成,一個自定義視圖(loadingLayout)和一個將它們粘在一起的transformer的RxLoading,您可以選擇使用它們中的任何一個或兩個。

雖然你需要使用retrofit2和RxJava。

RxLoading還支持空和錯誤狀態(帶內置重試機制,所以一定要檢查這個選項,以及)

你可以找到更多的GitHub page