Skip to main content
deleted 78 characters in body; edited tags
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

I have a helper class that does HttpHTTP operations on Android. Every method uses a client field. I think it's a good a idea to make this client a singleton since every method in the class uses it.

The problem is that I don't know 100% what's the best way to implement this singleton since it's only a field in my helper class.

    public class HttpHelper {
        private static OkHttpClient client;
    
        private HttpHelper() { }
    
        private static OkHttpClient getClient() {
            if(client == null) {
                client = new OkHttpClient();
                client.setConnectTimeout(TIMEOUT, TimeUnit.SECONDS);
            }
           return client;
        }
    
        // method that uses client:
        public static String getHTML(String cookie, String URL) throws IOException {
    
            Request request = new Request.Builder().url(URL).header("Cookie", cookie).build();
            Response response = getClient().newCall(request).execute(); // using client
            if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
    
            return readStream(response.body().byteStream());
        }
    }

I am using methods like getHTMLgetHTML() (yes, the class has more) all over my project.

  1. Is it OK that I use alota lot of static methods like getHTMLgetHTML()?

  2. Should I be implementing the HttpHelperHttpHelper class as singleton and use non static methods like HttpHelper.getInstance().getHTML(); HttpHelper.getInstance().getHTML() (where the client is created in the getInstancegetInstance() method of the HttpHelperHttpHelper?

I have a helper class that does Http operations on Android. Every method uses a client field. I think it's a good a idea to make this client a singleton since every method in the class uses it.

The problem is that I don't know 100% what's the best way to implement this singleton since it's only a field in my helper class.

    public class HttpHelper {
        private static OkHttpClient client;
    
        private HttpHelper() { }
    
        private static OkHttpClient getClient() {
            if(client == null) {
                client = new OkHttpClient();
                client.setConnectTimeout(TIMEOUT, TimeUnit.SECONDS);
            }
           return client;
        }
    
        // method that uses client:
        public static String getHTML(String cookie, String URL) throws IOException {
    
            Request request = new Request.Builder().url(URL).header("Cookie", cookie).build();
            Response response = getClient().newCall(request).execute(); // using client
            if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
    
            return readStream(response.body().byteStream());
        }
    }

I am using methods like getHTML (yes, the class has more) all over my project.

  1. Is it OK that I use alot of static methods like getHTML?

  2. Should I be implementing the HttpHelper class as singleton and use non static methods like HttpHelper.getInstance().getHTML(); (where the client is created in the getInstance method of the HttpHelper?

I have a helper class that does HTTP operations on Android. Every method uses a client field. I think it's a good idea to make this client a singleton since every method in the class uses it.

The problem is that I don't know 100% what's the best way to implement this singleton since it's only a field in my helper class.

public class HttpHelper {
    private static OkHttpClient client;

    private HttpHelper() { }

    private static OkHttpClient getClient() {
        if(client == null) {
            client = new OkHttpClient();
            client.setConnectTimeout(TIMEOUT, TimeUnit.SECONDS);
        }
       return client;
    }

    // method that uses client:
    public static String getHTML(String cookie, String URL) throws IOException {

        Request request = new Request.Builder().url(URL).header("Cookie", cookie).build();
        Response response = getClient().newCall(request).execute(); // using client
        if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);

        return readStream(response.body().byteStream());
    }
}

I am using methods like getHTML() (yes, the class has more) all over my project.

  1. Is it OK that I use a lot of static methods like getHTML()?

  2. Should I be implementing the HttpHelper class as singleton and use non static methods like HttpHelper.getInstance().getHTML() (where the client is created in the getInstance() method of the HttpHelper?

edited body
Source Link
Vogel612
  • 25.5k
  • 7
  • 59
  • 141

I have a helper class that does Http operations on Android. Every method uses a client field. I think it's a good a idea to make this client a singleton since every method in the class uses it.

The problem is that I don't know 100% what's the best way to implement this singleton since it's only a field in my helper class.

    public class HttpHelper {
        private static OkHttpClient client;
    
        private HttpHelper() { }
    
        private static OkHttpClient getClient() {
            if(client == null) {
                client = new OkHttpClient();
                client.setConnectTimeout(TIMEOUT, TimeUnit.SECONDS);
            }
           return client;
        }
    
        // method that uses client:
        public static String getHTML(String cookie, String URL) throws IOException {
    
            Request request = new Request.Builder().url(URL).header("Cookie", cookie).build();
            Response response = getClient().newCall(request).execute(); // using client
            if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
    
            return readStream(response.body().byteStream());
        }
    }

I am using methods like getHTML (yes, the class has more) all over my project.

  1. Is it OK that I use alot of static methods like getHTML?

  2. Should I be implementing the HttpHelper class as singleton and use non static methods like HttpHelper.getInstance().getHTML(); (where the client is created in the getInstance method of the HttpHelper?

I have a helper class that does Http operations on Android. Every method uses a client field. I think it's a good a idea to make this client a singleton since every method in the class uses it.

The problem is that I don't know 100% what's the best way to implement this singleton since it's only a field in my helper class.

    public class HttpHelper {
        private static OkHttpClient client;
    
        private HttpHelper() { }
    
        private static OkHttpClient getClient() {
            if(client == null) {
                client = new OkHttpClient();
                client.setConnectTimeout(TIMEOUT, TimeUnit.SECONDS);
            }
           return client;
        }
    
        // method that uses client:
        public static String getHTML(String cookie, String URL) throws IOException {
    
            Request request = new Request.Builder().url(URL).header("Cookie", cookie).build();
            Response response = getClient().newCall(request).execute(); // using client
            if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
    
            return readStream(response.body().byteStream());
        }
    }

I am using methods like getHTML (yes, the class has more) all over my project.

  1. Is it OK that I use alot of static methods like getHTML?

  2. Should I be implementing the HttpHelper class as singleton and use non static methods like HttpHelper.getInstance().getHTML(); (where the client is created in the getInstance method of the HttpHelper?

I have a helper class that does Http operations on Android. Every method uses a client field. I think it's a good a idea to make this client a singleton since every method in the class uses it.

The problem is that I don't know 100% what's the best way to implement this singleton since it's only a field in my helper class.

    public class HttpHelper {
        private static OkHttpClient client;
    
        private HttpHelper() { }
    
        private static OkHttpClient getClient() {
            if(client == null) {
                client = new OkHttpClient();
                client.setConnectTimeout(TIMEOUT, TimeUnit.SECONDS);
            }
           return client;
        }
    
        // method that uses client:
        public static String getHTML(String cookie, String URL) throws IOException {
    
            Request request = new Request.Builder().url(URL).header("Cookie", cookie).build();
            Response response = getClient().newCall(request).execute(); // using client
            if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
    
            return readStream(response.body().byteStream());
        }
    }

I am using methods like getHTML (yes, the class has more) all over my project.

  1. Is it OK that I use alot of static methods like getHTML?

  2. Should I be implementing the HttpHelper class as singleton and use non static methods like HttpHelper.getInstance().getHTML(); (where the client is created in the getInstance method of the HttpHelper?

added 16 characters in body
Source Link
VM4
  • 131
  • 1
  • 5

I have a helper class that does Http operations on Android. Every method uses a client field. I think it's a good a idea to make this client a singleton since every method in the class uses it.

The problem is that I don't know 100% what's the best way to implement this singleton since it's only a field in my helper class.

    public class HttpHelper {
        private static OkHttpClient client;
    
        private HttpHelper() { }
    
        private static OkHttpClient getClient() {
            if(client == null) {
                client = new OkHttpClient();
                client.setConnectTimeout(TIMEOUT, TimeUnit.SECONDS);
            }
           return client;
        }
    
        // method that uses client:
        public static String getHTML(String cookie, String URL) throws IOException {
    
            Request request = new Request.Builder().url(URL).header("Cookie", cookie).build();
            Response response = getClient().newCall(request).execute(); // using client
            if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
    
            return readStream(response.body().byteStream());
        }
    }

I am using methods like getHTML (yes, the class has more) all over my project.

  1. Is it OK that I use alot of static methods like getHTML?

  2. Should I be implementing the HttpHelper class as singleton and use non static methods like HttpHelper.getInstance().getHTML(); (where the client is created in the getInstance method of the HttpHelper?

I have a helper class that does Http operations on Android. Every method uses a client field. I think it's a good a idea to make this client a singleton since every method in the class uses it.

The problem is that I don't know 100% what's the best way to implement this singleton since it's only a field in my helper class.

    public class HttpHelper {
        private static OkHttpClient client;
    
        private HttpHelper() { }
    
        private static OkHttpClient getClient() {
            if(client == null) {
                client = new OkHttpClient();
                client.setConnectTimeout(TIMEOUT, TimeUnit.SECONDS);
            }
           return client;
        }
    
        // method that uses client:
        public static String getHTML(String cookie, String URL) throws IOException {
    
            Request request = new Request.Builder().url(URL).header("Cookie", cookie).build();
            Response response = getClient().newCall(request).execute();
            if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
    
            return readStream(response.body().byteStream());
        }
    }

I am using methods like getHTML (yes, the class has more) all over my project.

  1. Is it OK that I use alot of static methods like getHTML?

  2. Should I be implementing the HttpHelper class as singleton and use non static methods like HttpHelper.getInstance().getHTML(); (where the client is created in the getInstance method of the HttpHelper?

I have a helper class that does Http operations on Android. Every method uses a client field. I think it's a good a idea to make this client a singleton since every method in the class uses it.

The problem is that I don't know 100% what's the best way to implement this singleton since it's only a field in my helper class.

    public class HttpHelper {
        private static OkHttpClient client;
    
        private HttpHelper() { }
    
        private static OkHttpClient getClient() {
            if(client == null) {
                client = new OkHttpClient();
                client.setConnectTimeout(TIMEOUT, TimeUnit.SECONDS);
            }
           return client;
        }
    
        // method that uses client:
        public static String getHTML(String cookie, String URL) throws IOException {
    
            Request request = new Request.Builder().url(URL).header("Cookie", cookie).build();
            Response response = getClient().newCall(request).execute(); // using client
            if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
    
            return readStream(response.body().byteStream());
        }
    }

I am using methods like getHTML (yes, the class has more) all over my project.

  1. Is it OK that I use alot of static methods like getHTML?

  2. Should I be implementing the HttpHelper class as singleton and use non static methods like HttpHelper.getInstance().getHTML(); (where the client is created in the getInstance method of the HttpHelper?

Source Link
VM4
  • 131
  • 1
  • 5
Loading