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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:50:53+00:00 2026-05-16T06:50:53+00:00

I have a class that extends Application which has a lot of methods like:

  • 0

I have a class that extends Application which has a lot of methods like:

public User getUser(String name);
public List<User> getFriends(User user);
public List<Game> getGames(User user);

which wraps a service class.

The catch here is that no method will work if I have no internet on the device.
So for instance I am doing:

public User getUser(String name) {
        User ret = null;
        try {
            return myService.getUser(name);
        } catch (NoInternetException e) {
            NoInternetToast.show(this);
        }

    return ret;
}

Is there a way to wrap every call so I don’t have to add the try catch on every method of my Application?

  • 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-16T06:50:54+00:00Added an answer on May 16, 2026 at 6:50 am

    Without using any third party libraries that might be available on Android, there is no simple way to wrap methods of a class. If you can extract your application functionality into an interface, you can use java.lang.reflect.Proxy to implement your interface – the proxy implementation is a single method that calls your real implementation method, and caches and handles the exception.

    I can provide more details if factoring out the code into a separate class and interface is a workable approach for you.

    EDIT: Here’s the details:

    You currently are using myService, which implements the methods. If you don’t have one already, create an interface UserService that declares the service methods:

    public interface UserService {
      User getUser(String name);
      List<User> getFriends(User user);
      List<Game> getGames(User user);
    }
    

    And declare this interface on your existing MyService class,

    class MyService implements UserService {
         // .. existing methods unchanged 
         // interface implemented since methods were already present
    }
    

    To then avoid repetition, the exception handling is implemented as an InvocationHandler

    class HandleNoInternet implements InvocationHandler {
       private final Object delegate;   // set fields from constructor args
       private final Application app;
    
       public HandleNoInternet(Application app, Object delegate) {
          this.app = app; 
          this.delegate = delegate;
       }
       public Object invoke(Object proxy, Method method, Object[] args) {
           try {
               // invoke the method on the delegate and handle the exception
               method.invoke(delegate, args);
           } catch (Exception ex) {
               if ( ex.getCause() instanceof NoInternetException ) {
                 NoInternetToast.show(app);
               } else {
                 throw new RuntimeException(ex);
               }
           }
       }
    }
    

    This is then used as a proxy, in your Application class:

    InvocationHandler handler = new HandleNoInternet(this, myService);
    UserService appUserService = (UserService)Proxy.newProxyInstance(
       getClass().getClassLoader(), new Class[] { UserService.class }, handler);
    

    You then use the appUserService without needing to worry about catching the NoInternetException.

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

Sidebar

Related Questions

If I have a base class such that public abstract class XMLSubscription <T extends
I have a few classes such that: public class XMLStatusMessage extends XMLMessage {} public
I have a class that looks like this public class SomeClass { public SomeChildClass[]
Write a class named RaceHorse that extends Thread. Each RaceHorse has a name and
We have an application with a plugin which contains a service: public class TaskService
I have a CodeIgniter MVC application. I have a model called city that has
My current application has one Activity, the main one which extends ListActivity (listview of
I have a class that needs to obtain a reference to it's application's AssetManager
I have class method that returns a list of employees that I can iterate
I have a class that I want to use to store properties for another

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.