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

  • SEARCH
  • Home
  • 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 9179389
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:47:02+00:00 2026-06-17T17:47:02+00:00

I am working on an Application which is using the AsyncTask. I am trying

  • 0

I am working on an Application which is using the AsyncTask. I am trying to connect to my website, check user login details and return true if user exists and false if not found. Based on the response I will display an error message or load up the relevant screen for the user.

However I am wondering if the below code should be placed in the onClick listener of my login button or in the onPreExecute() method of the AsyncTask?

I call the asyncTask like this

public void onClick(View v) {
            //Call background task
            new HttpTask().execute(url);
        }
    });

Does it matter where I call the below code? Should it ideally be in the onPreExecute() method?

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.mysite.com/checklogindetails.php");

try {
    // Add your data
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
    nameValuePairs.add(new BasicNameValuePair("username", name));
    nameValuePairs.add(new BasicNameValuePair("password", pass));
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

    // Execute HTTP Post Request
    HttpResponse response = httpclient.execute(httppost);

} catch (ClientProtocolException e) {
    // TODO Auto-generated catch block
} catch (IOException e) {
    // TODO Auto-generated catch block
}

Thanks for any info regarding this guys. It’s not a problem as such, it’s more a learning curve for me and I’m curious what I should do.

thanks.

  • 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-17T17:47:03+00:00Added an answer on June 17, 2026 at 5:47 pm

    Your HTTP code needs to be in the doInBackground() method, otherwise it is still running in the main (UI) thread and can cause exceptions on new Android versions (not to mention lockups if your request takes a long time).

    Here is an example (note the constructor addition) I decided to keep the pairs inside doInBackground(). Since you haven’t given us all code, this probably won’t work right off the bat.

    public class LoginTask extends AsyncTask <String, Void, Boolean>{
    
      private String name = "", pass= "";
      public LoginTask (String name, String pass )
      {
        super();
        this.name = name;
        this.pass = pass;
      }
      @Override
      protected Boolean  doInBackground (String...url)
      { 
        boolean good = true;
    
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(url[0]);
    
        try {
          // Add your data
          List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
          nameValuePairs.add(new BasicNameValuePair("username", name));
          nameValuePairs.add(new BasicNameValuePair("password", pass));
          httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    
          // Execute HTTP Post Request
          HttpResponse response = httpclient.execute(httppost);
    
    
        } catch (ClientProtocolException e) {
          good = false;
        } catch (IOException e) {
          good = false;
        }
        return good;
      }
    
      @Override
      protected void onPostExecute (Boolean result)
      {
        Toast.makeText(MainActivity.this, result ? "Logged in": "Problem", Toast.LENGTH_SHORT).show();
      }
    }
    

    And you’d call using

    public void onClick(View v) {
                //Call background task
                new HttpTask("myusername", "mypassword").execute(url);
            }
        });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I’m working on an application which has been designed using n-tire application architecture .The
I am working on an application which sends an AJAX POST request (I'm using
I am working on an application in which I am using UIScrollview , in
I have been working on a facebook application which will be using Graph API
I have been working with iPhone application in which i am using sqlite database.
I am working on a web application (using Grails) which will generate a gift
I'm working on a MVC application in which the Model is implemented using an
I am working on big application build using ASP.NET which was started 1.5 years
I'm working on an android tablet application using phonegap in which i need to
I'm working on Android application which is taking content from internet using JSON, I

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.