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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:22:59+00:00 2026-06-13T13:22:59+00:00

I’m a long time (8 years) C# developer dabbling in a little android development.

  • 0

I’m a long time (8 years) C# developer dabbling in a little android development. This is the first time I’ve used Java and am having a little trouble shifting my mindset when it comes to inner classes.

I’m trying to write a class to wrap an RESTful API to execute various calls on the server from one typesafe place, e.g.

string jsonResult = APIWrapper.getItems("category 1");

And I want to use a AsyncTask to get stuff in the background.

So one way is this – have the AsyncTask in the APIWrapper :

class ActivityA extends Activity {

 myButton.setOnClickListener(new OnClickListener() {   
  public void onClick(View v) {

    //get stuff and use the onPostExecute inside the APIWrapper
    new APIWrapper().GetItems("Category A", MyGetItemsCallBack);

  }}); 

 function void MyGetItemsCallBack(String result)
 {
    //render the result in the activity context, in a listview or something
 }

}

I’m not sure the callback / delegate idea works in Java anyway!

The other way is to have the AsyncTask in the Activity and create the APIWrapper to just get the data like a worker / helper:

class ActivityA extends Activity {

 myButton.setOnClickListener(new OnClickListener() {   
  public void onClick(View v) {

    //get stuff and use the onProcessComplete inside the APIWrapper
    new GetItems("Category A");

  }}); 

 class GetItems(String theCategory) extends AsyncTask
 {
    doInBackground()
    {
      return new APIWrapper().GetItems(theCategory);
    }

    onPostExecute(String result)
    {
       //render the result in the activity context, in a listview or something
    }

 }


}

Can anyone help me make the right choice?

  • 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-13T13:22:59+00:00Added an answer on June 13, 2026 at 1:22 pm

    Java doesn’t have something like C# delegates, so your first proposal is not possible as-is.

    The standard solution is to declare an interface that can be passed as a callback:

    public interface MyCallback {
      void gotItems(String data);
    }
    

    Then you can do something like

    class ActivityA extends Activity {
      myButton.setOnClickListener(new OnClickListener() {   
        public void onClick(View v) {
          //get stuff and use the onPostExecute inside the APIWrapper
          new APIWrapper().getItems("Category A", new MyGetItemsCallBack());
      }}); 
    
      private class MyGetItemsCallBack implements MyCallback {
        public void gotItems(String result) {
          // bla bla bla
        }
      }
    }
    

    or you can use an anonymous class:

    class ActivityA extends Activity {
      myButton.setOnClickListener(new OnClickListener() {   
        public void onClick(View v) {
          //get stuff and use the onPostExecute inside the APIWrapper
          new APIWrapper().getItems("Category A", new MyCallback() {
            public void gotItems(String result) {
              // bla bla bla
            }
          });
      }}); 
    

    If you need many different kinds of callbacks you can use generics to avoid having do declare many small interfaces:

    interface Callback<T> {
      void run(T arg);
    }
    
    class ActivityA extends Activity {
      myButton.setOnClickListener(new OnClickListener() {   
        public void onClick(View v) {
          //get stuff and use the onPostExecute inside the APIWrapper
          new APIWrapper().getItems("Category A", new Callback<String>() {
            public void run(String result) {
              // bla bla bla
            }
          });
      }}); 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

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.