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 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

I am sure that this must be a pretty common question but after scouring
I just figured this out but instead of splitting my new question (why?) into
Update II I Figured this out. When using google maps api, the model must
This must be quite simple but I can't seem to figure this out. I've
This must be simple, but I can't seem to figure it out. I am
I figured this would be simple, but being new to SSRS I'm not sure.
I thought I had this figured out but it turns out I'm just deleting
I saw this question already, but it doesn't explain what I am wondering about.
I apologize if this is a super noob question, but I can't seem to
This must be a common problem because I see it in many published papers

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.