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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T16:03:40+00:00 2026-06-02T16:03:40+00:00

I’m trying to implement a thread on a function that starts an HttpClient because

  • 0

I’m trying to implement a thread on a function that starts an HttpClient because it’s recomended according to d.android.com So I have implemented a thread but, it doesn’t seem to run as if I remove the thread code I see results.

This is my code:

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.chat_box);// sd
    TextView inbox = (TextView) findViewById(R.id.inbox);
    final Functions function = new Functions();
    final SharedPreferences prefs = PreferenceManager
            .getDefaultSharedPreferences(getBaseContext());
    where = prefs.getString("chat", "null");

    class SendThread extends Thread {
           public void run(){
                //getInbox() runs an http client
               listOfMessages = function.getInbox(where);

           }
        }
    SendThread sendThread  = new SendThread();
    sendThread.start();

    inbox.setText(listOfMessages);

}

Like I said above, if I remove my thread code, then it works perfectly. Any ideas on what I’m doing wrong? This is my first time using threads, sorry for any rookie mistakes.

I don’t get any errors (at least I don’t see any) but, I don’t see the output that I get without the thread code inserted.

  • 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-02T16:03:40+00:00Added an answer on June 2, 2026 at 4:03 pm

    I agree with the others: 1) you didn’t allow time for the thread to finish, and 2) you must modify the UI on the main thread. I figured you might want to see a concrete solution, so here you go:

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.chat_box);// sd
    
        final Functions function = new Functions();
        final SharedPreferences prefs = PreferenceManager
                .getDefaultSharedPreferences(getBaseContext());
        where = prefs.getString("chat", "null");
        new AsyncTask<String, Void, String>() {
            @Override
            protected String doInBackground(String... args) {
                return function.getInbox(args[0]);
            }
    
            @Override
            protected void onPostExecute(String result) {
                TextView inbox = (TextView) findViewById(R.id.inbox);
                inbox.setText(result);
            }
    
        }.execute(where);
    }
    

    The use of AsyncTask makes this super simple: it’s the Android-recommended way to do simple tasks off the main thread. When execute() is called, a new thread is created and it calls doInBackground(). Then the result is returned on the main thread to the onPostExecute method. So you don’t have to deal with runOnUiThread or anything else.

    One thing to be aware of in this case: in the case of something like an orientation change, your Activity will be destroyed and recreated, and so the call getInbox() will be called again. This may or may not be a problem, depending on how long the method actually takes. If it’s unacceptable, you need something like a static AsyncTask, but then you run into the problem of attaching back to the new Activity. I’m just mentioning that, not because you have to handle it right now, but just so you’re aware.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
I am trying to loop through a bunch of documents I have to put
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) 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.