[英]retofit2 how to post plain String in body
I'm using retrofit2 我正在使用Retrofit2
com.squareup.retrofit2:retrofit:2.0.1
com.squareup.retrofit2:retrofit:2.0.1
My rest API(Post) returns plain string as response 我的其余API(Post)返回纯字符串作为响应
how to post String in body? 如何在体内张贴字符串?
My code is 我的代码是
I'm using custom String Converter Factory 我正在使用自定义字符串转换器工厂
public final class ToStringConverterFactory extends Converter.Factory {
@Override
public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
//noinspection EqualsBetweenInconvertibleTypes
if (String.class.equals(type)) {
return new Converter<ResponseBody, Object>() {
@Override
public Object convert(ResponseBody responseBody) throws IOException {
return responseBody.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(MediaType.parse("text/plain"), value);
}
};
}
return null;
}
}
and my Retrofit builder is 我的改造制造商是
Retrofit retrofit = new Retrofit.Builder().baseUrl(root_url).addConverterFactory(new ToStringConverterFactory()).build();
and my callback is 我的回调是
Callback<String> callback = new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
System.out.println("===>>> onResponse body res "+response.body());
System.out. println("===>>> onResponse error body res "+response.errorBody());
}
@Override
public void onFailure(Call<String> call, Throwable t) {
System.out.println("===>>> onFailure "+call.toString()+" "+t.toString());
t.printStackTrace();
}
};
My RetroService interface is 我的RetroService界面是
public interface RetroService {
@POST("/validateUser")
Call<String> login(@Body String body);
}
and the API call 和API调用
Call<String> loginCall = service.login(req);
loginCall.enqueue(callback);
What i'm getting is in onresponse() response.body its always showing as null. 我得到的是onresponse()response.body,它始终显示为null。
Please clarify what i'm doing wrong. 请说明我在做什么错。
Thx in adv. 副词
I google online, finding that latest GSON can handle plain text response. 我在Google上在线搜索,发现最新的GSON可以处理纯文本响应。 Add this into gradle:
将此添加到gradle中:
compile 'com.google.code.gson:gson:2.6.2'
compile 'com.squareup.okhttp3:okhttp:3.1.2'
compile 'com.squareup.retrofit2:converter-gson:2.0.1'
compile 'com.squareup.okhttp3:logging-interceptor:3.2.0'
Using this in your code 在您的代码中使用它
import com.google.gson.Gson;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
// Add log
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
httpClient.interceptors().add(logging);
Gson gson = new GsonBuilder()
.setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")
.create();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(root_url)
.addConverterFactory(GsonConverterFactory.create(gson))
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.client(httpClient.build())
.build();
Please give a try to see whether you can handle String in response. 请尝试看看是否可以处理String作为响应。
This is what I find online, not sure whether it is working for u. 这是我在网上找到的,不确定是否对您有用。 Please give a try.
请尝试一下。 link
链接
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.