1

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

1

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.

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

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.

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.