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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:47:14+00:00 2026-05-24T17:47:14+00:00

I figured this must be a common question, but I surprisingly couldn’t find an

  • 0

I figured this must be a common question, but I surprisingly couldn’t find an answer, so maybe my entire structure is terribad…

I have an activity which downloads values/states of a game from a web service via AsyncTask. These values are used to update a custom view.

Once the view is created, various events from the view launch an AsyncTask to download other information.

This is functional, but the problem is I now have half a dozen AsyncTask classes in the activity with almost identical code. The only difference is the type of object that is returned (which is based on the json from the web service) and the method that is called from onPostExecute().

How can I use just two AsyncTask (one for post and one for get) without knowing what type of json object will be returned by the web service?

In a similar vein, how can I determine the type of object returned by the web service? The web service, if there is a problem, will return a json string that correlates to an ErrorMessage object rather than (for example) a GameData object.

Should I be using switch and instanceof in onPostExecute() somehow? Callbacks maybe?

  • 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-24T17:47:14+00:00Added an answer on May 24, 2026 at 5:47 pm

    You can use an abstract base class, which your related classes extends.

    Sample code:

    public abstract class IBaseObject {
        protected String error;
    
        public IBaseObject(String param) {
            error = param;
        }
    
        public abstract String getError();
    }
    
    public class ObjectOne extends IBaseObject {        
        private String objectParam;        
    
        public ObjectOne(String error, String objectSpecificParam) {
            super(error);
            objectParam = objectSpecificParam;
        }
    
        @Override
        public String getError() {
            return error;
        }
    }
    

    and for example, use it like this:

    private class GetTask extends AsyncTask<String, Void, IBaseObject> {
        protected IBaseObject doInBackground(String... url) {
            // Get your data.
            // Construct your corresponding object given by specific
            // parameters from your JSON response.
            if (a_parameter_match) {
                return new ObjectOne(some_json_params...);
            } else {
                return new ObjectTwo(some_json_params...);
            }        
        }    
    
        protected void onPostExecute(IBaseObject object) {
            object.getError(); // Or whatever you need here.
        }
    } 
    

    This is just from the top of my head. I couldn’t relate to your specific problem, although the ideas here should be enough to get you started on your new structure.

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

Sidebar

Related Questions

Thought this would be a good SO question since couldn't find anything on Google
I want to do this, but haven't figured it quite out yet... $(document).ready(function() {
Ok sorry this might seem like a dumb question but I cannot figure this
This must be a common problem because I see it in many published papers
Forgive me if this is in the wrong place, but I figured a few
Ok, I've run across my first StackOverflowError since joining this site, I figured this
Before I do this I figured I would ask if it was the best
The people on this website seem to know everything so I figured I would
I'm not sure if this even exists or not, so I figured I would
I've seen a lot of different takes on this subject so I figured if

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.