AsyncHttpClient client = new AsyncHttpClient(); client.get("https://www.google.com", new AsyncHttpResponseHandler() { public void onStart() {} public void onSuccess(int statusCode, Header[] headers, byte[] response) {} public void onFailure(int statusCode, Header[] headers, byte[] errorResponse, Throwable e) {} public void onRetry(int retryNo) {} });
public class TwitterRestClient { private static final String BASE_URL = "https://api.twitter.com/1/"; 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; } }
//输入流上传 InputStream myInputStream = blah; RequestParams params = new RequestParams(); params.put("secret_passwords", myInputStream, "passwords.txt"); //本件上传。可以多个文件 File myFile = new File("/path/to/file.png"); RequestParams params = new RequestParams(); params.put("profile_picture", myFile); //二进制字节流上传 byte[] myByteArray = blah; RequestParams params = new RequestParams(); params.put("soundtrack", new ByteArrayInputStream(myByteArray), "she-wolf.mp3");
AsyncHttpClient client = new AsyncHttpClient(); client.get(new File("存储的位置"), new FileAsyncHttpResponseHandler(/* Context */ this) { @Override public void onSuccess(int statusCode, Header[] headers, File response) { // Do something with the file `response` } });