{"id":7721,"date":"2015-10-19T00:57:41","date_gmt":"2015-10-19T00:57:41","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2015\/10\/19\/okhttp-response-callbacks-on-the-main-thread-open-source-projects-square-okhttp\/"},"modified":"2015-10-19T00:57:41","modified_gmt":"2015-10-19T00:57:41","slug":"okhttp-response-callbacks-on-the-main-thread-open-source-projects-square-okhttp","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2015\/10\/19\/okhttp-response-callbacks-on-the-main-thread-open-source-projects-square-okhttp\/","title":{"rendered":"Okhttp response callbacks on the main thread-open source projects square\/okhttp"},"content":{"rendered":"<p>I have created a helper class to handle all of my http calls in my app. It is a simple singleton wrapper for okhttp that looks like this (I have omitted some unimportant parts):<\/p>\n<pre><code>public class HttpUtil {\n\n    private OkHttpClient client;\n    private Request.Builder builder;\n\n    ...\n\n    public void get(String url, HttpCallback cb) {\n        call(\"GET\", url, cb);\n    }\n\n    public void post(String url, HttpCallback cb) {\n        call(\"POST\", url, cb);\n    }\n\n    private void call(String method, String url, final HttpCallback cb) {\n        Request request = builder.url(url).method(method, method.equals(\"GET\") ? null : new RequestBody() {\n            \/\/ don't care much about request body\n            @Override\n            public MediaType contentType() {\n                return null;\n            }\n\n            @Override\n            public void writeTo(BufferedSink sink) throws IOException {\n\n            }\n        }).build();\n\n        client.newCall(request).enqueue(new Callback() {\n            @Override\n            public void onFailure(Request request, Throwable throwable) {\n                cb.onFailure(null, throwable);\n            }\n\n            @Override\n            public void onResponse(Response response) throws IOException {\n                if (!response.isSuccessful()) {\n                    cb.onFailure(response, null);\n                    return;\n                }\n                cb.onSuccess(response);\n            }\n        });\n    }\n\n\n    public interface HttpCallback  {\n\n        \/**\n         * called when the server response was not 2xx or when an exception was thrown in the process\n         * @param response - in case of server error (4xx, 5xx) this contains the server response\n         *                 in case of IO exception this is null\n         * @param throwable - contains the exception. in case of server error (4xx, 5xx) this is null\n         *\/\n        public void onFailure(Response response, Throwable throwable);\n\n        \/**\n         * contains the server response\n         * @param response\n         *\/\n        public void onSuccess(Response response);\n    }\n\n}\n<\/code><\/pre>\n<p>Then, in my main activity, I use this helper class :<\/p>\n<pre><code>HttpUtil.get(url, new HttpUtil.HttpCallback() {\n            @Override\n            public void onFailure(Response response, Throwable throwable) {\n                \/\/ handle failure\n            }\n\n            @Override\n            public void onSuccess(Response response) {\n                \/\/<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I have created a helper class to handle all of my http calls in my app. It is a simple singleton wrapper for okhttp that looks like this (I have omitted some unimportant parts): public class HttpUtil { private OkHttpClient client; private Request.Builder builder; &#8230; public void get(String url, HttpCallback cb) { call(&#8220;GET&#8221;, url, cb); [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-7721","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7721","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=7721"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7721\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=7721"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=7721"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=7721"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}