我一直想採取匕首2共軛與改造2.一切似乎除了GET
要求很好地工作;他們似乎沒有附加任何標題。不能得到改造2用匕首發送全局套頭2
下面是我NetworkModule
提供所有與網絡有關的依賴關係爲整個應用程序(注意灑在那裏@ForApplication
範圍註釋):
@Module
public class NetworkModule {
// …
@ForApplication
@Provides
OkHttpClient provideOkHttp(
HttpLoggingInterceptor loggingInterceptor,
@Named(PREFERENCE_CUR_LOGIN_SESSION) Preference<LoginSession> loginSessionPreference,
DeviceCredentials deviceCredentials
) {
final OkHttpClient.Builder builder = new OkHttpClient().newBuilder();
builder.addNetworkInterceptor(chain -> {
if (loginSessionPreference.isSet()) {
return chain.proceed(
chain.request().newBuilder()
.addHeader("token", loginSessionPreference.get().getTokenId())
.addHeader("device-id", deviceCredentials.getDeviceId())
.addHeader("Content-Type", "application/json")
.build()
);
} else {
return chain.proceed(
chain.request().newBuilder().build()
);
}
});
return builder.build();
}
@ForApplication
@Provides
Retrofit provideRetrofit(Gson gson, OkHttpClient client) {
return new Retrofit.Builder()
.baseUrl("http://xxx.xxx.xxx/api/1.0/")
.client(client)
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
}
@ForApplication
@Provides
XxxApi provideApi(Retrofit retrofit) {
return retrofit.create(XxxApi.class);
}
}
該模塊作爲依賴提供給我ApplicationComponent
(其他模塊中):
@ForApplication
@Component(
modules = {
ApplicationModule.class,
RuntimeModule.class,
DateFormatModule.class,
PreferenceModule.class,
NetworkModule.class
}
)
public interface ApplicationComponent {
// …
}
我已經跑了調試會話,並確認loginSessionPreference.isSet()
作爲true
評估但儘管如此我仍請求出現了無線沒有任何標題:
11-16 16:55:22.748 21747-22569/xxx.xxx.xxx D/OkHttp: --> GET http://xxx.xxx.xxx/api/1.0/public/get-all-data/site http/1.1
11-16 16:55:22.748 21747-22569/xxx.xxx.xxx D/OkHttp: --> END GET
我錯過了什麼嗎?
從這裏看看你更好理解https://github.com/saveendhiman/SampleApp/blob/master/app/src/main/java/com/sampleapp/api/NetModule.java – Saveen
@Raghunandan,isn' t addNetworkInterceptor'已經做了什麼?那麼,不完全是這個方法,而是傳入的'Interceptor'。 –
我現在看到了。抱歉! – Raghunandan