2012-11-22 23 views
0

在我的服務中我在服務的doInBackground()方法中使用http://loopj.com/android-async-http/ 。因爲它是異步的,所以方法在調用回調之前完成,因此onPostExecute被調用並關閉服務......我該如何避免這種情況?在Android服務中製作異步請求

public class LoginService extends AsyncTask<String, Void, LoginService.LoginStatus> { 

    private static String TAG = "x-LoginService"; 
    private ProgressDialog progressDialog; 
    private AlertDialog dialog = null; 

    private final Context context; 

    public LoginService(Context context) { 
     this.context = context; 
    } 

    @Override 
    protected void onPreExecute() { 
     progressDialog = ProgressDialog.show(context, "", context.getString(R.string.waitingLogin), true); 
    } 

    @Override 
    protected void onPostExecute(LoginStatus loginStatus) { 
     progressDialog.dismiss(); 
     Log.d(TAG, "--STARTONPOSTEXECUTE"); 
     String message; 
     LocalSettingsService settings = new LocalSettingsService(context); 

     if (loginStatus == LoginStatus.LOGGED_IN) { 
      settings.put("loggedIn", "true"); 

      Intent intent = new Intent(context, FragmentTabs.class); 
      context.startActivity(intent); 

      //Intent intent = new Intent(context, SummaryPage.class); 
      //Intent intent = new Intent(context, FeedbackPage.class); 
      //Intent intent = new Intent(context, NavTab.class); 
      //context.startActivity(intent); 

      return; 

     } else if (loginStatus == LoginStatus.INVALID_CREDENTIALS) { 
      settings.put("loggedIn", "false"); 

      message = context.getString(R.string.invalidCredentials); 
     } else { 
      settings.put("loggedIn", "false"); 
      message = context.getString(R.string.serverError); 
     } 

     dialog = new AlertDialog.Builder(context) 
       .setIcon(android.R.drawable.ic_dialog_alert) 
       .setTitle(context.getString(R.string.errorTitle)) 
       .setMessage(message) 
       .setPositiveButton(context.getString(R.string.ok), new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialogInterface, int i) { 
         dialogInterface.dismiss(); 
        } 
       }).create(); 

     dialog.show(); 
    } 

    @Override 
    protected LoginStatus doInBackground(String... strings) { 
     String username = strings[0]; 
     String password = strings[1]; 

     doLogin(); 

     return LoginStatus.LOGGED_IN; 
    } 

    private void doLogin() { 
    { 
     Log.d(TAG, "--STARTDOLOGIN"); 
     RequestParams params = new RequestParams(); 
     params.put("username", "un"); 
     params.put("password", "pw"); 
     ServicesRestClient.post("ajax/login", params, new JsonHttpResponseHandler() { 
      @Override 
      public void onSuccess(String s) { 
       Log.d(TAG, "--ONSUCCESS"); 

      } 
      @Override 
      public void onFailure(Throwable throwable, String s) { 
       Log.d(TAG, "--ONFAILURE"); 
      } 
     }); 
    } 

    } 

    public void onPause() { 
     if (dialog != null) { 
      dialog.dismiss(); 
     } 
    } 

    public static enum LoginStatus { 
     LOGGED_IN, INVALID_CREDENTIALS, SERVER_SIDE_ERROR 
    } 
} 
+0

顯示代碼... –

+0

對不起,完成..... – Baconbeastnz

回答

1

我覺得你這個代碼太複雜了。一般來說,你應該保持在doInBackground(),除非你服務結束,但不知道你使用的內部我可以告訴如何做到最好。但既然你使用這個庫宣佈要做異步網絡,我不會使用另一個異步任務在第一

+0

必須有一種方法可以做這似乎是您使用服務最常見的情況。 I.E異步請求來自遠程服務器的數據... – Baconbeastnz

+0

生成異步任務以產生執行異步操作的服務並不常見。你通常做**一個**異步工作。我使用AsyncTask並自己組網 –