gpt4 book ai didi

java - 从 retrofit2 获取字符串响应体

转载 作者:太空宇宙 更新时间:2023-11-03 11:30:46 25 4
gpt4 key购买 nike

我正在使用 retrofit1 旧样式

@GET("/loginUser")
public Call<Response> login(
@Query("email") String email,
@Query("password") String password,
Callback<Response> callback);

现在我不想得到“用户”类,但我想得到一个字符串响应。

以前我们使用的是“Response”,但是在 retrofit2 中没有 Response,

如何在不使用任何 json 解析的情况下从服务器获取字符串响应或完整主体响应?

最佳答案

创建这个类

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

编辑 以上代码适用于 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)!

转换器将按照它们被添加的顺序被调用,如果一个转换器消耗了响应,则不会调用下面的转换器。

关于java - 从 retrofit2 获取字符串响应体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35520012/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com