MockWebServer and Retrofit with Callback

后端 未结 3 1887
伪装坚强ぢ
伪装坚强ぢ 2021-02-05 20:04

I would like to simulate network communication by MockWebServer. Unfortulatelly retrofit callbacks are never invoking. My code:

    MockWebServer server = new M         


        
3条回答
  •  一个人的身影
    2021-02-05 20:49

    For retrofit 2 see the answer here: https://github.com/square/retrofit/issues/1259 You can supply the synchronous executor to an OkHttpClient (via its dispatcher) and set this client to the Retrofit.Builder. You can also set the same executor to the callbackExecutor.

    For example:

    CurrentThreadExecutor currentThreadExecutor = new CurrentThreadExecutor();
    okhttp3.Dispatcher dispatcher = new okhttp3.Dispatcher(currentThreadExecutor);
    OkHttpClient okHttpClient = new 
    OkHttpClient.Builder().dispatcher(dispatcher).build();
    
    new Retrofit.Builder()
            .client(okHttpClient)
            .baseUrl(httpUrl)
            .addConverterFactory(JacksonConverterFactory.create())
            .callbackExecutor(currentThreadExecutor)
            .build();
    

    Example of CurrentThreadExecutor implementation: https://gist.github.com/vladimir-bukhtoyarov/38d6b4b277d0a0cfb3af

提交回复
热议问题