You can use Android Async HTTP Client
http://loopj.com/android-async-http/
Gradle
compile 'com.loopj.android:android-async-http:1.4.9'
static HTTP client
public class RestClient {
public static final String TAG = RestClient.class.getSimpleName();
private static final String BASE_URL = "http://your-site.com/api/v1/whatever";
private static AsyncHttpClient client = new AsyncHttpClient();
public static void get(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
client.get(getAbsoluteUrl(url), params, responseHandler);
}
public static void post(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
client.post(getAbsoluteUrl(url), params, responseHandler);
}
private static String getAbsoluteUrl(String relativeUrl) {
return BASE_URL + relativeUrl;
}
}
Client code
...
RequestParams params = new RequestParams();
params.put("THE_KEY", "THE_VALUE");
...
AsyncHttpClient client = new AsyncHttpClient();
client.post("YOUR_URL", new AsyncHttpResponseHandler() {
@Override
public void onStart() {
// called before request is started
}
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] response) {
// called when response HTTP status is "200 OK"
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] errorResponse, Throwable e) {
// called when response HTTP status is "4XX" (eg. 401, 403, 404)
}
@Override
public void onRetry(int retryNo) {
// called when request is retried
}
...