我正在尝试使用Retrofit2执行@GET请求。我只是有一个很好的请求让令牌登录,但现在当我试图这个电话时我没有得到任何东西。
这是拦截器显示的错误:
╭--- cURL (http://192.168.1.107/project-task)
curl -X GET -H "Host: 192.168.1.107" -H "Connection: Keep-Alive" -H "Accept-Encoding: gzip" -H "User-Agent: okhttp/3.4.1" --compressed http://192.168.1.107/project-task
╰--- (copy and paste the above line to a terminal)
--> GET http://192.168.1.107/project-task http/1.1
Host: 192.168.1.107
Connection: Keep-Alive
Accept-Encoding: gzip
User-Agent: okhttp/3.4.1
--> END GET
<-- 401 Unauthorized http://192.168.1.107/project-task (167ms)
X-Powered-By: Sails <sailsjs.org>
WWW-Authenticate: Bearer realm="Users"
set-cookie: sails.sid=s%3Aw3NbP_fcx-rnmnDaJSIUcs_ZgQB5ar5B.dZogKwU4nOlmmplnjqMZAUbL4eshjLITmBpkiZLdNkU; Path=/; HttpOnly
Date: Mon, 05 Sep 2016 11:25:09 GMT
Connection: keep-alive
Transfer-Encoding: chunked
<-- END HTTP
我使用GET进行的调用是:
@GET("project-task")
Call<RootContainer> getChargeAccountsNTasks(@Header("Authorization") String token);
与此一起使用的实现是下一个代码,但崩溃了调用对象,我认为这是因为令牌没有添加到标题中。
public class DataApiRequestImpl implements DataApiRequest {
private GetDataRequestEntity getDataRequestEntity;
private RetrofitApi retrofitApi;
@Inject
public DataApiRequestImpl(
GetDataRequestEntity getDataRequestEntity,
RetrofitApi retrofitApi) {
this.getDataRequestEntity = getDataRequestEntity;
this.retrofitApi = retrofitApi;
}
@Override
public RootContainer getChargeAccountsNTasks(String token) throws Exception {
RootContainer data = null;
Call<RootContainer> call = retrofitApi.getChargeAccountsNTasks(token);
Response<RootContainer> response = call.execute();
data = response.body();
return data;
}
}
我怎么知道令牌是否被添加到标题中?
编辑: 添加了UserModule,它具有执行调用的初始化方法。
@Module
public class UserModule {
public UserModule() {}
@Provides
public Retrofit retrofit() {
OkHttpClient.Builder okHttpClientBuilder = new OkHttpClient.Builder();
if (BuildConfig.DEBUG) {
CurlLoggingInterceptor curlLoggingInterceptor = new CurlLoggingInterceptor();
okHttpClientBuilder.addNetworkInterceptor(curlLoggingInterceptor);
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
interceptor.setLevel(HttpLoggingInterceptor.Level.HEADERS);
okHttpClientBuilder.addNetworkInterceptor(interceptor);
}
return new Retrofit.Builder()
.baseUrl(BuildConfig.API_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(okHttpClientBuilder.build())
.build();
}
@Provides
public RetrofitApi userRetrofitApi(Retrofit retrofit) {
return retrofit.create(RetrofitApi.class);
}
@Provides
@PerActivity
LoginUser provideLoginUserInteractor(LoginUserInteractor interactor) {
return interactor;
}
@Provides
@PerActivity
LogoutUser provideLogoutUserInteractor(LogoutUserInteractor interactor) {
return interactor;
}
@Provides
public LoginRequestEntity loginRequestEntity() {
return new LoginRequestEntity();
}
}
答案 0 :(得分:2)
您将授权作为参数传递而非Header。我将向您展示我在使用OkHttp之前所做的示例代码:
OkHttpClient httpClient = getOkHttpClient();
httpClient.interceptors().clear();
httpClient.interceptors().add(new Interceptor() {
@Override
public Response intercept(Interceptor.Chain chain) throws IOException {
Request original = chain.request();
// Set authorization token here
Request.Builder requestBuilder = original.newBuilder()
.header("Authorization", token)
.header("Connection", "close")
.method(original.method(), original.body());
Request request = requestBuilder.build();
return chain.proceed(request);
}
});
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.client(httpClient)
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
// Assign retorfitAPI like this
RetrofitApi retrofitApi = retrofit.create(serviceClass);
然后调用API。