从 retrofit2 获取字符串响应体
Get String response body from retrofit2
我正在使用 retrofit1 旧样式
@GET("/loginUser")
public Call<Response> login(
@Query("email") String email,
@Query("password") String password,
Callback<Response> callback);
现在我不想得到 "User" class 但是我想得到一个字符串响应。
之前我们使用的是"Response"但是在retrofit2中没有响应,
如何在不使用任何 json 解析的情况下从服务器获取字符串响应或完整主体响应?
创建这个class
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Converter;
import retrofit2.Retrofit;
public class ToStringConverterFactory extends Converter.Factory {
private static final MediaType MEDIA_TYPE = MediaType.parse("text/plain");
@Override
public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
if (String.class.equals(type)) {
return new Converter<ResponseBody, String>() {
@Override
public String convert(ResponseBody value) throws IOException {
return value.string();
}
};
}
return null;
}
@Override
public Converter<?, RequestBody> requestBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
if (String.class.equals(type)) {
return new Converter<String, RequestBody>() {
@Override
public RequestBody convert(String value) throws IOException {
return RequestBody.create(MEDIA_TYPE, value);
}
};
}
return null;
}
}
与
一起使用
Retrofit retrofit = new Retrofit.Builder()
.addConverterFactory(new ToStringConverterFactory())
.build();
编辑:
您必须将其定义为
@GET("/loginUser")
public Call<String> login(
@Query("email") String email,
@Query("password") String password);
retrofit2 不支持回调,因此您必须将其删除。要使其异步,您必须执行
Call<String> call = service.login(username, password);
call.enqueue(new Callback<String>() {}
EDIT 以上代码用于 retrofit2 beta 3。对于 retrofit:2.1.0,您必须将 ToStringConverterFactory 创建为 -
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Converter;
import retrofit2.Retrofit;
public class ToStringConverterFactory extends Converter.Factory {
private static final MediaType MEDIA_TYPE = MediaType.parse("text/plain");
@Override
public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
if (String.class.equals(type)) {
return new Converter<ResponseBody, String>() {
@Override
public String convert(ResponseBody value) throws IOException {
return value.string();
}
};
}
return null;
}
@Override
public Converter<?, RequestBody> requestBodyConverter(Type type, Annotation[] parameterAnnotations,
Annotation[] methodAnnotations, Retrofit retrofit) {
if (String.class.equals(type)) {
return new Converter<String, RequestBody>() {
@Override
public RequestBody convert(String value) throws IOException {
return RequestBody.create(MEDIA_TYPE, value);
}
};
}
return null;
}
}
小知识:如果你想要多个转换器(例如,如上所示的字符串转换器和 GSON 转换器):
确保首先指定专用转换器(例如字符串转换器),最后指定通用转换器(如 Gson)!
转换器将按添加的顺序调用,如果一个转换器消耗了响应,则不会调用下面的转换器。
Retrofit 2.0.0-beta3 添加了一个转换器标量模块,提供了一个 Converter.Factory 用于将 String、8 种基本类型和 8 种装箱基本类型转换为 text/plain 主体。在您的普通转换器之前安装它以避免将这些简单的标量传递给例如 JSON 转换器。
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(API_BASE_URL)
.client(getClient())
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.build();
ScalarsConverterFactory 高于 GsonConverterFactory
我正在使用 retrofit1 旧样式
@GET("/loginUser")
public Call<Response> login(
@Query("email") String email,
@Query("password") String password,
Callback<Response> callback);
现在我不想得到 "User" class 但是我想得到一个字符串响应。
之前我们使用的是"Response"但是在retrofit2中没有响应,
如何在不使用任何 json 解析的情况下从服务器获取字符串响应或完整主体响应?
创建这个class
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Converter;
import retrofit2.Retrofit;
public class ToStringConverterFactory extends Converter.Factory {
private static final MediaType MEDIA_TYPE = MediaType.parse("text/plain");
@Override
public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
if (String.class.equals(type)) {
return new Converter<ResponseBody, String>() {
@Override
public String convert(ResponseBody value) throws IOException {
return value.string();
}
};
}
return null;
}
@Override
public Converter<?, RequestBody> requestBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
if (String.class.equals(type)) {
return new Converter<String, RequestBody>() {
@Override
public RequestBody convert(String value) throws IOException {
return RequestBody.create(MEDIA_TYPE, value);
}
};
}
return null;
}
}
与
一起使用Retrofit retrofit = new Retrofit.Builder()
.addConverterFactory(new ToStringConverterFactory())
.build();
编辑: 您必须将其定义为
@GET("/loginUser")
public Call<String> login(
@Query("email") String email,
@Query("password") String password);
retrofit2 不支持回调,因此您必须将其删除。要使其异步,您必须执行
Call<String> call = service.login(username, password);
call.enqueue(new Callback<String>() {}
EDIT 以上代码用于 retrofit2 beta 3。对于 retrofit:2.1.0,您必须将 ToStringConverterFactory 创建为 -
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Converter;
import retrofit2.Retrofit;
public class ToStringConverterFactory extends Converter.Factory {
private static final MediaType MEDIA_TYPE = MediaType.parse("text/plain");
@Override
public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
if (String.class.equals(type)) {
return new Converter<ResponseBody, String>() {
@Override
public String convert(ResponseBody value) throws IOException {
return value.string();
}
};
}
return null;
}
@Override
public Converter<?, RequestBody> requestBodyConverter(Type type, Annotation[] parameterAnnotations,
Annotation[] methodAnnotations, Retrofit retrofit) {
if (String.class.equals(type)) {
return new Converter<String, RequestBody>() {
@Override
public RequestBody convert(String value) throws IOException {
return RequestBody.create(MEDIA_TYPE, value);
}
};
}
return null;
}
}
小知识:如果你想要多个转换器(例如,如上所示的字符串转换器和 GSON 转换器):
确保首先指定专用转换器(例如字符串转换器),最后指定通用转换器(如 Gson)!
转换器将按添加的顺序调用,如果一个转换器消耗了响应,则不会调用下面的转换器。
Retrofit 2.0.0-beta3 添加了一个转换器标量模块,提供了一个 Converter.Factory 用于将 String、8 种基本类型和 8 种装箱基本类型转换为 text/plain 主体。在您的普通转换器之前安装它以避免将这些简单的标量传递给例如 JSON 转换器。
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(API_BASE_URL)
.client(getClient())
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.build();
ScalarsConverterFactory 高于 GsonConverterFactory