Reputation:
I have some trouble in converting my datas into json
to fetch in retrofit. So here's how my work does.
I have a bunch of data in my sqlite
database which like this:
and what I've done is get this all data and convert it into json
like this one:
(PS. I've got this code in this link. https://tech.sarathdr.com/convert-database-cursor-result-to-json-array-android-app-development-1b9702fc7bbb)
public void getdbAndSendData() {
Cursor cursor2=databaseHelper.selectLocationFromLocalDatabaseAll(db);
JSONArray resultSet = new JSONArray();
JSONObject returnObj = new JSONObject();
cursor2.moveToFirst();
while (cursor2.isAfterLast() == false) {
int totalColumn = cursor2.getColumnCount();
JSONObject rowObject = new JSONObject();
for (int i = 0; i < totalColumn; i++) {
if (cursor2.getColumnName(i) != null) {
try {
if (cursor2.getString(i) != null) {
Log.d("TAG_NAME", cursor2.getString(i));
rowObject.put(cursor2.getColumnName(i), cursor2.getString(i));
} else {
rowObject.put(cursor2.getColumnName(i), "");
}
} catch (Exception e) {
Log.d("TAG_NAME", e.getMessage());
}
}
}
resultSet.put(rowObject);
cursor2.moveToNext();
}
cursor2.close();
Log.d("PAkultie", resultSet.toString());
This the result of my app in debugger:
Since I've got correctly the data, I like to have a getter setter constructor so that it may call the retrofit as its data and make a call back..
Anyway, this is my previous getter setter constructor. (PS. This getter setter stores row by row data in my sqlite and send it on retrofit and its working. but I've to update my data that can carry many rows to be send in retrofit in a single call)
public class MapDetails {
@SerializedName("SerialNumber")
@Expose
private String SerialNumber;
@SerializedName("Coordinate1")
@Expose
private String Coordinate1;
@SerializedName("Coordinate2")
@Expose
private String Coordinate2;
@SerializedName("DateTime")
@Expose
private String DateTime;
@SerializedName("Speed")
@Expose
private String Speed;
@SerializedName("Port")
@Expose
private int Port;
public MapDetails(String serialNumber, String coordinate1, String coordinate2, String dateTime, String speed, int port) {
SerialNumber = serialNumber;
Coordinate1 = coordinate1;
Coordinate2 = coordinate2;
DateTime = dateTime;
Speed = speed;
Port = port;
}
public String getSerialNumber() {
return SerialNumber;
}
public void setSerialNumber(String serialNumber) {
SerialNumber = serialNumber;
}
public String getCoordinate1() {
return Coordinate1;
}
public void setCoordinate1(String coordinate1) {
Coordinate1 = coordinate1;
}
public String getCoordinate2() {
return Coordinate2;
}
public void setCoordinate2(String coordinate2) {
Coordinate2 = coordinate2;
}
public String getDateTime() {
return DateTime;
}
public void setDateTime(String dateTime) {
DateTime = dateTime;
}
public String getSpeed() {
return Speed;
}
public void setSpeed(String speed) {
Speed = speed;
}
public int getPort() {
return Port;
}
public void setPort(int port) {
Port = port;
}
}
FYI: this is also my previous code on that constructor..
Cursor cursor = databaseHelper.retrieveLocationFromLocalDatabase(db);
while (cursor.moveToNext()) {
serialID =ursor.getString(cursor.getColumnIndex
(DatabaseHelper.SERIALNUMBER));
lati = cursor.getString(cursor.getColumnIndex
(DatabaseHelper.LATITUDE));
longi = cursor.getString(cursor.getColumnIndex
(DatabaseHelper.LONGITUDE));
dateTime = cursor.getString(cursor.getColumnIndex
(DatabaseHelper.DATE_TIME));
speed = cursor.getString(cursor.getColumnIndex
(DatabaseHelper.SPEED));
portss = cursor.getInt(cursor.getColumnIndex(DatabaseHelper.PORT));
}
MapDetails mapDetails = new MapDetails(serialID,
lati, longi, dateTime, speed, portss);
List<MapDetails> data = new ArrayList<>();
data.add(mapDetails);
try {
Retrofit.Builder builder = new Retrofit.Builder()
.baseUrl("http://" + ADDRESS + ":" + PORT) // this cotains my api link
.addConverterFactory(GsonConverterFactory.create());
Retrofit retrofit = builder.build();
API locate = retrofit.create(API.class);
Upvotes: 0
Views: 459
Reputation:
try to looking with my code, i've been realize that im not setting the size of my arraylist also, i found that you can add data on your json object to have a json array and the retrofit will be followed that to be send via rest json array..
first i've set the arraylist so that, i can store more buch of json object:
data = new ArrayList<>(20);
Second, my data ive get every 2 seconds will be added to my detail/jsonObject:
MapDetails mapDetails = new MapDetails(id, lat, lon,well,"2", 9090);
data.add(mapDetails);
And this is my new data now, in my json object i found in my debuger: (JsonObject to Json Array)
and you can send now via retrofit... and its working now :)
Upvotes: 1