Question:
For API testing purposes, I need to read json files that would be my responses to API requests.
Well, with Retrofit 1.9 it was possible to implement a "Client" and override the "execute()" method so that it reads the json file locally.
With Retrofit 2.0, only OkHttpClient instances are accepted, and you can no longer make 'custom clients'.
What's the best way to get around this and make a mock for my Client and read the Json file locally?
Answer:
OkHttp which was optional in version 1.9 is now required as well as automatically integrated. This brought several benefits to the library.
In this version, to parse Json, you need to declare Gson Converter dependency separately: compile 'com.squareup.retrofit:converter-gson:2.0.0-beta2'
Then use addConverterFactory
. See example from the site itself:
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://api.nuuneoi.com/base/")
.addConverterFactory(GsonConverterFactory.create())
.build();
service = retrofit.create(APIService.class);
https://inthecheesefactory.com/blog/retrofit-2.0/en