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 7960887
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:53:10+00:00 2026-06-04T04:53:10+00:00

I’m developing an application in Android. The application can post a HTTP request to

  • 0

I’m developing an application in Android. The application can post a HTTP request to specific web server. That post request must run asyncronously, so I create a thread to do the job. But I need a callback that will be called at thread end and it must be called from thread that call the `post` method.

My post method looks like this:

interface EndCallback
{
    public void Success(String response);
    public void Fail(Exception e);
}

public void post(final String url, final List<NameValuePair> data, EndCallback callback)
{
    Thread t = Thread.currentThread();
    (new Thread()
    {
        public void run()
        {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(url);
            try
            {
                httppost.setEntity(new UrlEncodedFormEntity(data));
                HttpResponse r = httpclient.execute(httppost);

                HttpEntity en = r.getEntity();
                String response = EntityUtils.toString(en);

                //I want to call callback.Success(response)
                //here from thread t
            } 
            catch (Exception ex)
            {
                //And I want to call callback.Fail(ex)
                //here from thread t
            }
        }
    }).start();
}
  • 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-04T04:53:11+00:00Added an answer on June 4, 2026 at 4:53 am

    Creating new threads an Android is highly discouraged for most applications. This seems like the perfect place for an AsyncTask. It has built-in methods that switch between threads, without needing to manually manage thread creation.

    One approach I’ve used in a similar situation is to combine the task with an enum of possible success states:

    class HttpPostTask extends AsyncTask<Void, Void, ResponseStatus> {
    
        @Override
        protected ResponseStatus doInBackground( Void... params ){
    
            try {
                // do your HTTP stuff
                return ResponseStatus.SUCCESS;
            } catch( Exception e ){
                return ResponseStatus.FAILURE;
            }
    
        }
    
        @Override
        protected void onPostExecute( ResponseStatus status ){
            switch( status ){
            case SUCCESS:
                // run your success callback
                break;
            case FAILURE:
                // run the failure callback
                break;
            }
        }
    
    }
    
    enum ResponseStatus {
        SUCCESS,
        FAILURE
    }
    

    The doInBackground method will be run in a separate thread, managed by the OS. When that thread finishes, onPostExecute will be run on the thread that started the task, which is typically the UI thread.

    If you need to set up callback objects, just add a constructor to the HttpPostTask and do any initialization you need. Your client code will then just need to create and execute the task:

    new HttpPostTask().execute();
    

    You can also pass parameters into execute() as well, which accepts a variable number of arguments of the first generic type in the class signature. The params variable in the doInBackground is an array of things that were passed into execute, all of the same type.

    Passing params into execute is useful if, for example, you wanted to post to multiple URLs. For most dependencies, setting them in the constructor is simplest approach.

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

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a

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.