Q:

Retrofit 2 API Can I use local file path or json string instead of url?

Hello I am working on an Android App which uses retrofit API getting response from server. Retrofit Automatically parse the json response and creates objects of POJO class. I am storing that json into sqlite and if internet is not connected call the json from sqllite, facing difficulty have to parse json manually.
Is there any way I use retrofit library to parse json and make pojo from json string or file path?My code is here to fetch from url:
@FormUrlEncoded
@POST("getResponse")
Observable<UserResponse> getResponse(@Field("token") String token);

I want something like this if internet is not connected.
@FromStringEncoded
Observable<UserResponse> getResponseOffline(@Field("token") String token);

Thanks.

A:

You don't mentioned proposes. I use below solution for mocking server in app on very early stage of development when real server doesn't work yet. 
So you can use interceptors in OkHttp. Like this:
OkHttpClient.Builder builder = new OkHttpClient.Builder();       
builder.addInterceptor(new MockClient(context));

and MockClient looks like this:
public class MockClient implements Interceptor {

    Context context;

    public MockClient(Context context) {
        this.context = context;
    }

    @Override
    public Response intercept(Chain chain) throws IOException {

        HttpUrl url = chain.request().url();
        //here determine what to do base on url.
        //e.g.:
        switch(url.encodedPath()) {
           case "some/path" :
            String response = readJsonFieleFromAssestOrAnyOtherStorage();
            return new Response.Builder()
                        .code(200)
                        .message(response)
                        .request(chain.request())
                        .protocol(Protocol.HTTP_1_1)
                                    .body(ResponseBody.create(MediaType.parse("application/json"), response.getBytes()))
                        .addHeader("content-type", "application/json")
                        .build();
        }
    }
}

A:

Simply use Google's GSON Library that allows you to convert json to POJO and vice versa. Fetch json from sqlite and parse it using gson.
Gson gson=new Gson();
UserResponse userResponse= gson.fromJson(jsonInStringFromDb,UserResponse.class);

You can also parse JSON from file using Gson.
JSON to Java object, read it from a file.
Gson gson = new Gson();
Staff staff = gson.fromJson(new FileReader("D:\\file.json"), Staff.class);

