在Android服务中发出异步请求

时间:2012-11-22 21:33:27

标签: android android-service

在我的服务中我正在使用http://loopj.com/android-async-http/ 在服务的doInBackground()方法中。因为它是异步的,所以该方法在调用回调之前完成,因此调用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
    }
}

1 个答案:

答案 0 :(得分:1)

我觉得你这个代码太复杂了。一般情况下,你应该以某种方式留在doInBackground(),除非你的服务结束,但不知道你使用的内部我可以告诉你如何做到最好。但是,由于你使用的这个库宣布进行异步网络,我不会在第一时间使用另一个异步任务