retofit2如何在正文中发布普通字符串

时间:2016-04-12 14:46:16

标签: android retrofit2

我正在使用retrofit2

  

com.squareup.retrofit2:改型:2.0.1

我的rest API(Post)返回纯字符串作为响应

如何在正文中发布字符串?

我的代码是

我使用自定义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;
        }


    }

和我的Retrofit构建器

Retrofit retrofit = new Retrofit.Builder().baseUrl(root_url).addConverterFactory(new ToStringConverterFactory()).build();

我的回调是

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

我的RetroService界面是

public interface RetroService {


    @POST("/validateUser")
    Call<String> login(@Body String body);

}

和API调用

Call<String> loginCall = service.login(req);
                    loginCall.enqueue(callback);

我得到的是onresponse()response.body它总是显示为null。

请澄清我做错了什么。

先生。

1 个答案:

答案 0 :(得分:0)

我在线谷歌,发现最新的GSON可以处理纯文本响应。将其添加到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'

在您的代码中使用此功能

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

请尝试查看是否可以在响应中处理字符串。

这是我在网上找到的,不确定它是否适用于你。请试一试。 link