Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8880751
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:13:07+00:00 2026-06-14T20:13:07+00:00

in my Service I am using http://loopj.com/android-async-http/ within the doInBackground() method of a service.

  • 0

in my Service I am using http://loopj.com/android-async-http/
within the doInBackground() method of a service. Because it’s asynchronous, the method finishes before the callbacks are called, and therefore onPostExecute is being called and shutting the service down… How can I avoid this?

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T20:13:08+00:00Added an answer on June 14, 2026 at 8:13 pm

    I think you this code too much complicated. In general you should somehow stay in doInBackground() unless you service ends, but not knowing the internals of what you use I can tell how to do it best. But since this library you use announces to be doing asynchronous networking, I’d not use another async task in first place

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using the AsyncHttpClient library from http://loopj.com/android-async-http/ and have it calling web services fine
I'm setting up a net.tcp WCF service using instructions here: http://blogs.msdn.com/swiss_dpe_team/archive/2008/02/08/iis-7-support-for-non-http-protocols.aspx One of the
I am using the following code to invoke the http://www.highoncoding.com/Customers.asmx web service. UPDATE 1:
Im using wordpress 3.0 here: http://www.adventure-service.com On homepage i am using template that creates
I'm building an Android app that will communicate with a REST service using HTTP
I wrote some android application that get xml from some service ( using http
I am adding a service reference in VS.NET 2010 using http://[orgname].api.crm.dynamics.com/XrmServices/2011/Organization.svc?wsdl URL but it
I'm creating a pretty simple HTTP service using OpenRasta. For HEAD requests, the HTTP
I have this web service : using System.Web.Services; namespace Contracts { [WebService(Namespace = http://tempuri.org/)]
I have a simple WCF service hosted in IIS7 using the HTTP protocol. The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.