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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:09:56+00:00 2026-05-27T06:09:56+00:00

First off – I’m rather novice at Java so if the question makes no

  • 0

First off – I’m rather novice at Java so if the question makes no sense do let me know.

Basically I’m making an Android app which communicates with my web service and so I’ve made a separate class to deal with the communication, which also includes the AsyncTask (I’ve removed a lot from the code here just for preview):

public class api {

private String caller = null;
Context that = null;

api(Context that) {

    this.that = that;
    this.caller = that.getClass().getSimpleName();

}

void call(String action) {

    /* .... */

    }

    new back().execute(param1, param2);

}

void callback(String action, String result){

    that.callback(action, result);

}



public class back extends AsyncTask<String, Void, String> {

    public String response = null;

    protected String doInBackground(String... params) {

        response = connection.executeRequest(params[1]);
        return response;

    }

    protected void onPostExecute(String result) {

        callback("a", "b");                 

    }

}    


}

And when I use the class from some part of the app (let’s say SomeClass.class), I do:

api WS = new api(this);
WS.call("....");

And it’s supposed to execute the function ‘callback’ which is in SomeClass.
But the key problem here is this line:

that.callback(action, result);

Eclipse makes me add the name of the “caller” class in the cast:

(SomeClass) that.callback(action, result);

But that doesn’t work for me, because I use the ‘api’ class from many different classes, so ideally I need to put a variable in the cast. I do get the name of the “caller” class here:

this.caller = that.getClass().getSimpleName();
//obviously this won't work:
(this.caller) that.callback(action, result);

Is there anyway to do that, or am I doing something fundamentally wrong?

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-05-27T06:09:56+00:00Added an answer on May 27, 2026 at 6:09 am

    Currently your api class accepts a Context object in its default constructor. It would make more sense to extend Context with a new class which contains a callback method which you can then override in subclasses such as SomeClass, that would negate the need for casting in your api class. e.g:

    public class APIContext extends Context
    {
        public void callback( String action, String result )
        {
            /* ... */
        }
    }
    
    public class SomeClass extends APIContext
    {
        @Override
        public void callback( String action, String result )
        {
            /* ... */
        }
    }
    
    public class api
    {
        private APIContext callerContext = null;
    
        public api( APIContext context )
        {
            this.callerContext = context;
        }
    
        public void callback( String action, String result )
        {
            callerContext.callback( action, result );
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First off I'm new to Android and Java.. I have been trying to add
First off, I know this may be a very stupid question, so don't shoot
First off: I'm using a rather obscure implementation of javascript embedded as a scripting
First off, let me start off that I am not a .net developer. The
First off, this question is ripped out from this question. I did it because
First off, I know next to nothing about language theory, and I barely know
First off, I apologize if this doesn't make sense. I'm new to XHTML, CSS
First off, I'm not terribly experienced in XML. I know the very basics of
First off, I want to apologize beforehand, as I know there is a lot
First off, please accept my apologies if this question is basic, I mainly have

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.