How to read a local json file using retrofit 2.0?

Asked

Viewed 836 times

4

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

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

With Retrofit 2.0, it is accepted only instances of Okhttpclient and can no longer do 'custom clients''.

How best to get around this and mock my Client and read the Json file locally?

  • Theoretically you don’t need to use Retrofit to read this JSON locally, just point to the location. Then just use JSON Parsers only.

1 answer

1

Okhttp which was optional in version 1.9 is now required and automatically integrated. This has 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 the 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);

https://inthecheesefactory.com/blog/retrofit-2.0/en

Browser other questions tagged

You are not signed in. Login or sign up in order to post.