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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:35:06+00:00 2026-06-12T12:35:06+00:00

I have a thread which get some data from Internet. It seams that it

  • 0

I have a thread which get some data from Internet. It seams that it is executed correctly and data is retrieved. However if I call a method which should return data it leaves me with null. From that I drew a conclusion that thread is somehow stopped just before finning.

Here is the code:

private class getHash extends AsyncTask<String, Void, String>{
    @Override
    protected String doInBackground(String... params) {
        String str = null;
        try {
            // Create a URL for the desired page
            URL url = new URL(params[0]);

            // Read all the text returned by the server
            InputStream is =  url.openStream();
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader in = new BufferedReader(isr);
            str = in.readLine();
            is.close();
            isr.close();
            in.close();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        hash = str; //If I set a global variable here it gets passed without a hitch
        return str;
    }
    @Override
    protected void onPostExecute(String result) {
        hash = result; // If I comment the line above and live this one I left with a null
    }
}

EDIT:
As requested adding code where the thread was called:

            getHash hashThread =  new getHash();
            hashThread.execute(new String[] {"http://www.full.path/to/the/file.hash"});


            if(hash != null && !hash.equals(localHash)){
....
  • 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-12T12:35:07+00:00Added an answer on June 12, 2026 at 12:35 pm

    Whatever launched the AsyncTask

    {
     ....
     getHash hashThread =  new getHash(this);
     hashThread.execute(new String[] {"http://www.full.path/to/the/file.hash"});
     return; // ok now we just have to wait for it to finish ... can't read it until then
    }
    
    // Separate callback method
    public void onHashComplete(String hash) {
    
       if(hash != null && !hash.equals(localHash)) {
          ....
       }
       ....
     }
    

    Now in your GetHash class

    public String doInBackground(String[] params) {
        .... // don't set hash here ... it will work but you will probably read it at the wrong time.
        return str;
    }
    
    public void onPostExecute(String str) {
        onHashComplete(str); // or just do all the work in here since it is a private inner class
    }
    

    ….

    Hopefully that helps. Remember doInBackground() happens on the AsyncTask thread, onPostExecute() executes on the main Thread. Whatever thread called execute() is should also be the main thread. Because of the way the main thread works, you can’t expect the onPostCreate() to occur until whatever callback that it was using to call execute() in the first place, finishes. So that is why I add the return.

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

Sidebar

Related Questions

I have an asp.net website which contains a Thread that fetches some data from
I have the following thread code which executes correct first time. After that from
I have an app in android that in a different thread from the UI(which
i am going to call a function which will retrieve some data value from
I have to make a few request to get some data from third party
I have a thread which calls Popen to get a string back from a
I have the main thread from which I start a window using invokeLater .
I have a parent thread (non-UI) which creates some child threads to do some
I have a backgorund thread that extends AsyncTask and which I use in activity
I have a program which receives data from unmanaged code about 500 to 700

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.