I am using Retrofit on Android.
I define a service GitHubService
.
public interface GithubService {
@GET("users/{user}")
Call<ResponseBody> fetchUserInfo(@Path("user") String user);
}
Then I create service.
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://api.github.com")
.build();
GithubService service = retrofit.create(GithubService.class);
Call<ResponseBody> call = service.fetchUserInfo("coxier");
call.enqueue(...);
As you see, I use above code to get user info of github. And above code works well for me. Now I want to know how Retofit works so I read source code.The code below is Retrofit#create
. After reading, I still don't know its magic then I decide to debug Retroft.
public <T> T create(final Class<T> service) {
...
return (T) Proxy.newProxyInstance(...,
new InvocationHandler() {
...
@Override public Object invoke(..)
throws Throwable {
if (method.getDeclaringClass() == Object.class) {
return method.invoke(this, args);
}
...
return loadServiceMethod(method).invoke(args != null ? args : emptyArgs);
}
});
}
I debug at if(method.getDeclaringClass() == Object.class)
line ,however it crashes. I don't know why it crashes.
I tested several devices.
It may crash above Android 8.0.
Since the crashes occur with Android 8 and higher, I suppose it has to do with the restrictions of that versions in regard of apps running in background.
I suppose, that due to your debugging efforts, the app stays active in background for too long, causing the system to eventually kill it.
However, there should be something in the Logcat about that. Perhaps you see it, if you remove any filters from the Logcat window.
Now I want to know how Retofit works so I read source code.
Is this what You were trying to achieve? By adding interceptor You will be able see the requests in logcat.
private RestApiStack() {
OkHttpClient.Builder builder = new OkHttpClient.Builder();
// preview requests
if (BuildConfig.DEBUG) {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
builder.addInterceptor(interceptor);
}
OkHttpClient client = builder.build();
Retrofit retrofit = (new Retrofit.Builder())
.baseUrl(BASE_URL)
.client(client)
.addConverterFactory(/*...*/)
.build();
api = retrofit.create(ApiStack.class);
}
Dependencies:
implementation "com.squareup.okhttp3:logging-interceptor:3.9.1"
implementation "com.squareup.okhttp3:okhttp:3.11.0"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With