Retrofit with String response

前端 未结 2 1627
挽巷
挽巷 2021-01-18 17:24

I want a POST call on an URL, and as response I just get a String \"ok\" or \"no\".. So I have here my interface like this:

public interface registerAPI
{
           


        
2条回答
  •  渐次进展
    2021-01-18 17:34

    @Klatschen your answer is right,in Retrofit2 the abstarct function has changed.

    public final class StringConverterFactory extends Converter.Factory {
    @Override
    public Converter responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
        if (String.class.equals(type)) {
            return new Converter() {
                @Override
                public Object convert(ResponseBody responseBody) throws IOException {
                    return responseBody.string();
                }
            };
        }
    
        return null;
    }
    
    @Override
    public Converter requestBodyConverter(Type type,
                                                          Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) {
        if (String.class.equals(type)) {
            return new Converter() {
                @Override
                public RequestBody convert(String value) throws IOException {
                    return RequestBody.create(MediaType.parse("text/plain"), value);
                }
            };
        }
        return null;
    }
    }
    

提交回复
热议问题