本文介绍了如何在翻新Android中捕获异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
- 我已经定义了以下类.
- 我在这里使用了带有匕首的翻新版
我要做什么 ::
我正在尝试在下面的请求中捕获OfflineException
,以了解如何在Main Activity中正确捕获它.
I am trying to catch the OfflineException
in the request below how to catch it in Main Activity properly.
RequestInterceptor.java
public class RequestInterceptor implements Interceptor {
ConnectivityManager connectivityManager;
Application mApplication;
@Inject
public RequestInterceptor(Application mApplication) {
this.mApplication = mApplication;
connectivityManager = (ConnectivityManager) mApplication.getSystemService(Context.CONNECTIVITY_SERVICE);
}
@Override
public Response intercept(Chain chain) throws IOException {
if (isConnected()) {
throw new OfflineException();
}
Request.Builder r = chain.request().newBuilder();
return chain.proceed(r.build());
}
protected boolean isConnected() {
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
return networkInfo != null && networkInfo.isConnectedOrConnecting();
}
public class OfflineException extends IOException {
@Override
public String getMessage() {
return mApplication.getResources().getString(R.string.app_no_connectivity);
}
}
}
NetModule.java
import android.app.Application;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.util.concurrent.TimeUnit;
import javax.inject.Singleton;
import commons.LinksAndKeys;
import dagger.Module;
import dagger.Provides;
import okhttp3.Cache;
import okhttp3.OkHttpClient;
import retrofitDagger.retrofitUtils.RequestInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
@Module
public class NetModule {
String mBaseUrl;
Application mApplication;
// Constructor needs one parameter to instantiate.
public NetModule(String baseUrl, Application application) {
this.mBaseUrl = baseUrl;
this.mApplication = application;
}
// Dagger will only look for methods annotated with @Provides
@Provides
@Singleton
// Application reference must come from AppModule.class
SharedPreferences providesSharedPreferences(Application application) {
return PreferenceManager.getDefaultSharedPreferences(application);
}
@Provides
@Singleton
Application providesApplication() {
return mApplication;
}
@Provides
@Singleton
Cache provideOkHttpCache(Application application) {
int cacheSize = 10 * 1024 * 1024; // 10 MiB
Cache cache = new Cache(application.getCacheDir(), cacheSize);
return cache;
}
@Provides
@Singleton
Gson provideGson() {
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES);
return gsonBuilder.create();
}
@Provides
@Singleton
OkHttpClient provideOkHttpClient(Cache cache) {
OkHttpClient.Builder client = new OkHttpClient.Builder();
client.cache(cache);
client.addInterceptor(new RequestInterceptor(mApplication));
client.readTimeout(LinksAndKeys.READ_TIMEOUT, TimeUnit.SECONDS);
client.connectTimeout(LinksAndKeys.CONNECTION_TIMEOUT, TimeUnit.SECONDS);
return client.build();
}
@Provides
@Singleton
Retrofit provideRetrofit(Gson gson, OkHttpClient okHttpClient) {
Retrofit retrofit = new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create(gson))
.baseUrl(mBaseUrl)
.client(okHttpClient)
.build();
return retrofit;
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Inject
Retrofit retrofit;
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
((App) getApplication()).getNetComponent().inject(this);
//Create textview and findViewByID
textView = (TextView) findViewById(R.id.textview_post);
//Create a retrofit call object
Call<List<Post>> posts = retrofit.create(Restapi.class).getPosts();
//Enque the call
posts.enqueue(new Callback<List<Post>>() {
@Override
public void onResponse(Call<List<Post>> call, Response<List<Post>> response) {
//Set the response to the textview
textView.setText(response.body().get(0).getBody());
}
@Override
public void onFailure(Call<List<Post>> call, Throwable t) {
//Set the error to the textview
textView.setText(t.toString());
}
});
}
}
推荐答案
据我所知,没有互联网连接,RetrofitError包含ConnectionException作为原因.
As I know, there is no internet connection the RetrofitError contains a ConnectionException as the cause.
public class RetrofitErrorHandler implements ErrorHandler {
@Override
public Throwable handleError(RetrofitError cause) {
if (cause.isNetworkError()) {
if (cause.getCause() instanceof SocketTimeoutException) {
/* your code here*/
return new MyConnectionTimeoutException();
} else {
/* your code here*/
return new MyNoConnectionException();
}
} else {
//Do whatever you want to do if there is not a network error.
}
}
}
或者您可以创建自定义的Retrofit客户端,该客户端在执行请求之前检查连接性并引发异常.
Or you can create a custom Retrofit client that checks for connectivity before executing a request and throws an exception.
public class ConnectivityCheck implements Client {
Logger log = LoggerFactory.getLogger(ConnectivityCheck.class);
public ConnectivityCheck (Client wrappedClient, NetworkConnectivityManager ncm) {
this.wrappedClient = wrappedClient;
this.ncm = ncm;
}
Client wrappedClient;
private NetworkConnectivityManager ncm;
@Override
public Response execute(Request request) throws IOException {
if (!ncm.isConnected()) {
log.debug("No connectivity %s ", request);
throw new NoConnectivityException("No connectivity");
}
return wrappedClient.execute(request);
}
}
,然后在配置RestAdapter时使用它
and then use it when configuring RestAdapter
RestAdapter.Builder().setEndpoint(serverHost)
.setClient(new ConnectivityCheck(new OkHttpClient(), ...))
这篇关于如何在翻新Android中捕获异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!