Skip to content

Instantly share code, notes, and snippets.

@ndorigatti
Created November 22, 2015 09:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ndorigatti/1ae58b33bfd52a24746e to your computer and use it in GitHub Desktop.
Save ndorigatti/1ae58b33bfd52a24746e to your computer and use it in GitHub Desktop.
Test for Retrofit 2.0.0 async call and thread names
private void doRetrofitRequest() {
mRefreshLayout.post(new Runnable() {
@Override
public void run() {
mRefreshLayout.setRefreshing(true);
}
});
Log.i(TAG, "Check thread name before callback:" + Thread.currentThread().getName());
OkHttpClient _okClient = new OkHttpClient();
_okClient.interceptors().add(new AddCookiesInterceptor());
Retrofit retrofit = new Retrofit.Builder().client(_okClient)
.addConverterFactory(SimpleXmlConverterFactory.create())
.baseUrl(Util.GAZZETTA_RETROFIT_ROOT)
.build();
MockRetrofit mockRetrofit = new MockRetrofit(NetworkBehavior.create(), new CallBehaviorAdapter(retrofit, Executors.newSingleThreadExecutor()));
MockGazzetta mockGazzettaService = new MockGazzetta(getApplicationContext());
GazzettaRetrofitService service = mockRetrofit.create(GazzettaRetrofitService.class, mockGazzettaService);
Call<Publication> publications = service.getPublications();
publications.enqueue(new Callback<Publication>() {
@Override
public void onResponse(Response<Publication> response, Retrofit retrofit) {
try {
//get the rsponse object
dailies = response.body().getIssues();//used in the adapter!
Log.i(TAG, "Check loopers=" + (Looper.getMainLooper() == Looper.myLooper()));
Log.i(TAG, "Check thread name in callback:" + Thread.currentThread().getName());
updateList();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onFailure(Throwable t) {
Toast.makeText(getApplicationContext(), "Failure calling service!", Toast.LENGTH_SHORT).show();//TODO put a snackbar!
}
});
}
public void updateList() {
//set the adapter to refresh
mRVAdapter.notifyItemRangeInserted(0, dailies.size());
//stop the refresh layout to show
mRefreshLayout.setRefreshing(false);
}
Log Result:
11-22 10:10:16.006 30180-30180/it.dorigatti.gazzetta I/GAZZA: Check thread name before callback:main
11-22 10:10:18.422 30180-30224/it.dorigatti.gazzetta I/GAZZA: Check loopers=false
11-22 10:10:18.422 30180-30224/it.dorigatti.gazzetta I/GAZZA: Check thread name in callback:pool-1-thread-1
11-22 10:10:18.426 30180-30224/it.dorigatti.gazzetta W/System.err: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
11-22 10:10:18.427 30180-30224/it.dorigatti.gazzetta W/System.err: at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6357)
11-22 10:10:18.428 30180-30224/it.dorigatti.gazzetta W/System.err: at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:909)
11-22 10:10:18.428 30180-30224/it.dorigatti.gazzetta W/System.err: at android.view.ViewGroup.invalidateChild(ViewGroup.java:4690)
11-22 10:10:18.428 30180-30224/it.dorigatti.gazzetta W/System.err: at android.view.View.invalidateInternal(View.java:11801)
11-22 10:10:18.428 30180-30224/it.dorigatti.gazzetta W/System.err: at android.view.View.invalidate(View.java:11765)
11-22 10:10:18.428 30180-30224/it.dorigatti.gazzetta W/System.err: at android.view.View.invalidateParentIfNeeded(View.java:11954)
11-22 10:10:18.429 30180-30224/it.dorigatti.gazzetta W/System.err: at android.view.View.clearAnimation(View.java:17870)
11-22 10:10:18.429 30180-30224/it.dorigatti.gazzetta W/System.err: at android.support.v4.widget.SwipeRefreshLayout.startScaleDownAnimation(SwipeRefreshLayout.java:416)
11-22 10:10:18.429 30180-30224/it.dorigatti.gazzetta W/System.err: at android.support.v4.widget.SwipeRefreshLayout.setRefreshing(SwipeRefreshLayout.java:402)
11-22 10:10:18.429 30180-30224/it.dorigatti.gazzetta W/System.err: at android.support.v4.widget.SwipeRefreshLayout.setRefreshing(SwipeRefreshLayout.java:355)
11-22 10:10:18.429 30180-30224/it.dorigatti.gazzetta W/System.err: at it.dorigatti.gazzetta.MainActivity.updateList(MainActivity.java:219)
11-22 10:10:18.429 30180-30224/it.dorigatti.gazzetta W/System.err: at it.dorigatti.gazzetta.MainActivity$6.onResponse(MainActivity.java:288)
11-22 10:10:18.430 30180-30224/it.dorigatti.gazzetta W/System.err: at retrofit.mock.BehaviorCall$2$1.run(BehaviorCall.java:88)
11-22 10:10:18.430 30180-30224/it.dorigatti.gazzetta W/System.err: at retrofit.mock.BehaviorCall$1.execute(BehaviorCall.java:54)
11-22 10:10:18.430 30180-30224/it.dorigatti.gazzetta W/System.err: at retrofit.mock.BehaviorCall$2.callResponse(BehaviorCall.java:86)
11-22 10:10:18.430 30180-30224/it.dorigatti.gazzetta W/System.err: at retrofit.mock.BehaviorCall$2.access$500(BehaviorCall.java:71)
11-22 10:10:18.430 30180-30224/it.dorigatti.gazzetta W/System.err: at retrofit.mock.BehaviorCall$2$3.onResponse(BehaviorCall.java:112)
11-22 10:10:18.430 30180-30224/it.dorigatti.gazzetta W/System.err: at retrofit.mock.Calls$1.enqueue(Calls.java:37)
11-22 10:10:18.431 30180-30224/it.dorigatti.gazzetta W/System.err: at retrofit.mock.BehaviorCall$2.run(BehaviorCall.java:109)
11-22 10:10:18.431 30180-30224/it.dorigatti.gazzetta W/System.err: at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
11-22 10:10:18.431 30180-30224/it.dorigatti.gazzetta W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
11-22 10:10:18.431 30180-30224/it.dorigatti.gazzetta W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
11-22 10:10:18.431 30180-30224/it.dorigatti.gazzetta W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
11-22 10:10:18.432 30180-30224/it.dorigatti.gazzetta W/System.err: at java.lang.Thread.run(Thread.java:818)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment