Example usage for com.squareup.okhttp ResponseBody string

List of usage examples for com.squareup.okhttp ResponseBody string

Introduction

In this page you can find the example usage for com.squareup.okhttp ResponseBody string.

Prototype

public final String string() throws IOException 

Source Link

Document

Returns the response as a string decoded with the charset of the Content-Type header.

Usage

From source file:retrofit2.CallTest.java

License:Apache License

@Test
public void http204SkipsConverter() throws IOException {
    final Converter<ResponseBody, String> converter = spy(new Converter<ResponseBody, String>() {
        @Override// w w w  .  j a v a 2 s. c  o  m
        public String convert(ResponseBody value) throws IOException {
            return value.string();
        }
    });
    Retrofit retrofit = new Retrofit.Builder().baseUrl(server.url("/"))
            .addConverterFactory(new ToStringConverterFactory() {
                @Override
                public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations,
                        Retrofit retrofit) {
                    return converter;
                }
            }).build();
    Service example = retrofit.create(Service.class);

    server.enqueue(new MockResponse().setStatus("HTTP/1.1 204 Nothin"));

    Response<String> response = example.getString().execute();
    assertThat(response.code()).isEqualTo(204);
    assertThat(response.body()).isNull();
    verifyNoMoreInteractions(converter);
}

From source file:retrofit2.CallTest.java

License:Apache License

@Test
public void http205SkipsConverter() throws IOException {
    final Converter<ResponseBody, String> converter = spy(new Converter<ResponseBody, String>() {
        @Override/*w w w.j  av a2s . com*/
        public String convert(ResponseBody value) throws IOException {
            return value.string();
        }
    });
    Retrofit retrofit = new Retrofit.Builder().baseUrl(server.url("/"))
            .addConverterFactory(new ToStringConverterFactory() {
                @Override
                public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations,
                        Retrofit retrofit) {
                    return converter;
                }
            }).build();
    Service example = retrofit.create(Service.class);

    server.enqueue(new MockResponse().setStatus("HTTP/1.1 205 Nothin"));

    Response<String> response = example.getString().execute();
    assertThat(response.code()).isEqualTo(205);
    assertThat(response.body()).isNull();
    verifyNoMoreInteractions(converter);
}

From source file:retrofit2.ToStringConverterFactory.java

License:Apache License

@Override
public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations,
        Retrofit retrofit) {// w  ww .  ja  va2  s. c om
    if (String.class.equals(type)) {
        return new Converter<ResponseBody, String>() {
            @Override
            public String convert(ResponseBody value) throws IOException {
                return value.string();
            }
        };
    }
    return null;
}