1

im a newbie here. my problem goes with this; i like to send all my data created from my sqlite database and send it all to my custom api via json object. is this possible? if is it, can you give me reference on how to do it? because i can't find any solution for this. thanks you :)

This is my database from my mobile ive like to fetch on retrofit

enter image description here

1 Answers1

1

The easiest way is to create an Object for your data then convert that Object to json via GSON.

public class Data {

private int id;
private double latitude;
private double longitude;
private long dateTime;
private int syncStatus;

//Create a constructor for all fields

//Don't forget to generate setters/getters

}

Then set the data for the Object

Data data = new Data(id, latitude, longitude, datetime, synctime);

To convert the Object to Json

Gson gson = new Gson();
String json = gson.toJson(data);

You can also use the Object directly without using GSON via Retrofit.

public interface GetDataService {

    @POST("/api/data")
    Call<Data> createData(@Body Data data);
}

Also use ORM when using sqlite.

mariozawa
  • 1,504
  • 1
  • 11
  • 14
  • almost my code was the same(no gson conversion only), but i dont get the logic... this is my last previous code it only sends data what ive been getting in my onvclick listener. to the api.. but i want the all data saved to be send like json object. –  Dec 06 '18 at 03:47
  • here sir, https://stackoverflow.com/questions/53644148/sqlite-data-send-it-via-json-object-for-api –  Dec 06 '18 at 05:05