'java.net.ProtocolException: expected * bytes but received * #4302

@post()
@multipart
Call<FileUploadResult[]> upload File(@nonnull @part MultipartBody.Part... files);


MultipartBody.Part body = MultipartBody.Part.createFormData("file","Timesheet-" + 
new Date().getTime() + FileUtils.IMAGE_EXT,
RequestBody.create(
MediaType.parse(URLConnection.guessContentTypeFromName(file.getName())), file));

ServiceBuilder.getService().uploadFile(body).enqueue(syncCallback);

I got this bug when upload an image using MultipartBody.Part and it throw that exception. Here is the code: java.net.ProtocolException: expected * bytes but received *

As some one who have the same issue, its wrong content-lenght between HEADER and BODY but i dont think so cause i dont modify anything when call API and its not happen all the time



Solution 1:[1]

I found the problem is when save photo,they use another thread to save image and sometime it not enough time to save the image to sdcard -> when push image to server,its not enough the size of the image -> caught that exception

Solution 2:[2]

I got same error while uploading a .csv file to server with multipart body request. Issue was that I was getting accelerometer and gyroscope sensor readings and was saving in .csv file continuously. When I was getting readings and tried to send file in the api it gives me exact this error. I stopped sensors saved all readings and than sent file to the Api without any error.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 shadow
Solution 2 Awais Yaseen