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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:07:57+00:00 2026-05-13T10:07:57+00:00

I am creating an android application which has to execute web requests in the

  • 0

I am creating an android application which has to execute web requests in the background and then handle the received data and modify the user interface according to the server response.

The goal of posting requests and handling data in the background is to avoid the freezing of user interface. Currently however I notice that the user interface is freezing so I am not sure the logic is working as it is supposed to.

Here is the part of code which is supposed to post requests and handle responses in its own thread and then pass the data to GUI:

public class ServerConnection {

Queue<String> requests;

...

DefaultHttpClient httpClient;
HttpHost targetHost;

Handler handler;

ServerResponseHandler responseHandler;
Activity activity;

public ServerConnection(Activity activity){
    this.activity = activity;
    this.responseHandler = (ServerResponseHandler) activity;
    httpClient = new DefaultHttpClient();
    targetHost = new HttpHost(TARGET_DOMAIN, 80, "http");
    requests = new LinkedList<String>();
}



private Runnable requestSender = new Runnable(){

    @Override
    public void run() {
        if(!requests.isEmpty()){
            String requestString = requests.remove();
            HttpGet httpGet = new HttpGet(requestString);
            httpGet.addHeader("Accept", "text/xml");
            String encodingString = "testuser:testpass";
            String sEncodedString = Base64Coder.encodeString(encodingString);

            try{

                String sContent = fetchURL(requestString, sEncodedString);

                XMLParser xmlParser = new XMLParser();

                List <Product> products = xmlParser.getProducts(sContent);

                responseHandler.onProductsResponse(products);
            }
            catch(Exception ex){
                Log.e(TAG, ex.getMessage());
            }
        }
    }
};

public void sendRequest(String requestString){
    requests.add(requestString);
    handler = new Handler();
    handler.post(requestSender);
}

The method sendRequest() is called from the main activity which implements ServerResponseHandler. I guess the request is executed in its own thread and by calling

responseHandler.onProductsResponse(products);

the list of products (data from the web) is passed to main activity. Anyway due to poor performance I would appreciate if anyone could correct any possible issue in the logic above or suggest any other (better) option.

  • 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-13T10:07:58+00:00Added an answer on May 13, 2026 at 10:07 am

    I’d suggest you take a look at ASyncTask class (available since Android 1.5).

    It simplifies the process of creating a background Thread that synchronizes with the GUI thread once it’s complete.

    You should be able to achieve what you’re trying using code something list this

    private class DownloadFilesTask extends AsyncTask<String, List<Product>, Integer> {
         protected List<Products> doInBackground(String... requestStrings) {
            int count = requestStrings.length;
            int results = 0;
            for (int i = 0; i < count; i++) {
              String requestString = requestStrings[i];
              HttpGet httpGet = new HttpGet(requestString);
              httpGet.addHeader("Accept", "text/xml");
              String encodingString = "testuser:testpass";
              String sEncodedString = Base64Coder.encodeString(encodingString);
              try{
                String sContent = fetchURL(requestString, sEncodedString);
                XMLParser xmlParser = new XMLParser();
                List <Product> products = xmlParser.getProducts(sContent);
                results++;
                publishProgress(products);
              }
              catch(Exception ex){
                Log.e(TAG, ex.getMessage());
              }
            }
            return results;
         }
    
         protected void onProgressUpdate(Integer... progress) {
             // TODO You are on the GUI thread, and the first element in 
             // the progress parameter contains the last progress
             // published from doInBackground, so update your GUI
         }
    
         protected void onPostExecute(int result) {
           // Processing is complete, result contains the number of 
           // results you processed
         }
     }
    

    And execute by calling

    new DownloadFilesTask().execute(url1, url2, url3);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 419k
  • Answers 419k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The issue is in your GROUP BY clause. If you… May 15, 2026 at 10:12 am
  • Editorial Team
    Editorial Team added an answer You can execute them, sequentially, like this: (nohup S1 &&… May 15, 2026 at 10:12 am
  • Editorial Team
    Editorial Team added an answer Don't know if this will help, but using 1 px… May 15, 2026 at 10:12 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.