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.
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);