2012-11-22 5 views
0

내 서비스에서 서비스의 doInBackground() 메소드 내에서 http://loopj.com/android-async-http/ 을 사용하고 있습니다. 그것은 비동기이기 때문에 콜백이 호출되기 전에 메소드가 끝나기 때문에 onPostExecute가 호출되고 서비스가 종료됩니다 ... 어떻게 이것을 피할 수 있습니까?안드로이드 서비스 내에서 비동기식 요청하기

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를 사용하고 직접 네트워킹하기 –

관련 문제