0

I have cross checked a lot of reference before actually asking the question. As most people I am calling a web service and and getting the above result. My problem is rather more specific, so before asking let me add my code snippet

public static WebService getRestService(String newApiBaseUrl, String accessToken)
    {
        OkHttpClient.Builder httpClient = new OkHttpClient.Builder();


        if (BuildConfig.DEBUG) {
            HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
            logging.setLevel(HttpLoggingInterceptor.Level.BODY);
            httpClient.addInterceptor(logging);
        }


        Gson gson = new GsonBuilder()
                .setLenient()
                .create();

        restBuilder = new Retrofit.Builder()
                .baseUrl(newApiBaseUrl)
                .client(httpClient.build())
                .addConverterFactory(GsonConverterFactory.create(gson));

        if (accessToken != null) {
            AuthenticationInterceptor interceptor =
                    new AuthenticationInterceptor(accessToken);

            if (!httpClient.interceptors().contains(interceptor)) {
                httpClient.addInterceptor(interceptor);

                restBuilder.client(httpClient.build());
            }
        }

        retrofit = restBuilder.build();
        return retrofit.create(WebService.class);
    }

Please ignore the interpreter. This is my service generator class through which all web service is called. I am calling a POST method which will return me a simple response.

I can see my correct response in "Android Monitor". But when its passes through JsonConverterFactory, it gives me above error. One thing I know for sure that web service is returning a simple text response as there is nothing specific to return to application. Is there a way by which I can convert my text response to class. Please free shout back if I missed anything.

Also adding my interface method

@Headers({
            Constants.ApiHeader.CONTENT_TYPE_JSON
    })

    @POST("POST_URL/{number}")
    Call<MyResponseModel> getActualProductLocation(@Header("access_token") String accessToken, @Path("number") String number, @Body List<MyRequestModel> body);

Update I managed to get a part of code snippet from server. This is what their response look like:

Response.status(200).entity("Response Success").build()
Community
  • 1
  • 1
Android
  • 3,828
  • 9
  • 46
  • 79
  • Post `MyResponseModel`. Most possibly it is not in the right format. – azizbekian Feb 15 '17 at 14:45
  • Its empty as there is no key value pair returned by server(JSON data).Do I have to define any string variable in the class? – Android Feb 15 '17 at 14:46
  • *My problem is rather more specific* how so? These problems are all the same: the json data is not what the converter expected. It's posible the gson converter does not find a lone string valid json (it is, though) – Tim Feb 15 '17 at 14:46
  • @Android, server returns you empty string. – azizbekian Feb 15 '17 at 14:47
  • its returning me something "Response success" – Android Feb 15 '17 at 14:49
  • @TimCastelijns Here I know server is returning simple text and not JSON, so I have to consume text instead of converting JSON. – Android Feb 15 '17 at 14:50

1 Answers1

0

Because the backend doesn't reply you with JSON formatted data, you have to handle it with custom converter.

Here's an example of a custom converter that would fulfil your needs.

A snippet from StringConverterFactory.java

...
private static class StringConverter implements Converter<String> {
    private static final MediaType PLAIN_TEXT = MediaType.parse("text/plain; charset=UTF-8");

    @Override
    public String fromBody(ResponseBody body) throws IOException {
        return new String(body.bytes());
    }

    @Override
    public RequestBody toBody(String value) {
        return RequestBody.create(PLAIN_TEXT, convertToBytes(value));
    }

    private static byte[] convertToBytes(String string) {
        try {
            return string.getBytes("UTF-8");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }
}
...

And then when creating your Retrofit:

Retrofit retrofit = new Retrofit.Builder()
   ...
   .addConverterFactory(StringConverterFactory.create());
   ...
   .build();
azizbekian
  • 60,783
  • 13
  • 169
  • 249
  • Hey Thanks buddy, I will try it out. I have one question. If I start using custom converter, I assume I still would be able to use my general converter, and it would not interfere with my existing converter. Is that right? – Android Feb 16 '17 at 06:20
  • You can have one converter per Retrofit instance. You cannot change them dynamically. If you want to have different converters, you have to initialize different Retrofit instances with that converters. – azizbekian Feb 16 '17 at 06:22
  • I am having issue with imports. Are these classes belong to retrofit? Package like `import retrofit2.Converter;` – Android Feb 16 '17 at 06:34
  • [Converter](https://square.github.io/retrofit/2.x/retrofit/retrofit2/Converter.html) comes with Retrofit's package. – azizbekian Feb 16 '17 at 13:42