如何通过改型将成功的Response主体转换为特定类型? [英] How do I convert a successful Response body to a specific type using retrofit?

查看:102
本文介绍了如何通过改型将成功的Response主体转换为特定类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在异步模式下进行改装

public void success(T t, Response rawResponse)

是转换后的响应,而rawResponse是原始响应.这样您就可以访问原始响应和转换后的响应.

were t is the converted response, and rawResponse is the raw response. This provides you with access to both the raw response and the converted response.

在同步模式下,您可以获取转换后的响应原始响应

In sync mode you can get either the converted response OR the raw response

转换后的响应

@GET("/users/list")
List<User> userList();

原始响应

@GET("/users/list")
Response userList();

Response对象确实具有获取正文的方法

The Response object does have a method to get the body

TypedInput  getBody()

并且翻新api确实具有转换器类,可以将其转换为Java对象

and the retrofit api does have a converter class that can convert this to a java object

Object fromBody(TypedInput body,Type type)

但是我不知道如何获取Converter对象的实例

But I can not figure out how to get an instance of the Converter object

我也许能够创建Converter类的实例,但是这需要了解用于创建RestAdapter的Gson对象的知识,而我可能无法访问它.理想情况下,我想直接获得RestAdpater对转换器对象的引用.

I might be able to create an instance of the Converter class, but that would require knowledge of the Gson object used to create the RestAdapter, which I may not have access to. Ideally, I would like obtain a reference to the converter object directly the RestAdpater.

  1. 有没有办法获得改造所使用的默认Converter的引用?
  2. 有人知道默认转换器的构造方式吗?(没有默认构造函数,并且有两个构造函数 public GsonConverter(Gson gson)和public GsonConverter(Gson gson,String charset)
  3. 还有其他方法可以在同步模式下同时获取原始和转换后的响应吗?
  1. Is there a way to get a reference to the default Converter used by retrofit?
  2. Does anyone know how the default Converter is constructed? (there is no default constructor and there are two Constructors public GsonConverter(Gson gson) and public GsonConverter(Gson gson, String charset)
  3. Is there any other way to get both the raw and converted response in sync mode?

推荐答案

这是一个 StringConverter 类的示例,该类实现了在改造中找到的 Converter .基本上,您必须重写 fromBody()并告诉您想要的内容.

Here's an example of a StringConverter class that implements the Converter found in retrofit. Basically you'll have to override the fromBody() and tell it what you want.

public class StringConverter implements Converter {

    /*
     * In default cases Retrofit calls on GSON which expects a JSON which gives
     * us the following error, com.google.gson.JsonSyntaxException:
     * java.lang.IllegalStateException: Expected BEGIN_OBJECT but was
     * BEGIN_ARRAY at line x column x
     */

    @Override
    public Object fromBody(TypedInput typedInput, Type type)
            throws ConversionException {

        String text = null;
        try {
            text = fromStream(typedInput.in());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return text;
    }

    @Override
    public TypedOutput toBody(Object o) {
        return null;
    }

    // Custom method to convert stream from request to string
    public static String fromStream(InputStream in) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
        StringBuilder out = new StringBuilder();
        String newLine = System.getProperty("line.separator");
        String line;
        while ((line = reader.readLine()) != null) {
            out.append(line);
            out.append(newLine);
        }
        return out.toString();
    }
}

将此应用于您的请求,您将必须执行以下操作:

Applying this to your request you'll have to do the following:

// initializing Retrofit's rest adapter
RestAdapter restAdapter = new RestAdapter.Builder()
        .setEndpoint(ApiConstants.MAIN_URL).setLogLevel(LogLevel.FULL)
        .setConverter(new StringConverter()).build();

这篇关于如何通过改型将成功的Response主体转换为特定类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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