java Retrofit 2.0 beta1:如何发布原始字符串主体

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32656358/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 20:34:43  来源:igfitidea点击:

Retrofit 2.0 beta1: how to post raw String body

javaandroidretrofit

提问by Vitaliy Istomov

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

我正在寻找某种方法来使用新的 Retrofit 2.0b1 发布带有原始正文的请求。像这样的东西:

@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.

据我所知,应该有某种海峡“到字符串”转换器,但我还不清楚它是如何工作的。

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

有一些方法可以使用 TypedInput 在 1.9 中实现它,但它在 2.0 中不再有帮助。

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

PS是的,后端很愚蠢,据我所知没有人会为我改变它:(

Thanks for help.

感谢帮助。

采纳答案by Nikola Despotoski

You should be registering a converter for your Typewhen you are building your Retrofitusing addConverter(type, converter).

Type在构建Retrofitusing时,您应该为自己注册一个转换器addConverter(type, converter)

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

Converter<T>在 2.0 中使用类似的方法使用 1.x 版本中的旧转换器。

Your StringConvertershould be something like this:

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());
    }
}

Notes:

笔记:

  1. ByteStringis from Okio library.
  2. Mind the Charsetin your MediaType
  1. ByteString来自 Okio 图书馆。
  2. 介意Charset在你的MediaType

回答by Wasky

In Retrofit 2.0.0-beta2 you can use RequestBodyand ResponseBodyto post a body to server using Stringdata and read from server's response body as String.

在 Retrofit 2.0.0-beta2 中,您可以使用RequestBodyResponseBody使用String数据将正文发布到服务器,并从服务器的响应正文中读取为String.

First you need to declare a method in your RetrofitService:

首先,您需要在 RetrofitService 中声明一个方法:

interface RetrofitService {
    @POST("path")
    Call<ResponseBody> update(@Body RequestBody requestBody);
}

Next you need to create a RequestBodyand Callobject:

接下来你需要创建一个RequestBodyCall对象:

Retrofit retrofit = new Retrofit.Builder().baseUrl("http://somedomain.com").build();
RetrofitService retrofitService = retrofit.create(RetrofitService.class);

String strRequestBody = "body";
RequestBody requestBody = RequestBody.create(MediaType.parse("text/plain"),strRequestBody);
Call<ResponseBody> call = retrofitService.update(requestBody);

And finally make a request and read response body as String:

最后发出请求并将响应正文读取为String

try {
    Response<ResponseBody> response = call.execute();
    if (response.isSuccess()) {
        String strResponseBody = response.body().string();
    }
} catch (IOException e) {
    // ...
}