问题描述
我要寻找一些方法来发布与原体与新改造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());
}
}
注:
-
字节字符串
是奥基奥库。 - 胸怀
字符集
在的MediaType
ByteString
is from Okio library.- Mind the
Charset
in yourMediaType
这篇关于改造2.0β1的:如何发布原始字符串体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!