Last active
October 5, 2016 12:39
-
-
Save hboregio/3163f5fa8bcf4cf30ac0 to your computer and use it in GitHub Desktop.
Api
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Api { | |
private static final String BASE_URL = "https://my.api.com/"; | |
private static AsyncHttpClient aSyncClient; | |
private static String USER_AGENT = "Our Custom User Agent"; | |
static { | |
// setup asynchronous client | |
aSyncClient = new AsyncHttpClient(); | |
aSyncClient.setUserAgent(USER_AGENT); | |
aSyncClient.getHttpClient().getParams().setParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, false); | |
aSyncClient.getHttpClient().getParams().setParameter(ClientPNames.HANDLE_REDIRECTS, false); | |
aSyncClient.getHttpClient().getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); | |
} | |
public static void get(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) { | |
aSyncClient.get(getAbsoluteUrl(url), params, responseHandler); | |
} | |
public static void post(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) { | |
aSyncClient.post(getAbsoluteUrl(url), params, responseHandler); | |
} | |
public static void put(String url, AsyncHttpResponseHandler responseHandler) { | |
aSyncClient.put(null, getAbsoluteUrl(url), null, "application/json", responseHandler); | |
} | |
public static void delete(String url, AsyncHttpResponseHandler responseHandler) { | |
aSyncClient.delete(null, getAbsoluteUrl(url), responseHandler); | |
} | |
private static String getAbsoluteUrl(String relativeUrl) { | |
return BASE_URL + relativeUrl; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment