Android 如何在2中使用卡盘
我想将ChuckInterceptor添加到okHttpClient。上下文有问题。如何将MainActivity上下文设置为chuckAndroid 如何在2中使用卡盘,android,android-studio,okhttp,Android,Android Studio,Okhttp,我想将ChuckInterceptor添加到okHttpClient。上下文有问题。如何将MainActivity上下文设置为chuck public Parser(){ OkHttpClient okHttpClient = new OkHttpClient.Builder().connectTimeout(1, TimeUnit.MINUTES) .readTimeout(60, TimeUnit.SECONDS)
public Parser(){
OkHttpClient okHttpClient = new OkHttpClient.Builder().connectTimeout(1, TimeUnit.MINUTES)
.readTimeout(60, TimeUnit.SECONDS)
.writeTimeout(60, TimeUnit.SECONDS)
.addInterceptor(new ChuckInterceptor(context))
.build();
mRetrofit = new Retrofit.Builder()
.baseUrl(API_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(okHttpClient)
.build();
}
下面是我如何调用解析器的
Parser.getInstance().getInfo().getUpdate(check).enqueue(new Callback<MobileCheck>() {
@Override
public void onResponse(Call<MobileCheck> call, retrofit2.Response<MobileCheck> response) {
mobileCheck = response.body();
mainPresenter.enterApplication(false, true,
mobileCheck.getResult().getUpdate(), mobileCheck.getResult().getUpgrade());
}
@Override
public void onFailure(Call<MobileCheck> call, Throwable t) {
Toast.makeText(MainActivity.this, "Error", Toast.LENGTH_SHORT).show();
}
});
Parser.getInstance().getInfo().getUpdate(检查).enqueue(新回调(){
@凌驾
公共void onResponse(呼叫,改装2.响应){
mobileCheck=response.body();
mainPresenter.enterApplication(false、true、,
mobileCheck.getResult().getUpdate(),mobileCheck.getResult().getUpgrade());
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
Toast.makeText(MainActivity.this,“Error”,Toast.LENGTH_SHORT.show();
}
});
首先,创建应用程序的单例实例
舱单申请中的第二个登记册
public class App extends Application {
// Create the instance
private static App instance;
public static App getInstance() {
if (instance == null) {
synchronized (App.class) {
if (instance == null)
instance = new App();
}
}
// Return the instance
return instance;
}
@Override
public void onCreate() {
super.onCreate();
}
}
<application
android:name=".App"
....
>
OkHttpClient okHttpClient = new OkHttpClient.Builder().
addInterceptor(new ChuckInterceptor(App.getInstance().getApplicationContext()))
.build();