orhanobut / wasp

Compact and easy to use, 'all-in-one' android network solution

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Problem with post body with byte[], body changed!

fengshenzhu opened this issue · comments

Parser parse post body returns String, while VolleyRequest return body byte[] with convert string body to byte[] with encoding "UTF-8". The converted byte[] may be changed after the convertion.
For example, I want to post a 665 length byte[], in Parser I convert it to String with
new String(bytes, "UTF-8")
, but in VolleyRequest I get a 669 length byte[] after
requestBody.getBytes(PROTOCOL_CHARSET).

What do you mean by parser? or specifically how did you test it? I couldn't get tthe issue. Can you show me how did you try? That would be great sample thus we can try to figure out the difference. Thanks

Sorry for my delay.
The UTF-8 is variable-length encoding. When I post a byte[] format body via WASP, the original byte[] body changed for Volley.

public class MyWaspParser extends GsonParser {
    @Override
    public String toBody(Object body) {
        if (body instanceof byte[]) {
            String bodyString = new String((byte[]) body);
            return bodyString;
        }
        return super.toBody(body);
    }
}
interface MyWaspService {
    @POST("/path")
    void postBody(@Body byte[] bytes, Callback<String> callback);
}
byte[] body = new byte[]{50, 0, -1, 28, -24};
waspService.postBody(body, new Callback<String>() {
    @Override
    public void onSuccess(Response response, String s) {
    }

    @Override
    public void onError(WaspError error) {
    }
});

In VolleyRequest, getBody() return the byte[] body {50, 0, -17, -65, -67, 28, -17, -65, -67}. The body changed!

I have solved it by change the default encoding PROTOCOL_CHARSET in VolleyRequest from "UTF-8" to "ISO-8859-1", and using the same encoding "ISO-8859-1" to new body String in Parser:

String bodyString = new String((byte[]) body, "ISO-8859-1");

Great! At least work around works. Although not sure we should apply any functionality for this.