Skip to content

Okhttp multipart/form-data post frequently throws java.net.ProtocolException on Samsung S6 #2830

@jennadeng

Description

@jennadeng

Okhttp multipart/form-data always failing on java.net.ProtocolException: expected x bytes but received y bytes, and the amount is always an extra of 67 bytes. Tested the code for multipart/form-data on HTC, Nexus, Motorola, Samsung s7 and two Samsung S6. The multipart post works on all the other phone but the two S6. The Samsung S6, S7 and HTC were all running on the same OS version which is Android 6 Marshmallow. Here is the code I was using:

` public void HTTPPostImage(File imageFile, String imageName, Context context)
throws IOException {

    // Build the RequestBody with the event id and the photo.
    RequestBody requestBody = new MultipartBody.Builder()
            .setType(MultipartBody.FORM)
            .addFormDataPart("name", imageName)
            .addFormDataPart("action", "upload")
            .addFormDataPart("filename", imageName)
            .addFormDataPart("image", imageName,
                    RequestBody.create(MEDIA_TYPE_JPEG, imageFile))
            .build();


    Request request = new Request.Builder().url(POST_NEW_IMAGE_URL).post(requestBody).build();

    try (Response response = mHttpClient.newCall(request).execute()) {
        if (!response.isSuccessful()) {
            throw new IOException(TAG + "The response is  " + response.body());
        }
    }
}`

Also, the issue exist in both okhttp2 and okhttp3.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions