Android Crashing With Retrofit static Instance

时间:2017-12-26 07:45:55

标签: android retrofit retrofit2

当我使用静态Retrofit对象时,应用程序会崩溃并显示以下日志 我不能在没有静态的情况下使用改装对象,因为我经常使用它。请让我知道相同的

的解决方法
W/WindowAnimator: Failed to dispatch window animation state change.
        android.os.DeadObjectException
at android.os.BinderProxy.transactNative(Native Method)
at android.os.BinderProxy.transact(Binder.java:513)
at android.view.IWindow$Stub$Proxy.onAnimationStopped(IWindow.java:548)
at com.android.server.wm.WindowAnimator.updateWindowsLocked(WindowAnimator.java:302)
at com.android.server.wm.WindowAnimator.animateLocked(WindowAnimator.java:694)
at com.android.server.wm.WindowAnimator.access$000(WindowAnimator.java:56)
at com.android.server.wm.WindowAnimator$1.doFrame(WindowAnimator.java:128)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:894)
at android.view.Choreographer.doCallbacks(Choreographer.java:698)
at android.view.Choreographer.doFrame(Choreographer.java:630)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:882)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:207)
at android.os.HandlerThread.run(HandlerThread.java:61)
at com.android.server.ServiceThread.run(ServiceThread.java:46)

有些时候我也得到这个错误

Fail to sendHttpRequest
                                              java.lang.IllegalArgumentException: HTTP entity may not be null
                                                  at org.apache.http.util.EntityUtils.toString(EntityUtils.java:115)
                                                  at org.apache.http.util.EntityUtils.toString(EntityUtils.java:151)
                                                  at miui.util.ErrorReport.c(SourceFile:396)
                                                  at miui.util.ErrorReport.sendReportRequest(SourceFile:353)
                                                  at miui.util.ErrorReport$1.a(SourceFile:369)
                                                  at miui.util.ErrorReport$1.doInBackground(SourceFile:366)
                                                  at android.os.AsyncTask$2.call(AsyncTask.java:295)
                                                  at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                                                  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
                                                  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
                                                  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
                                                  at java.lang.Thread.run(Thread.java:818)

这是改造对象,它正常工作

public static Retrofit getAPIClient(Context context) {
        return new Retrofit.Builder()
                .baseUrl(context.getString(R.string.base_url))
                .client(getOkHttpClient(context))
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create(getGson()))
                .build();
    }

但是当我把它转换为静态时它会崩溃

     if ( appApiClient == null ) {
                appApiClient = new Retrofit.Builder()
                        .baseUrl( context.getString( R.string.base_url ) )
                        .client( getOkHttpClient( context)
                        .addCallAdapterFactory( RxJavaCallAdapterFactory.create() )
                        .addConverterFactory( GsonConverterFactory.create( getGson() ) )
                        .build();
       }
      return appApiClient ;

1 个答案:

答案 0 :(得分:0)

public class RetroFitServiceGenerator {

    MySharedPreferences sharedPreferences;

    public static Retrofit.Builder builder =
            new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create());
    public static Retrofit retrofit = builder.build();
    public static HttpLoggingInterceptor logging = new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY);

    private static Context context;

    private OkHttpClient.Builder httpClient =
            new OkHttpClient.Builder();

    public RetroFitServiceGenerator(Context context) {
        this.context = context;
    }

    public <S> S serviceNOAuth(
            Class<S> serviceClass) {

        httpClient.addInterceptor(logging);
        builder.client(httpClient.build());
        retrofit = builder.build();
        return retrofit.create(serviceClass);
    }

}

现在您可以根据您的要求修改或添加服务,如缓存或身份验证

在您的班级中,您只需致电:

 RetrofitAPI service = new RetroFitServiceGenerator(context).serviceNOAuth(RetrofitAPI.class);