I'm using retrofit2 for uploading binary image file:
File file = new File(filePath);
RequestBody requestBody = new ProgressRequestBody(
MediaType.parse("application/octet-stream"),
file,
this);
Call<ResponseBody> call = service.uploadFile(requestBody);
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call,
Response<ResponseBody> response) {
if (!response.isSuccessful()) {
Toasti.showS("fail");
return;
}
Log.v("Upload", "success");
UploadFileOutput uploadFileOutput = new UploadFileOutput();
try {
uploadFileOutput =
new Gson().fromJson(response.body().string(), UploadFileOutput.class);
} catch (IOException e) {
e.printStackTrace();
}
ImageLoader.getInstance().displayImage(uploadFileOutput.imageSrc, giftImageview);
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
Log.e("Upload error:", t.getMessage());
Toasti.showS("fail");
}
});
But most of the times(sometimes it works fine!) it fails because of this error:
sendto failed: ECONNRESET (Connection reset by peer)
I read lots of post and tutorials:
java.net.SocketException: sendto failed: ECONNRESET (Connection reset by peer)
Getting “SocketException : Connection reset by peer” in Android
retrofit.RetrofitError: sendto failed: ECONNRESET (Connection reset by peer)
But none of them helped me.
I test api method with postman, It always works without any error!