Everytime I make a http request I must make sure there is internet connection.

public class InternetCheck {
    public static boolean isOnline() {


class Httphandler {

    public static String login(String uname,String pass){
        if(InternetCkeck.isOnline)//make the request


What if I add logout,delete,uploadFile?I have to copy the if line again and so on for every http I make.

I was looking at the proxy pattern but I don't thing is the right choice.Using the proxy pattern every time I add a new request I also have to add to the proxy.(Maybe this is acceptable?)

(Maybe I get proxy pattern wrong that's why i'm gone explain how I undertand it: Make an interface with some methods: for eg login.Then create two classes one proxy one and the real thing. Then in the proxy I make the method login where I do put the if and if is true I call the method from the real thing. But again everytime I do add a new http call I have now to add it in two classes.How is this better than simply copy pasting the if statement alone?)

How to do this in a nice way without the copy paste thing?(I'm thinking about dry)

2 Answers 2


Create a private method that just makes a request, and check the connection once in there. All other methods call this with parameters defining which kind of request to make.

  • I never tought about this.It makes a lot of sence.Thanks.
    – Fox Alex
    Dec 22, 2016 at 21:33
  • I was about to suggest to take a look to AOP, but this solution is much simpler :-)
    – Laiv
    Dec 23, 2016 at 0:22

Just a note: It is very, very, very difficult to determine precisely whether you are online or not. The only method that really works is to send a request, and to interpret any success or error messages that you get. That's what I would do at the level where you want to perform a task that requires making a request.

On a higher level, in the user interface, you might tell the user if they have no internet connection (but make 100% sure that you will detect when connectivity comes back), and possibly what they could do to fix this.

  • That's almost what I do.I make a ping go google dns server and see if I get a response.
    – Fox Alex
    Dec 23, 2016 at 9:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.