|
10 | 10 | import java.util.Hashtable; |
11 | 11 | import java.util.Random; |
12 | 12 |
|
| 13 | +class EmptyResponseHandler extends ResponseHandler { |
| 14 | + |
| 15 | + @Override |
| 16 | + public void handleResponse(HttpRequest hreq, String response) { |
| 17 | + |
| 18 | + } |
| 19 | + |
| 20 | + @Override |
| 21 | + public void handleError(HttpRequest hreq, PubnubError error) { |
| 22 | + |
| 23 | + } |
| 24 | + |
| 25 | +} |
| 26 | + |
| 27 | +class PublishResponseHandler extends ResponseHandler { |
| 28 | + Callback callback; |
| 29 | + String channel; |
| 30 | + public PublishResponseHandler(Callback callback, String channel) { |
| 31 | + this.callback = callback; |
| 32 | + this.channel = channel; |
| 33 | + } |
| 34 | + public void handleResponse(HttpRequest hreq, String response) { |
| 35 | + JSONArray jsarr; |
| 36 | + try { |
| 37 | + jsarr = new JSONArray(response); |
| 38 | + } catch (JSONException e) { |
| 39 | + handleError(hreq, |
| 40 | + PubnubError.getErrorObject(PubnubError.PNERROBJ_INVALID_JSON, 1, response)); |
| 41 | + return; |
| 42 | + } |
| 43 | + callback.successCallback(channel, jsarr); |
| 44 | + } |
| 45 | + |
| 46 | + public void handleError(HttpRequest hreq, PubnubError error) { |
| 47 | + callback.errorCallback(channel, error); |
| 48 | + return; |
| 49 | + } |
| 50 | +} |
13 | 51 |
|
14 | 52 | /** |
15 | 53 | * Pubnub object facilitates querying channels for messages and listening on |
@@ -876,25 +914,8 @@ protected void publish(Hashtable args) { |
876 | 914 | PubnubUtil.urlEncode(msgStr) |
877 | 915 | }; |
878 | 916 |
|
879 | | - class PublishResponseHandler extends ResponseHandler { |
880 | | - public void handleResponse(HttpRequest hreq, String response) { |
881 | | - JSONArray jsarr; |
882 | | - try { |
883 | | - jsarr = new JSONArray(response); |
884 | | - } catch (JSONException e) { |
885 | | - handleError(hreq, |
886 | | - PubnubError.getErrorObject(PubnubError.PNERROBJ_INVALID_JSON, 1, response)); |
887 | | - return; |
888 | | - } |
889 | | - callback.successCallback(channel, jsarr); |
890 | | - } |
891 | 917 |
|
892 | | - public void handleError(HttpRequest hreq, PubnubError error) { |
893 | | - callback.errorCallback(channel, error); |
894 | | - return; |
895 | | - } |
896 | | - } |
897 | | - HttpRequest hreq = new HttpRequest(urlComponents, parameters, new PublishResponseHandler()); |
| 918 | + HttpRequest hreq = new HttpRequest(urlComponents, parameters, new PublishResponseHandler(callback, channel)); |
898 | 919 |
|
899 | 920 | _request(hreq, nonSubscribeManager); |
900 | 921 | } |
@@ -1759,14 +1780,7 @@ private void _leave(String channel, Hashtable params) { |
1759 | 1780 |
|
1760 | 1781 | params.put("uuid", UUID); |
1761 | 1782 |
|
1762 | | - HttpRequest hreq = new HttpRequest(urlArgs, params, |
1763 | | - new ResponseHandler() { |
1764 | | - public void handleResponse(HttpRequest hreq, String response) { |
1765 | | - } |
1766 | | - |
1767 | | - public void handleError(HttpRequest hreq, PubnubError error) { |
1768 | | - } |
1769 | | - }); |
| 1783 | + HttpRequest hreq = new HttpRequest(urlArgs, params, new EmptyResponseHandler()); |
1770 | 1784 |
|
1771 | 1785 | _request(hreq, nonSubscribeManager); |
1772 | 1786 | } |
|
0 commit comments