In my last app I\'m going to use Koush Ion library. It\'s so handy but i have a problem with uploading file to my rest server. note: My server response to success upload Pro
Searched pretty long for this and noticed that the important thing was to include POST in .load()
Ion.with(getBaseContext()).load("POST",url).uploadProgressHandler(new ProgressCallback()
{
@Override
public void onProgress(long uploaded, long total)
{
System.out.println("uploaded " + (int)uploaded + " Total: "+total);
}
}).setMultipartParameter("platform", "android").setMultipartFile("image", new File(getPath(selectedImage))).asString().setCallback(new FutureCallback<String>()
{
@Override
public void onCompleted(Exception e, String result)
{
}
});
I actually works for me, Here is my code:
final File fileToUpload = new File(localFilePath);
Ion.with(context)
.load(Urls.UPLOAD_PICTURE)
.uploadProgressHandler(new ProgressCallback() {
@Override
public void onProgress(long uploaded, long total) {
// Displays the progress bar for the first time.
mNotifyManager.notify(notificationId, mBuilder.build());
mBuilder.setProgress((int) total, (int) uploaded, false);
}
})
.setTimeout(60 * 60 * 1000)
.setMultipartFile("upload", "image/jpeg", fileToUpload)
.asJsonObject()
// run a callback on completion
.setCallback(new FutureCallback<JsonObject>() {
@Override
public void onCompleted(Exception e, JsonObject result) {
// When the loop is finished, updates the notification
mBuilder.setContentText("Upload complete")
// Removes the progress bar
.setProgress(0, 0, false);
mNotifyManager.notify(notificationId, mBuilder.build());
if (e != null) {
Toast.makeText(context, "Error uploading file", Toast.LENGTH_LONG).show();
return;
}
Toast.makeText(context, "File upload complete", Toast.LENGTH_LONG).show();
}
});
}
Hope it helps someone :)