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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:22:38+00:00 2026-05-24T09:22:38+00:00

Possible Duplicate: Download a file with Android, and showing the progress in a ProgressDialog

  • 0

Possible Duplicate:
Download a file with Android, and showing the progress in a ProgressDialog

I want to load info from a web server into my app. Currently I’m doing it in the main thread, which I’ve read is very bad practice (if the request takes longer than 5 secs, the app crashes).

So instead I’d like to learn how to move this operation to a background thread. Does this involve a service somehow?

Here is a sample of the code where I make server requests:

        // send data
        URL url = new URL("http://www.myscript.php");
        URLConnection conn = url.openConnection(); 
        conn.setDoOutput(true); 
        OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
        wr.write(data);
        wr.flush();

        // Get the response
        BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        StringBuilder sb = new StringBuilder(); 
        String line;
        while ((line = rd.readLine()) != null) {
            sb.append(line + "\n"); 
        }

        wr.close();
        rd.close();

        String result = sb.toString(); 

        Intent i = new Intent(searchEventActivity.this, searchResultsActivity.class);
            i.putExtra("result", result);
        startActivity(i);

So I’m waiting to build up a JSON string response, then I’m passing that string to a new activity. This is a timely operation, and instead of hanging the UI, I’d like to show the user a nice “Progress” bar of some sort (even one of those circles with the spinning lights is good) while this URL business is happening in a background thread.

Thanks for any help or links to tutorials.

  • 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-05-24T09:22:39+00:00Added an answer on May 24, 2026 at 9:22 am

    The basic idea of the process is to create a Thread to handle the web request, then use Handlers and Runnables to manage the UI interaction.

    The way that I manage this in my apps is to use a custom class that includes all of the intelligence and business rules to manage my communications. It also includes variables in the constructor to allow the UI thread to be called.

    Here’s an example:

    public class ThreadedRequest
    {
        private String url;
        private Handler mHandler;
        private Runnable pRunnable;
        private String data;
        private int stausCode;
    
        public ThreadedRequest(String newUrl, String newData)
        {
            url = newUrl;
            data = newData;
            mHandler = new Handler();
        }
    
        public void start(Runnable newRun)
        {
            pRunnable = newRun;
            processRequest.start();
        }
    
        private Thread processRequest = new Thread()
        {
            public void run()
            {
                //Do you request here...
                if (pRunnable == null || mHandler == null) return;
                mHandler.post(pRunnable);
            }
        }
    }
    

    This would be called from your UI thread as follows:

    final ThreadedRequest tReq = new ThreadedRequest(url, maybeData);
    //This method would start the animation/notification that a request is happening
    StartLoading();
    tReq.start(new Runnable() 
        {
            public void run() 
            {
               //This would stop whatever the other method started and let the user know
               StopLoading();
            }
        });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Save Youtube video to iPhone in the app I want to download
Possible Duplicate: programatically get the file size from a remote file using delphi, before
Possible Duplicate: How to Download A file stored in SQL DB in Binary Format
Possible Duplicate: Best way to download a file in PHP i have been asked
Possible Duplicate: Force Download an Image Using Javascript Basically I want to have the
Possible Duplicate: How can I download HTML source in C# I want the source
Possible Duplicate: How do you send email from a Java app using Gmail? How
Possible Duplicate: Singleton: How should it be used Following on from Ewan Makepeace 's
Possible Duplicate: Using ZXing to create an android barcode scanning app I know it
Possible Duplicate: No generated R.java file in my project I am trying to run

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.