在android改造中检查网络状态时收到错误

时间:2019-03-08 13:27:53

标签: java android retrofit retrofit2

我正在尝试使用改型存储preloaded响应的缓存来检查网络状态以管理应用程序中的脱机支持。这是我的代码

 public static Interceptor provideOfflineCacheInterceptor ()
    {
        return new Interceptor()
        {
            @Override
            public Response intercept (Interceptor.Chain chain) throws IOException
            {
                Request request = chain.request();

                if ( !AdeptAndroid.hasNetwork() )
                {
                    CacheControl cacheControl = new CacheControl.Builder()
                            .maxStale( 7, TimeUnit.DAYS )
                            .build();

                    request = request.newBuilder()
                            .cacheControl( cacheControl )
                            .build();
                }

                return chain.proceed( request );
            }
        };
    }

在这部分中,我正在检查网络状态,在这里我得到了空指针异常。

 if ( !AdeptAndroid.hasNetwork() )

这是AdepAndroid

public class AdeptAndroid extends Application {

    private static AdeptAndroid instance;

    @Override
    public void onCreate()
    {
        super.onCreate();

        instance = this;

        if (BuildConfig.DEBUG)
        {
            Timber.plant(new Timber.DebugTree());
        }

        Timber.i("Creating our Application");
    }

    public static AdeptAndroid getInstance ()
    {
        return instance;
    }

    public static boolean hasNetwork ()
        {
            return instance.checkIfHasNetwork();
    }

    public boolean checkIfHasNetwork()
    {
        ConnectivityManager connectivityManager
                = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
        return activeNetworkInfo != null && activeNetworkInfo.isConnected();
    }
}

在该课程中,我只是在检查网络状态,并在此处出现错误,如nullpointerException

 public static boolean hasNetwork ()
            {
                return instance.checkIfHasNetwork();
        }

请帮助解决此问题。谢谢

1 个答案:

答案 0 :(得分:0)

您是说AdeptAndroid.getInstance().hasNetwork()吗?

似乎您在调用类本身,而不是实例。因此,调用实例。


Update2:您的代码需要像这样修改。

public static Interceptor provideOfflineCacheInterceptor ()
{
    return new Interceptor()
    {
        @Override
        public Response intercept (Interceptor.Chain chain) throws IOException
        {
            Request request = chain.request();

            // You need context at this point. Your Application can be called by getApplicationContext(), not by custom getInstance().
            if ( !((AdeptAndroid)context.getApplicationContext()).hasNetwork() ) 
            {
                CacheControl cacheControl = new CacheControl.Builder()
                        .maxStale( 7, TimeUnit.DAYS )
                        .build();

                request = request.newBuilder()
                        .cacheControl( cacheControl )
                        .build();
            }

            return chain.proceed( request );
        }
    };
}

您的应用程序应如下所示:

public class AdeptAndroid extends Application {

    @Override
    public void onCreate() {
        super.onCreate();

        if (BuildConfig.DEBUG) {
            Timber.plant(new Timber.DebugTree());
        }

        Timber.i("Creating our Application");
    }

    public boolean hasNetwork () {
        return checkIfHasNetwork();
    }

    public boolean checkIfHasNetwork() {
        ConnectivityManager connectivityManager
            = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
        return activeNetworkInfo != null && activeNetworkInfo.isConnected();
    }
}