[英]How to get String in response using Retrofit 2?
I would like to understand how Retrofit works, but the official documentation is very weak. 我想了解翻新的工作原理,但是官方文档非常薄弱。
I need to make a very simple GET
request and get the response as a String
. 我需要发出一个非常简单的GET
请求,并以String
获取响应。
Now I use standard HTTPUrlConnection
and it works nicely, just request - response 现在,我使用标准的HTTPUrlConnection
,它可以很好地工作,只是请求-响应
Can anyone tell me how to get a String
response without converting it to an object or something like that? 谁能告诉我如何获取String
响应而不将其转换为对象或类似的东西?
You can use ScalarsConverterFactory for strings and both primitives and their boxed types to text/plain bodies. 您可以将ScalarsConverterFactory用于字符串,并将原语及其框式输入到文本/纯文本主体。 Add this dependency to your build.gradle file: 将此依赖项添加到您的build.gradle文件中:
compile 'com.squareup.retrofit2:converter-scalars:2.1.0'
Try this: 尝试这个:
public interface ExampleService {
@GET("/users/{user}/repos")
Call<String> listRepos(@Path("user") String user);
}
And add ScalarsConverterFactory to your builder: 并将ScalarsConverterFactory添加到您的构建器中:
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(ScalarsConverterFactory.create())
.build();
You can then retrieve this string like this: 然后,您可以像下面这样检索该字符串:
Call<String> call = exampleService.listRepos(user);
Response<String> response = call.execute();
String value = response.body();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.