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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T20:35:50+00:00 2026-06-03T20:35:50+00:00

I’m working on an Android project (API level 10) which needs to send and

  • 0

I’m working on an Android project (API level 10) which needs to send and receive http messages to/from a server.

I implemented a class named NetworkManager which provides different methods, one for each http request (e.g.: loginRequest(user pass), RegistrationRequest(user…..) ).

All these methods generates a JSON object that is passed to the method called sendMessage, which is the method that actually establish the connection, sends and receives the response (also a json object).

Of course network calls are time consuming, so i first decided to use an AsyncTask to display a progressDialog while the network operation is being performed.

The problem is that i need to get the response value retrived from the background thread before executing any other operation which involves the result itself done by the Main thread.
At the same time i would like to make a common and reusable implementation of the AsyncTask.

E.g.: I have a login activity which shows 2 EditText (username, password) and a button called Login. When I press the login button, a progressDialog must appear, and must be disposed once the doInBackground task is accomplished. Of course i could do this way:

onClick(View v) //called when the login button is pressed
{
    onPreExecute()
    {
        //Show the progress dialog
    }

    doInBackground()
    {
        //Retreive the login response (an integer containing a message code) using sendLoginRequest(username, password);
        //return the response
    }

    onPostExecute(int response)
    {
        //Dispose the progress dialog, then loginSucessfull ? start new activity : show error toast
    }
}

But, doing this way i should implement an async task for every request i need to send which is what i would like to avoid because if i have N requests i should create N classes that extend AsyncTask.

Thank you!

  • 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-03T20:35:52+00:00Added an answer on June 3, 2026 at 8:35 pm

    What i would suggest you is to use INTERFACES for handling response of http request.

    The background thread either it be a AysncTask or it be Thread needs to handle both

    • response

    • exception

    Think it like this way

    MainThread – Hey Background Thread do this operation and let me know when you are done.

    MainThread – Ok till Background Thread executes its operation let me show progress dialog.

    BackGroundThread – I am done with my work. hey MainThread here catch you response or exception

    MainThread – Let me stop showing progress bar.

    So we need to simulate this callback mechanism via code and also needs to take care that we implement a reusable architecture.

    Something like this

    Define a Interface

    public interface HttpRequestResponse {
        public void onSuccess(HttpResponse response);
        public void onException(Exception exception); 
    }     
    
    
    class HttpRequestResponseHandler {
    
       private ActionItem action;
       private HttpRequestResponse hrr;
       private Executor executor; 
    
       public enum ActionItem {
          LOGIN_REQUEST ,
          REGISTRATION_REQUEST            
       } 
    
       public HttpRequestResponseHandler(ActionItem action, HttpRequestResponse hrr) {
          this.action = action;
          this.hrr = hrr;       
       }
    
       public void execute(){
         executor = new Executor();
         executor.execute();       
       }
    
       private class Executor extends AsyncTask<Void,Void,Void> {
    
          @Override
          public Void doInBackground() {
             switch(action) {
    
                case LOGIN_REQUEST : doLogin();
                                     break;   
    
                case REGISTRATION_REQUEST : doRegistration();
                                            break; 
             }
          }
       }
    
       private void doLogin() {
    
          HttpResponse response = null;
          Exception exception = null;
          try {
             response = makeHttpRequestHere();
          } catch (Exception e) {
             exception = e;
          }
    
          if(exception != null) {
             hrr.onException(exception);
          } else {
             hrr.onSuccess(response);
          }
    
       }   
    
    }
    

    Now in somewhere in your activity code file do like this.

     HttpRequestResponse hrr = new HttpRequestResponse(){
    
        @Override
        public void onSuccess(HttpResponse response) {
           hideProgressDialog();
           handleResponse(response);
        }
    
        @Override
        public void onException(Exception exception) {
           hideProgressDialog();
           showErrorDialog(exception.getMessage());
        } 
     }
    
     HttpRequestResponseHandler hrrh = new HttpRequestResponseHandler(ActionItem.LOGIN_REQUEST,hrr);
     hrrh.execute();
     showProgressDialog();  
    

    Hope all this lead to what you want.
    Its been a long answer and took quite a effort of mine to figure. 🙂

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

Sidebar

Related Questions

I have a text area in my form which accepts all possible characters from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

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.