package org.smartpaws.net; import com.loopj.android.http.AsyncHttpClient; import com.loopj.android.http.AsyncHttpResponseHandler; import com.loopj.android.http.RequestParams; import org.apache.http.Header; import org.apache.http.HttpEntity; import org.apache.http.entity.BasicHttpEntity; import org.apache.http.entity.StringEntity; import org.apache.http.message.BasicHeader; import org.smartpaws.MainActivity; import java.io.UnsupportedEncodingException; public class HttpClient { private static final int SOCKET_TIMEOUT = 30 * 1000; // 30 seconds public static final String BASE_URL = "http://smartpaws.org/app/"; private static AsyncHttpClient CLIENT = new AsyncHttpClient(); static { CLIENT.setTimeout(SOCKET_TIMEOUT); } public static void get(String url, RequestParams params, AsyncHttpResponseHandler handler) { CLIENT.get(getAbsoluteUrl(url), params, handler); } public static void getExternalUrl(String url, RequestParams params, AsyncHttpResponseHandler handler) { CLIENT.get(url, params, handler); } public static void postOAuthUrl(String url, String base64, RequestParams params, AsyncHttpResponseHandler handler) { try { CLIENT.post(MainActivity.INSTANCE, url, new Header[] { new BasicHeader("Authorization", "Basic " + base64) }, new StringEntity("grant_type=client_credentials"), "application/x-www-form-urlencoded;charset=UTF-8", handler); } catch (UnsupportedEncodingException ex) { ex.printStackTrace(); } } public static void getOAuthUrl(String url, String bearer, RequestParams params, AsyncHttpResponseHandler handler) { CLIENT.get(MainActivity.INSTANCE, url, new Header[] { new BasicHeader("Authorization", "Bearer " + bearer), new BasicHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8") }, params, handler); } private static String getAbsoluteUrl(String url) { return BASE_URL + url; } }