改造2.0β1的:如何发布原始字符串体 [英] Retrofit 2.0 beta1: how to post raw String body

查看:284
本文介绍了改造2.0β1的:如何发布原始字符串体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要寻找一些方法来发布与原体与新改造2.0b1请求。事情是这样的:

I am looking for some way to post request with raw body with new Retrofit 2.0b1. Something like this:

@POST("/token")
Observable<TokenResponse> getToken(@Body String body);

据我的理解,应该有某种形式的两岸到字符串转换器,但目前尚不清楚这对我来说又是如何工作的。

As far as I understand, there should be some kind of strait "to-string" converter, but it is not clear for me yet how it works.

有分别的方式,使之在1.9与TypedInput发生,但它不能在2.0帮助了。

There were ways to make it happen in 1.9 with TypedInput, but it does not help in 2.0 anymore.

PS是的,后端是愚蠢的,据我看到没有人会改变对我来说:(

PS yes, the backend is stupid, as far as I see nobody will change it for me :(

感谢您的帮助。

推荐答案

您应该被注册为一个转换器的键入当你构建你的改造使用 addConverter(类型转换器)

You should be registering a converter for your Type when you are building your Retrofit using addConverter(type, converter).

转换器和LT; T&GT; 在2.0版本1.x的使用旧转换器采用类似的方法

Converter<T> in 2.0 uses similar approach using old Converter in 1.x version.

StringConverter 应该是这样的:

public class StringConverter implements Converter<Object>{


    @Override
    public String fromBody(ResponseBody body) throws IOException {
        return ByteString.read(body.byteStream(), (int) body.contentLength()).utf8();
    }

    @Override
    public RequestBody toBody(Object value) {
        return RequestBody.create(MediaType.parse("text/plain"), value.toString());
    }
}

注:


  1. 字节字符串是奥基奥库。

  2. 胸怀字符集的MediaType

  1. ByteString is from Okio library.
  2. Mind the Charset in your MediaType

这篇关于改造2.0β1的:如何发布原始字符串体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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