How to read a local json file using retrofit 2.0?

4

For API testing purposes, I need to read json files that would be my responses in API requests.

Well, with Retrofit 1.9 it was possible to implement a "Client" and override the "execute ()" method to read the json file locally.

With Retrofit 2.0, only instances of OkHttpClient are accepted, no longer being able to make 'custom clients'.

What is the best way to get around this and mock my client and read the Json file locally?

    
asked by anonymous 01.12.2015 / 13:44

1 answer

1

OkHttp that was optional in version 1.9 is now required, as well as automatically integrated. This has brought several benefits to the library.

In this release, 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 of the site itself:

Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://api.nuuneoi.com/base/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        service = retrofit.create(APIService.class);

link

    
11.01.2017 / 11:55