Retrofit 2排队失败但在http响应中得到200(Android REST)

时间:2017-06-30 10:06:49

标签: android rest gson retrofit retrofit2

登录主要电话:

public void work(){

        api = createService(context, XYZapi.class);
        Call<HashMap<String, String>> repo = api.doLogin(login);

        repo.enqueue(new Callback<HashMap<String, String>>() {
            @Override
            public void onResponse(Call<HashMap<String, String>> call, Response<HashMap<String, String>> response) {
                Headers headers = response.headers();
                mAuth = headers.get("Authorization");
                // get header value
                String cookie = response.headers().get("Set-Cookie");
                // TODO
                if (response.headers().get("Authorization")!= null) {
                    Log.d(TAG, "onResponse: "+response.body());
                    Log.d(TAG, "onResponse: "+response.headers().get("Authorization"));
                    mAuth = response.headers().get("Authorization");
                    callConfig();
                }
                Log.d(TAG, "onResponse error : "+response.errorBody().toString());

            }

            @Override
            public void onFailure(Call<HashMap<String, String>> call, Throwable t) {
                Log.d(TAG, "onFailure: "+t.getCause());
            }
        });

}

这里的电话直接转到失败。 但我在http log

中得到200

这是api:

XYZapi

@POST("/api/login")
    Call<HashMap<String, String>> doLogin(@Body LoginPojo login);

的CreateService

private static Retrofit.Builder builder = new Retrofit.Builder();

public static <S> S createService(Context context, Class<S> serviceClass) {
    HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
    logging.setLevel(HttpLoggingInterceptor.Level.BODY);

    OkHttpClient okHttpClient = new OkHttpClient().newBuilder().addInterceptor(new Interceptor() {
        @Override
        public okhttp3.Response intercept(@NonNull Chain chain) throws IOException {
            Request original = chain.request();
            String credentials = "username@gmail.com" + ":" + "password";
            final String basic =
                    "Bearer " + Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP);

            Request request = original.newBuilder()
                    .addHeader("Authorization", basic)
                    .method(original.method(), original.body())
                    .build();
            return chain.proceed(request);
        }
    }).addInterceptor(logging).build();

    builder.client(okHttpClient);
    builder.baseUrl("https://app.xyz.com");

    Retrofit adapter = builder.addConverterFactory(GsonConverterFactory.create()).build();
    return adapter.create(serviceClass);
}

我想要的是: 1.我想阅读“授权”的回复标题; 2.我想读取响应数据并将json解析为对象。 3.我想根据响应代码(状态)调用另一个服务。

到目前为止,我一直在使用改装版1.9但由于某些原因,我需要更新它以进行改造。

0 个答案:

没有答案