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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:59:00+00:00 2026-06-13T11:59:00+00:00

I am trying to write a Http API in android. I am using a

  • 0

I am trying to write a Http API in android. I am using a AsyncTask to run the calls to my web service.I am not interested in updating the UI, instead all I want is the data to use in my application logic. This is what I have so far:

public class DataManager{
    public static String result;

    public DataManager ()
    {

    }


    public String get ()
    {
        User user = new User ();
        user.execute("http://someuri/service/users/id/21001");
        return user.getResult();
    }
}


public class User extends AsyncTask <String,Void,String>{

    private String result;
    @Override
    protected String doInBackground(String... arg0) 
    {
        HttpClient client = new DefaultHttpClient();
        HttpGet get = new HttpGet (arg0[0]);

        try
        {
            HttpResponse response = client.execute (get);
            if (response.getStatusLine().getStatusCode () == 200)
            {
                HttpEntity entity = response.getEntity();
                return EntityUtils.toString(entity);
            }

        } 
        catch (ClientProtocolException e) 
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }


    @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        this.result = result;
    }

    public String getResult ()
    {
        return result;
    }

}

I want a typical call to be:
DataManager manager = new DataManager ();
String value = manager.get ();

But when I run this I get back null. What is causing this and how can I refactor this code to get the desired behavior.

  • 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-13T11:59:01+00:00Added an answer on June 13, 2026 at 11:59 am

    The whole idea of a thread is that it runs concurrently. Basically, here’s what you’re doing:

    User user = new User (); // Create User object
    user.execute("http://someuri/service/users/id/21001"); // Start thread
    return user.getResult(); // Return thread result
    

    However, there is no time for the thread to run between “start” and “return result”.

    I would suggest using some kind of callback; first, make get() return void, and remove the return statement. Then, you pass in some object which implements YourCallback, and then call onCallback(result) from within onPostExecute().

    Your calling code would then look like this:

    DataManager x = new DataManager();
    x.get(new YourCallback() {
        public void onCallback(String result) {
            // ...
        }
    });
    

    There is a much fuller example in this fantastic answer.

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

Sidebar

Related Questions

I'm trying to write a voip app for Android, using the Android SIP API
Trying to write app for service technicians that will display open service calls within
I'm trying to write a hard-coded HTTP Post request with SOAP action, using the
Trying to obtain blog followers using Tumblr's API and RESTConsole in Chrome (not building
I'm trying to write some Thread Management utility, and reading up on ThreadMXBean. http://download.oracle.com/javase/6/docs/api/java/lang/management/ThreadMXBean.html#dumpAllThreads(boolean,
I'm trying to write an MQL query to be executed using Freebase API's. I
I'm trying to write a Tasks app for Android, using the v1.3 library here:
I am using libevent and its http API to write a simple HTTP server
i am trying to write a API using JSP/JSTL to generate JSON output but
I'm trying to write a JSON-based web API in a Sinatra app. I want

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.