0

I built the request like following:

public static ApiBuilder newInstance(){
        GsonBuilder gsonBuilder = new GsonBuilder()
                .registerTypeAdapter(Boolean.class, booleanAsIntAdapter)
                .registerTypeAdapter(boolean.class, booleanAsIntAdapter)
                .registerTypeAdapter(Date.class, (JsonDeserializer<Date>) (json, typeOfT, context)
                        -> new Date(Long.valueOf(json.getAsString())));

        OkHttpClient client = new OkHttpClient();
        client.setConnectTimeout(180, TimeUnit.SECONDS);
        client.setReadTimeout(240, TimeUnit.SECONDS);

        String url = MyApplicationConfiguration.getInstance().get("urlofservice");

        RestAdapter restAdapter = new RestAdapter.Builder()
                .setRequestInterceptor(
                        request -> {
                            request.addHeader(deviceIdHeader, MyApplication.getDeviceId());
                        }

                ).setConverter(new GsonConverter(gsonBuilder.create()))
                .setEndpoint(url)
                .setClient(new OkClient(client)).build();

        restAdapter.setLogLevel(RestAdapter.LogLevel.FULL);

        return restAdapter.create(ApiExtendedValidation.class);
    }

    private static final TypeAdapter<Boolean> booleanAsIntAdapter = new TypeAdapter<Boolean>() {
        @Override
        public void write(JsonWriter out, Boolean value) throws IOException {
            if (value == null) {
                out.nullValue();
            } else {
                out.value(value);
            }
        }

        @Override
        public Boolean read(JsonReader in) throws IOException {
            JsonToken peek = in.peek();
            switch (peek) {
                case BOOLEAN:
                    return in.nextBoolean();
                case NULL:
                    in.nextNull();
                    return null;
                case NUMBER:
                    return in.nextInt() != 0;
                case STRING:
                    return Boolean.parseBoolean(in.nextString());
                default:
                    throw new IllegalStateException("Expected BOOLEAN or NUMBER but was " + peek);
            }
        }
    };

The API interface is:

@Headers("Content-Type: application/json")
@POST("/validate")
Observable<QRValidateResponse> validateQRCode(@Body QRRequest request);

The QRRequest is an object, that contains a String field with a Base64 ecnoded string

Encoding example

byte[] data = rawResult.getText().getBytes(StandardCharsets.ISO_8859_1);
        String encodedResult = Base64.encodeToString(data, Base64.DEFAULT);

object example

request{
    "lang": "EN",
    "content": "SGVsbG8gV29ybGQh="
}

When Retrofit execute the request, the request.content changes like follwing:

SGVsbG8gV29ybGQh\u003d

The Server doesn't recognize the content, so its response in an error.

Why during the request the content changes? Is there a way to avoid it?

SOLUTION

There was an escaping error linked to the use of Gson. According to this post, solved adding this disableHtmlEscaping() to the GsonBuilder.

MikeKeepsOnShine
  • 1,730
  • 4
  • 23
  • 35

0 Answers0