Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 251
Saqib Ahmed Avatar asked Jan 13 '17 04:01

Saqib Ahmed


People also ask

What are @path parameters in retrofit?

They also come before method parameters. They are named replacement in a URL path segment. Path parameters may not be null. Values passed in @Path annotations modified and made URL encoded before full API path resolution. 2.1. Simple path parameter An example to use @Path parameters in Retrofit 2.

How does retrofit handle JSON data?

In the past, Retrofit relied on the Gson library to serialize and deserialize JSON data. Retrofit 2 now supports many different parsers for processing network response data, including Moshi, a library build by Square for efficient JSON parsing.

Does Retrofit 2 support URL handling?

We’ve recognized that the url handling in Retrofit 2 definitely requires more attention and in tomorrow’s post you’ll learn how request urls will be revolved within different scenarios. Still Have Questions? Get Our Retrofit Book!

What is the difference between null and @path in retrofit?

E.g. Call to service.getUsers (null, null) will result in 'https://DOMAIN/api/users'. 2. Path parameters Path parameters in Retrofit 2 are denoted with @Path annotation. They also come before method parameters. They are named replacement in a URL path segment. Path parameters may not be null.


1 Answers

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();
        Log.d("TAG","url="+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();
        }
    }
}
like image 111
LunaVulpo Avatar answered Oct 12 '22 04:10

LunaVulpo