You can use Android-Async-http libraray https://github.com/loopj/android-async-http
See his doc for usage
http://loopj.com/android-async-http/
Gradle
repositories {
mavenCentral()
}
dependencies { compile 'com.loopj.android:android-async-http:1.4.9'
}
Uploading
Uploading Files with RequestParams
The RequestParams class additionally supports multipart file uploads as follows:
Add an InputStream to the RequestParams to upload:
InputStream myInputStream = blah;
RequestParams params = new RequestParams();
params.put("secret_passwords", myInputStream, "passwords.txt");
Add a File object to the RequestParams to upload:
File myFile = new File("/path/to/file.png");
RequestParams params = new RequestParams();
try {
params.put("profile_picture", myFile);
} catch(FileNotFoundException e) {}
Add a byte array to the RequestParams to upload:
byte[] myByteArray = blah;
RequestParams params = new RequestParams();
params.put("soundtrack", new ByteArrayInputStream(myByteArray), "she-wolf.mp3");
And then
AsyncHttpClient client = new AsyncHttpClient();
client.post("https://myendpoint.com", params, responseHandler);
Downloading
Downloading Binary Data with FileAsyncHttpResponseHandler
The FileAsyncHttpResponseHandler class can be used to fetch binary data such as images and other files. For example:
AsyncHttpClient client = new AsyncHttpClient();
client.get("https://example.com/file.png", new FileAsyncHttpResponseHandler(/* Context */ this) {
@Override
public void onSuccess(int statusCode, Header[] headers, File response) {
// Do something with the file `response`
}
});