在POST请求中,拦截器卡在它的创建上

时间:2017-10-30 11:50:29

标签: android web-services http-post retrofit2 interceptor

我有一个应用程序需要为我的服务器发送一个POST请求,通过JSON发送一个项目列表。但要执行此POST,它需要带有令牌身份验证的标头配置。当我从应用程序发送请求时,在logcat上,可能会看到拦截器实例化被卡住了。 请看一下我的代码和logcat调试:

public static Retrofit getServerClient(String token) {
    Log.d("DEBUG 1 -> ","getServerClient");
    OkHttpClient httpClient;
    Gson gson = new GsonBuilder()
            .setDateFormat("yyyy-MM-dd'T'HH:mm:ss")
            .create();
    Retrofit.Builder builder = new Retrofit.Builder()
            .baseUrl(SERVER_BASE_URL)
            .addConverterFactory(GsonConverterFactory.create(gson));
    if (token!=null && !TextUtils.isEmpty(token)) {
        Log.d("DEBUG 2 -> ","getServerClient needs add header");
        httpClient = getHttpClientWithInterceptor(token);
        Log.d("DEBUG 8 -> ","getServerClient with headers");
    }else {
        httpClient = getOkHttpClient();
    }
    builder.client(httpClient);
    Log.d("DEBUG 9 -> ","Return server client");
    return builder.build();
}

private static OkHttpClient getHttpClientWithInterceptor(final String token) {
    Log.d("DEBUG 3 -> ","Instantiate interceptor");
    Interceptor interceptor = new Interceptor() {
        @Override
        public Response intercept(Chain chain) {
            try {
                Log.d("DEBUG 4 -> ","Add headers START");
                final Request request = chain.request().newBuilder()
                        .addHeader("Authorization", "Bearer "+token)
                        .addHeader("Content-Type", "application/json")
                        .build();
                Log.d("DEBUG 5 -> ","Add headers END");
                return chain.proceed(request);
            }catch (Exception e){
                Log.d("DEBUG 6 -> ","Failure on request build");
                e.printStackTrace();
                return null;
            }
        }
    };

    OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
    httpClient.addNetworkInterceptor(interceptor);
    Log.d("DEBUG 7 -> ","Return client with interceptor");
    return httpClient.build();
}

GET请求中有什么奇怪的,它可以看到:

    10-30 09:40:16.081 8379-8379/br.com.vegait.android.hotelexperience D/DEBUG 1 ->: getServerClient
    10-30 09:40:16.097 8379-8379/br.com.vegait.android.hotelexperience D/DEBUG 2 ->: getServerClient needs add header
    10-30 09:40:16.097 8379-8379/br.com.vegait.android.hotelexperience D/DEBUG 3 ->: Instantiate interceptor
    10-30 09:40:16.103 8379-8379/br.com.vegait.android.hotelexperience D/DEBUG 7 ->: Return client with interceptor
    10-30 09:40:16.104 8379-8379/br.com.vegait.android.hotelexperience D/DEBUG 8 ->: getServerClient with headers
    10-30 09:40:16.104 8379-8379/br.com.vegait.android.hotelexperience D/DEBUG 9 ->: Return server client
    10-30 09:40:16.193 8379-8425/br.com.vegait.android.hotelexperience D/EGL_emulation: eglMakeCurrent: 0xa39a67a0: ver 2 0 (tinfo 0xa39ef300)
    10-30 09:40:16.256 8379-8455/br.com.vegait.android.hotelexperience D/DEBUG 4 ->: Add headers START
    10-30 09:40:16.256 8379-8455/br.com.vegait.android.hotelexperience D/DEBUG 5 ->: Add headers END

但是在POST上它会在循环中显示以下日志:

        10-30 09:45:56.882 12054-12054/br.com.vegait.android.hotelexperience D/DEBUG 1 ->: getServerClient
10-30 09:45:56.897 12054-12054/br.com.vegait.android.hotelexperience D/DEBUG 2 ->: getServerClient needs add header
10-30 09:45:56.897 12054-12054/br.com.vegait.android.hotelexperience D/DEBUG 3 ->: Instantiate interceptor
10-30 09:45:56.901 12054-12054/br.com.vegait.android.hotelexperience D/DEBUG 7 ->: Return client with interceptor
10-30 09:45:56.902 12054-12054/br.com.vegait.android.hotelexperience D/DEBUG 8 ->: getServerClient with headers
10-30 09:45:56.902 12054-12054/br.com.vegait.android.hotelexperience D/DEBUG 9 ->: Return server client
10-30 09:45:56.920 12054-12059/br.com.vegait.android.hotelexperience I/art: Do partial code cache collection, code=30KB, data=26KB
10-30 09:45:56.921 12054-12059/br.com.vegait.android.hotelexperience I/art: After code cache collection, code=29KB, data=26KB
10-30 09:45:56.921 12054-12059/br.com.vegait.android.hotelexperience I/art: Increasing code cache capacity to 128KB
10-30 09:45:56.994 12054-12061/br.com.vegait.android.hotelexperience W/art: Suspending all threads took: 7.348ms
10-30 09:45:57.005 12054-12065/br.com.vegait.android.hotelexperience W/art: Suspending all threads took: 9.870ms
10-30 09:45:57.412 12054-12065/br.com.vegait.android.hotelexperience I/art: Background sticky concurrent mark sweep GC freed 47547(884KB) AllocSpace objects, 6(120KB) LOS objects, 3% free, 18MB/19MB, paused 5.274ms total 17.663ms
10-30 09:45:57.928 12054-12065/br.com.vegait.android.hotelexperience W/art: Suspending all threads took: 5.320ms
10-30 09:45:57.934 12054-12065/br.com.vegait.android.hotelexperience I/art: Background sticky concurrent mark sweep GC freed 175307(3MB) AllocSpace objects, 22(576KB) LOS objects, 16% free, 15MB/18MB, paused 6.735ms total 28.375ms
10-30 09:45:58.380 12054-12065/br.com.vegait.android.hotelexperience W/art: Suspending all threads took: 7.855ms
10-30 09:45:58.388 12054-12065/br.com.vegait.android.hotelexperience I/art: Background partial concurrent mark sweep GC freed 149517(2MB) AllocSpace objects, 18(572KB) LOS objects, 18% free, 17MB/21MB, paused 9.750ms total 35.467ms
10-30 09:45:58.493 12054-12061/br.com.vegait.android.hotelexperience W/art: Suspending all threads took: 7.182ms
10-30 09:45:58.555 12054-12065/br.com.vegait.android.hotelexperience W/art: Suspending all threads took: 7.622ms
10-30 09:45:58.563 12054-12065/br.com.vegait.android.hotelexperience I/art: Background sticky concurrent mark sweep GC freed 180086(3MB) AllocSpace objects, 17(548KB) LOS objects, 14% free, 18MB/21MB, paused 9.906ms total 36.522ms
10-30 09:45:58.569 12054-12065/br.com.vegait.android.hotelexperience W/art: Suspending all threads took: 6.073ms
10-30 09:45:58.718 12054-12065/br.com.vegait.android.hotelexperience W/art: Suspending all threads took: 8.127ms
10-30 09:45:58.726 12054-12065/br.com.vegait.android.hotelexperience I/art: Background partial concurrent mark sweep GC freed 151954(2MB) AllocSpace objects, 17(604KB) LOS objects, 17% free, 19MB/23MB, paused 9.878ms total 43.588ms
10-30 09:45:58.732 12054-12065/br.com.vegait.android.hotelexperience W/art: Suspending all threads took: 5.808ms
10-30 09:45:58.995 12054-12061/br.com.vegait.android.hotelexperience W/art: Suspending all threads took: 7.158ms
10-30 09:45:59.277 12054-12065/br.com.vegait.android.hotelexperience W/art: Suspending all threads took: 5.132ms
10-30 09:45:59.640 12054-12065/br.com.vegait.android.hotelexperience W/art: Suspending all threads took: 6.369ms
10-30 09:45:59.981 12054-12065/br.com.vegait.android.hotelexperience W/art: Suspending all threads took: 11.514ms
10-30 09:45:59.994 12054-12065/br.com.vegait.android.hotelexperience I/art: Background sticky concurrent mark sweep GC freed 186259(3MB) AllocSpace objects, 13(572KB) LOS objects, 10% free, 24MB/27MB, paused 14.243ms total 54.366ms
10-30 09:46:00.003 12054-12065/br.com.vegait.android.hotelexperience W/art: Suspending all threads took: 9.053ms
10-30 09:46:00.190 12054-12065/br.com.vegait.android.hotelexperience W/art: Suspending all threads took: 6.135ms
10-30 09:46:00.397 12054-12065/br.com.vegait.android.hotelexperience W/art: Suspending all threads took: 8.317ms
10-30 09:46:00.508 12054-12061/br.com.vegait.android.hotelexperience W/art: Suspending all threads took: 6.196ms
10-30 09:46:00.576 12054-12065/br.com.vegait.android.hotelexperience W/art: Suspending all threads took: 5.746ms
10-30 09:46:00.763 12054-12065/br.com.vegait.android.hotelexperience W/art: Suspending all threads took: 7.054ms
10-30 09:46:00.936 12054-12065/br.com.vegait.android.hotelexperience W/art: Suspending all threads took: 6.152ms
10-30 09:46:01.014 12054-12061/br.com.vegait.android.hotelexperience W/art: Suspending all threads took: 9.358ms
10-30 09:46:01.207 12054-12065/br.com.vegait.android.hotelexperience I/art: Background sticky concurrent mark sweep GC freed 173843(3MB) AllocSpace objects, 11(572KB) LOS objects, 10% free, 29MB/32MB, paused 3.833ms total 120.445ms
10-30 09:46:01.499 12054-12065/br.com.vegait.android.hotelexperience I/art: Background partial concurrent mark sweep GC freed 176026(3MB) AllocSpace objects, 13(676KB) LOS objects, 11% free, 29MB/33MB, paused 7.948ms total 158.936ms
10-30 09:46:01.513 12054-12065/br.com.vegait.android.hotelexperience W/art: Suspending all threads took: 14.086ms
10-30 09:46:01.768 12054-12065/br.com.vegait.android.hotelexperience I/art: Background sticky concurrent mark sweep GC freed 183401(3MB) AllocSpace objects, 10(540KB) LOS objects, 8% free, 30MB/33MB, paused 6.976ms total 55.042ms
10-30 09:46:01.782 12054-12065/br.com.vegait.android.hotelexperience W/art: Suspending all threads took: 14.141ms
10-30 09:46:02.091 12054-12061/br.com.vegait.android.hotelexperience W/art: Suspending all threads took: 83.993ms
10-30 09:46:02.119 12054-12065/br.com.vegait.android.hotelexperience I/art: Background partial concurrent mark sweep GC freed 146812(2MB) AllocSpace objects, 10(556KB) LOS objects, 11% free, 31MB/35MB, paused 4.086ms total 148.374ms
10-30 09:46:02.132 12054-12065/br.com.vegait.android.hotelexperience W/art: Suspending all threads took: 12.503ms
10-30 09:46:02.355 12054-12065/br.com.vegait.android.hotelexperience W/art: Suspending all threads took: 10.725ms
10-30 09:46:02.538 12054-12065/br.com.vegait.android.hotelexperience W/art: Suspending all threads took: 13.960ms
10-30 09:46:02.551 12054-12065/br.com.vegait.android.hotelexperience I/art: Background sticky concurrent mark sweep GC freed 179103(3MB) AllocSpace objects, 8(448KB) LOS objects, 5% free, 33MB/35MB, paused 16.906ms total 86.519ms
10-30 09:46:02.561 12054-12065/br.com.vegait.android.hotelexperience W/art: Suspending all threads took: 9.621ms
10-30 09:46:02.713 12054-12065/br.com.vegait.android.hotelexperience W/art: Suspending all threads took: 13.718ms
10-30 09:46:02.730 12054-12065/br.com.vegait.android.hotelexperience I/art: Background partial concurrent mark sweep GC freed 176741(3MB) AllocSpace objects, 13(744KB) LOS objects, 10% free, 33MB/37MB, paused 17.026ms total 105.761ms
10-30 09:46:02.740 12054-12065/br.com.vegait.android.hotelexperience W/art: Suspending all threads took: 9.648ms
10-30 09:46:02.919 12054-12065/br.com.vegait.android.hotelexperience W/art: Suspending all threads took: 11.148ms
10-30 09:46:03.093 12054-12065/br.com.vegait.android.hotelexperience W/art: Suspending all threads took: 15.640ms
10-30 09:46:03.278 12054-12065/br.com.vegait.android.hotelexperience W/art: Suspending all threads took: 8.964ms
10-30 09:46:03.466 12054-12065/br.com.vegait.android.hotelexperience W/art: Suspending all threads took: 11.234ms
10-30 09:46:03.523 12054-12061/br.com.vegait.android.hotelexperience W/art: Suspending all threads took: 6.985ms
10-30 09:46:03.622 12054-12065/br.com.vegait.android.hotelexperience W/art: Suspending all threads took: 11.846ms
10-30 09:46:03.749 12054-12065/br.com.vegait.android.hotelexperience W/art: Suspending all threads took: 15.134ms
10-30 09:46:03.911 12054-12065/br.com.vegait.android.hotelexperience W/art: Suspending all threads took: 9.538ms
10-30 09:46:04.108 12054-12065/br.com.vegait.android.hotelexperience W/art: Suspending all threads took: 10.659ms
10-30 09:46:04.264 12054-12065/br.com.vegait.android.hotelexperience W/art: Suspending all threads took: 12.444ms
10-30 09:46:04.429 12054-12065/br.com.vegait.android.hotelexperience W/art: Suspending all threads took: 16.430ms
10-30 09:46:04.446 12054-12065/br.com.vegait.android.hotelexperience I/art: Background partial concurrent mark sweep GC freed 177011(3MB) AllocSpace objects, 13(860KB) LOS objects, 8% free, 40MB/44MB, paused 20.255ms total 121.688ms
10-30 09:46:04.461 12054-12065/br.com.vegait.android.hotelexperience W/art: Suspending all threads took: 15.013ms
10-30 09:46:04.530 12054-12061/br.com.vegait.android.hotelexperience W/art: Suspending all threads took: 6.298ms
10-30 09:46:04.670 12054-12065/br.com.vegait.android.hotelexperience W/art: Suspending all threads took: 11.041ms
10-30 09:46:04.839 12054-12065/br.com.vegait.android.hotelexperience W/art: Suspending all threads took: 12.578ms
10-30 09:46:05.030 12054-12065/br.com.vegait.android.hotelexperience W/art: Suspending all threads took: 7.919ms
10-30 09:46:05.242 12054-12065/br.com.vegait.android.hotelexperience W/art: Suspending all threads took: 13.381ms
10-30 09:46:05.400 12054-12065/br.com.vegait.android.hotelexperience W/art: Suspending all threads took: 14.841ms
10-30 09:46:05.537 12054-12065/br.com.vegait.android.hotelexperience W/art: Suspending all threads took: 14.684ms
10-30 09:46:05.735 12054-12065/br.com.vegait.android.hotelexperience W/art: Suspending all threads took: 15.828ms
10-30 09:46:05.755 12054-12065/br.com.vegait.android.hotelexperience I/art: Background partial concurrent mark sweep GC freed 191824(3MB) AllocSpace objects, 12(860KB) LOS objects, 8% free, 45MB/49MB, paused 19.760ms total 154.848ms
10-30 09:46:05.770 12054-12065/br.com.vegait.android.hotelexperience W/art: Suspending all threads took: 15.253ms
10-30 09:46:05.953 12054-12065/br.com.vegait.android.hotelexperience W/art: Suspending all threads took: 14.552ms
10-30 09:46:06.044 12054-12061/br.com.vegait.android.hotelexperience W/art: Suspending all threads took: 9.720ms
10-30 09:46:06.138 12054-12065/br.com.vegait.android.hotelexperience W/art: Suspending all threads took: 14.542ms
10-30 09:46:06.284 12054-12065/br.com.vegait.android.hotelexperience W/art: Suspending all threads took: 15.762ms
10-30 09:46:06.464 12054-12065/br.com.vegait.android.hotelexperience I/art: Background partial concurrent mark sweep GC freed 194534(3MB) AllocSpace objects, 13(968KB) LOS objects, 7% free, 48MB/52MB, paused 3.728ms total 127.593ms
10-30 09:46:06.476 12054-12065/br.com.vegait.android.hotelexperience W/art: Suspending all threads took: 11.657ms
10-30 09:46:06.550 12054-12061/br.com.vegait.android.hotelexperience W/art: Suspending all threads took: 13.106ms
10-30 09:46:06.664 12054-12065/br.com.vegait.android.hotelexperience W/art: Suspending all threads took: 13.684ms
10-30 09:46:06.821 12054-12065/br.com.vegait.android.hotelexperience W/art: Suspending all threads took: 14.076ms
10-30 09:46:07.000 12054-12065/br.com.vegait.android.hotelexperience W/art: Suspending all threads took: 16.330ms
10-30 09:46:07.016 12054-12065/br.com.vegait.android.hotelexperience I/art: Background partial concurrent mark sweep GC freed 202163(3MB) AllocSpace objects, 11(836KB) LOS objects, 7% free, 51MB/55MB, paused 19.939ms total 136.394ms
10-30 09:46:07.028 12054-12065/br.com.vegait.android.hotelexperience W/art: Suspending all threads took: 12.080ms
10-30 09:46:07.227 12054-12065/br.com.vegait.android.hotelexperience W/art: Suspending all threads took: 15.434ms
10-30 09:46:07.395 12054-12065/br.com.vegait.android.hotelexperience W/art: Suspending all threads took: 17.207ms
10-30 09:46:07.525 12054-12065/br.com.vegait.android.hotelexperience W/art: Suspending all threads took: 16.650ms
10-30 09:46:07.713 12054-12065/br.com.vegait.android.hotelexperience I/art: Background partial concurrent mark sweep GC freed 214425(3MB) AllocSpace objects, 12(948KB) LOS objects, 6% free, 53MB/57MB, paused 4.599ms total 122.695ms
10-30 09:46:07.725 12054-12065/br.com.vegait.android.hotelexperience W/art: Suspending all threads took: 12.268ms
10-30 09:46:07.928 12054-12065/br.com.vegait.android.hotelexperience W/art: Suspending all threads took: 16.880ms
10-30 09:46:08.080 12054-12065/br.com.vegait.android.hotelexperience W/art: Suspending all threads took: 20.240ms
10-30 09:46:08.094 12054-12065/br.com.vegait.android.hotelexperience I/art: Background sticky concurrent mark sweep GC freed 160556(2MB) AllocSpace objects, 5(400KB) LOS objects, 3% free, 55MB/57MB, paused 24.451ms total 71.926ms
10-30 09:46:08.113 12054-12065/br.com.vegait.android.hotelexperience W/art: Suspending all threads took: 18.642ms
10-30 09:46:08.305 12054-12065/br.com.vegait.android.hotelexperience I/art: Background partial concurrent mark sweep GC freed 212686(3MB) AllocSpace objects, 11(880KB) LOS objects, 6% free, 55MB/59MB, paused 5.110ms total 134.449ms
10-30 09:46:08.319 12054-12065/br.com.vegait.android.hotelexperience W/art: Suspending all threads took: 14.065ms
10-30 09:46:08.535 12054-12065/br.com.vegait.android.hotelexperience W/art: Suspending all threads took: 15.462ms
10-30 09:46:08.712 12054-12065/br.com.vegait.android.hotelexperience W/art: Suspending all threads took: 17.943ms
10-30 09:46:08.907 12054-12065/br.com.vegait.android.hotelexperience W/art: Suspending all threads took: 19.325ms
10-30 09:46:08.930 12054-12065/br.com.vegait.android.hotelexperience I/art: Background partial concurrent mark sweep GC freed 219521(3MB) AllocSpace objects, 12(1004KB) LOS objects, 6% free, 58MB/62MB, paused 24.278ms total 157.949ms
10-30 09:46:08.948 12054-12065/br.com.vegait.android.hotelexperience W/art: Suspending all threads took: 17.521ms
10-30 09:46:09.067 12054-12061/br.com.vegait.android.hotelexperience W/art: Suspending all threads took: 12.066ms
10-30 09:46:09.170 12054-12065/br.com.vegait.android.hotelexperience W/art: Suspending all threads took: 18.647ms
10-30 09:46:09.361 12054-12065/br.com.vegait.android.hotelexperience W/art: Suspending all threads took: 19.054ms
10-30 09:46:09.583 12054-12061/br.com.vegait.android.hotelexperience W/art: Suspending all threads took: 25.753ms
10-30 09:46:09.619 12054-12065/br.com.vegait.android.hotelexperience W/art: Suspending all threads took: 18.583ms
10-30 09:46:09.634 12054-12065/br.com.vegait.android.hotelexperience I/art: Background partial concurrent mark sweep GC freed 228847(4MB) AllocSpace objects, 11(924KB) LOS objects, 6% free, 61MB/65MB, paused 22.781ms total 182.292ms
10-30 09:46:09.645 12054-12065/br.com.vegait.android.hotelexperience W/art: Suspending all threads took: 11.615ms
10-30 09:46:09.922 12054-12065/br.com.vegait.android.hotelexperience I/art: Background sticky concurrent mark sweep GC freed 209342(3MB) AllocSpace objects, 6(528KB) LOS objects, 4% free, 62MB/65MB, paused 5.230ms total 98.012ms
10-30 09:46:09.943 12054-12065/br.com.vegait.android.hotelexperience W/art: Suspending all threads took: 21.805ms
10-30 09:46:10.175 12054-12061/br.com.vegait.android.hotelexperience W/art: Suspending all threads took: 114.591ms
10-30 09:46:10.206 12054-12065/br.com.vegait.android.hotelexperience I/art: Background partial concurrent mark sweep GC freed 157725(2MB) AllocSpace objects, 10(876KB) LOS objects, 6% free, 62MB/66MB, paused 4.308ms total 153.014ms
10-30 09:46:10.223 12054-12065/br.com.vegait.android.hotelexperience W/art: Suspending all threads took: 17.394ms
10-30 09:46:10.460 12054-12065/br.com.vegait.android.hotelexperience W/art: Suspending all threads took: 18.833ms
10-30 09:46:10.579 12054-12061/br.com.vegait.android.hotelexperience W/art: Suspending all threads took: 16.845ms
10-30 09:46:10.739 12054-12065/br.com.vegait.android.hotelexperience I/art: Background partial concurrent mark sweep GC freed 278135(4MB) AllocSpace objects, 11(968KB) LOS objects, 5% free, 64MB/68MB, paused 4.272ms total 179.332ms
10-30 09:46:10.752 12054-12065/br.com.vegait.android.hotelexperience W/art: Suspending all threads took: 12.981ms

并保持这样直到崩溃。我做错了什么?

2 个答案:

答案 0 :(得分:0)

又是我了。

只是留下关于我的问题的反馈。我发现了问题。

我根据请求发送的对象与映射为发送的对象不同。 Idk为什么它会卡住de interceptor,但是当我改变它的对象时,它就像一个魅力。我会把这个留给其他可能有同样问题的人。

谢谢你。

答案 1 :(得分:0)

首先,您必须添加崩溃日志。但无论如何,你提供的日志意味着你正在使用内存,然后它被GC释放但似乎你做了很多工作,GC无法提供你需要的内存和你的应用程序用OutOfMemory异常击落。所以首先在这种情况下你必须使用Singleton模式来避免创建所有这些对象(如HttpClient,Gson,Interceptor等)。所以让我们假装这个名为HttpClient的类。那么你需要像NetManager这样的单身人士......

public class NetManager {

    private static NetManager instance;
    private HttpClient httpClient;

    private NetManager() {
        httpClient = new HttpClient();
    }

    public static NetManager() {
        if (instance == null) {
            instance = new NetManager();
        }
        return instance;
    }

    public HttpClient getHttpClient() {
        return httpClient;
    }
}

通过此经理使用您的httpClient。

但总的来说,在循环中发出网络请求是非常非常糟糕的。