从改装2获取字符串响应体 [英] Get String response body from retrofit2

查看:133
本文介绍了从改装2获取字符串响应体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用retrofit1旧样式

I am using retrofit1 old style

@GET("/loginUser")
    public Call<Response> login(
            @Query("email") String email,
            @Query("password") String password,
            Callback<Response> callback);

现在我不想得到用户类,但是我想获得一个String响应。

Now i don't want to get "User" class however i want to get a String response.

以前我们使用响应但是在retrofit2中没有响应,

Previously we were using "Response" However there is no Response in retrofit2,

我如何获取字符串

推荐答案

创建这个课程

import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Converter;
import retrofit2.Retrofit;

public class ToStringConverterFactory extends Converter.Factory {
    private static final MediaType MEDIA_TYPE = MediaType.parse("text/plain");


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

    @Override
    public Converter<?, RequestBody> requestBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {

        if (String.class.equals(type)) {
            return new Converter<String, RequestBody>() {
                @Override
                public RequestBody convert(String value) throws IOException {
                    return RequestBody.create(MEDIA_TYPE, value);
                }
            };
        }
        return null;
    }
}

使用它与

Retrofit retrofit = new Retrofit.Builder()
                        .addConverterFactory(new ToStringConverterFactory())
                        .build();

编辑:
您必须将其定义为



You have to define it as

@GET("/loginUser")
    public Call<String> login(
            @Query("email") String email,
            @Query("password") String password);

在retrofit2中不支持回调,所以你必须删除它。要使其异步,您必须执行

There is no callback supported in retrofit2 so you have to remove that. To make it asynchronous, you have to do

Call<String> call = service.login(username, password);
call.enqueue(new Callback<String>() {}

以上代码适用于retrofit2 beta 3.对于改装:2.1.0,您必须创建ToStringConverterFactory作为 -

EDIT The above code was for retrofit2 beta 3. For retrofit:2.1.0, you have to create ToStringConverterFactory as -

import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Converter;
import retrofit2.Retrofit;

public class ToStringConverterFactory extends Converter.Factory {
    private static final MediaType MEDIA_TYPE = MediaType.parse("text/plain");


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

    @Override
    public Converter<?, RequestBody> requestBodyConverter(Type type, Annotation[] parameterAnnotations,
                                                          Annotation[] methodAnnotations, Retrofit retrofit) {

        if (String.class.equals(type)) {
            return new Converter<String, RequestBody>() {
                @Override
                public RequestBody convert(String value) throws IOException {
                    return RequestBody.create(MEDIA_TYPE, value);
                }
            };
        }
        return null;
    }
}

知道:您想要拥有多个转换器(例如,如上所示的字符串转换器,还有GSON转换器):

确保先指定专用转换器(例如String转换器)和通用转换器(如Gson )最后!

GOOD TO KNOW: Incase you want to have multiple converters (e.g. a String converter as shown above and also a GSON converter):
Make sure you specify the special-purpose converters first (e.g. String converter) and general converters (like Gson) last!

转换器将按照已添加的顺序调用,如果转换器消耗响应,则不会调用下一个转换器。

Converters will be called by the order they have been added, if a converter consumed the response, the follwoing converters will not be called.

这篇关于从改装2获取字符串响应体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆