改造+基本认证

时间:2017-07-19 12:43:29

标签: android retrofit2 basic-authentication

我已经使用基本身份验证实现了改造,我在登录请求时发送了@header,但是当我调用一些经过身份验证的请求时,它返回401(未授权)。

我如何实现它通用?或者我总是需要用@header调用我的身份验证请求?

@PUT("usuario/{userId}/")
Call<User> putUserById(@Path("userId") Integer id, @Body User user);

当我通过电子邮件调用Get User时(我对用户进行身份验证)...

public class NetworkService {
private NetworkAPI networkAPI;
private OkHttpClient okHttpClient;

public NetworkService() {
    okHttpClient = buildClient();
    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(BuildConfig.HOST)
            .addConverterFactory(GsonConverterFactory.create())
            .client(okHttpClient)
            .build();

    networkAPI = retrofit.create(NetworkAPI.class);
}

public NetworkAPI getAPI() {
    return networkAPI;
}

private OkHttpClient buildClient() {

    OkHttpClient.Builder builder = new OkHttpClient.Builder();

    builder.addInterceptor(new Interceptor() {
        @Override
        public Response intercept(Chain chain) throws IOException {
            Response response = chain.proceed(chain.request());

            return response;
        }
    });

    builder.addInterceptor(new Interceptor() {
        @Override
        public Response intercept(Chain chain) throws IOException {
            //this is where we will add whatever we want to our request headers.
            Request request = chain.request().newBuilder()
                    .addHeader("Accept", "application/json")
                    .addHeader("Content-Type", "application/json")
                    .build();
            return chain.proceed(request);
        }
    });

    return builder.build();
}
}

当我调用Put用户时(我需要进行经过身份验证的请求)。

我的改装课......

DROP TABLE IF EXISTS my_table;

CREATE TABLE my_table
(booking_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
,guest VARCHAR(12) NOT NULL
,arrive DATE NOT NULL
,depart DATE NOT NULL
,booked INT NOT NULL
,UNIQUE KEY(guest,arrive)
);

INSERT INTO my_table (guest,arrive,depart,booked) VALUES
('Smith','2002-06-11','2002-06-18',1),
('Jones','2002-06-12','2002-06-14',2),
('Brown','2002-06-13','2002-06-16',1),
('White','2002-06-15','2002-06-17',2);

SELECT x.dt
     , 9 - COALESCE(SUM(booked),0) available 
  FROM calendar x 
  LEFT 
  JOIN my_table y 
    ON x.dt >= y.arrive AND x.dt < y.depart 
 WHERE x.dt BETWEEN '2002-06-10' AND '2002-06-20' 
 GROUP 
    BY dt;
+------------+-----------+
| dt         | available |
+------------+-----------+
| 2002-06-10 |         9 |
| 2002-06-11 |         8 |
| 2002-06-12 |         6 |
| 2002-06-13 |         5 |
| 2002-06-14 |         7 |
| 2002-06-15 |         5 |
| 2002-06-16 |         6 |
| 2002-06-17 |         8 |
| 2002-06-18 |         9 |
| 2002-06-19 |         9 |
| 2002-06-20 |         9 |
+------------+-----------+
11 rows in set (0.00 sec)

有人可以帮助我吗?

2 个答案:

答案 0 :(得分:1)

使用@Header(&#34;授权&#34;)NetworkService中的字符串授权

 Request request = chain.request().newBuilder()
                    .addHeader("Accept", "application/json")
                    .addHeader("Content-Type", "application/json")
                    .addHeader("Authorization", "AuthorizationValue")
                    .build();

答案 1 :(得分:-1)

如果您使用Retrofit 2,这里是标题

中基本身份验证的示例
public interface SomeApi {
@Headers({
   "Content-Type: application/json",
   "Authorization: Basic KzY1OTY0MjA4NTQ6MTExMTE="
})
@POST("test/someService")
Completable getTrips();

}