sqlite database /retrofit (json object) - android-sqlite

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

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.

Related

How to access object data c# (Firebase)

I get data from my database (Firebase database). This is a document with several data, including a Array with GeoPoint fields.
var documents = await CrossCloudFirestore.Current .Instance .Collection("Activity") .GetAsync();
My object is defined like this in my Model class: public class Activity { public object elapsedTime { get; set; } public object positions { get; set; } ...... }
It works. I can have the data on my Activity class.
However, I would like to know how I can access this data because foreach loops do not work for object types. Maybe I need to redefine my Activity Class ?
Thx :=)
Edit :
You can see here my firebase :
And then the result of the request :
In your scenario , the property is dynamic for each object , I would suggest you deserialize the root level as a dictionaryand then get the data you want .
Items = JsonConvert.DeserializeObject<Dictionary<string, YourClass>>(content).Values.ToList()
Oh before this, Newtonsoft plugin is required .
Refer to
Deserialize JSON into readable format
It works ! :)
To do it, I defined Array (from firestore) as IEnumerable on my C# class, with the same name from firestore to c#.
And then, here is my query and the line to get my objects converted.

How to make and relate complex table with each other in sqfllite flutter?

I am using sqflite package to store data offline as well once it got from API. I had seen a couple of examples and all used the simple Strings and not lists in their models as a type and due to that I am feeling difficulty in the conversion of these classes in respective tables and link them so that data can be fetch easily once synced. Can someone guide me on how to make create and link tables in sqflite with these types of classes so that they work perfectly?
TodayDeliveriesModel class
class TodayDeliveriesModel {
final String Name;
final List<ItemsModel> allItems;
final bool pending;
TodayDeliveriesModel({this.Name, this.allItems, this.pending});
}
Items Model class:
class ItemsModel {
String name;
String quantity;
ItemsModel({this.name, this.quantity});
}
fetch and Parse JSON data from the server.
Create Database Table with just 3 parameters.
class DeliveryModel{
String name;
String items;
bool isPending;
}
then when you save data in the database, just covert List to String
and save this string as the items.
After then when you get this string from database convert this into again List

How to convert SinkRecord to JSON string?

Imagine myAPICreate requires a JSON string.
public void put(Collection<SinkRecord> collection) {
for (SinkRecord record : collection) {
JSONObject recordJson = toJSON(record.value());
String recordJsonString = recordJson.toString();
myAPICreate(recordJsonString);
}
}
toJSON is a helper I have defined which just takes the record and returns a JSONObject.
JSONObject json = new JSONObject()
.put("a", record.getString("a"))
.put("b", record.getString("b"))
.put("c", record.getString("c"));
I feel like I might be doing a lot of redundant work here. Is it necessary to have the code in put convert it to JSON or is there a way to use the converters so that record already comes in as JSON or a JSON string? Then I can just pass myAPICreate(record.value().toString()) without having to manually do it?
When you create a SinkRecord, you have a key & value schema w/ a key and value Object. Those objects should be Struct instances that must be created with the matching Schema
In the Connector configuration, you would then use JSONConverter (or other converter) to get the serialized output

GWT AutoBean - not serializing ArrayList<String>, other pojos ok?

I can't get an ArrayList to serialize using the AutoBean mechanism. I'm using GWT 2.7. Here is my setup:
public interface IUser {
String getName();
void setName(String name);
ArrayList<String> getFriends();
void setFriends(ArrayList<String> friends);
}
public class User implements IUser {
private String name;
private ArrayList<String> friends;
... getters and setters for all members attributes ...
}
then my bean factory:
public interface AutoBeanFactoryImpl extends AutoBeanFactory {
AutoBean<IUser> user(IUser inst);
}
and finally my usage:
ArrayList<String> friends = new ArrayList<String>();
friends.add("Mary");
User user = new User();
user.setName("Fritz");
user.setFriends(friends);
AutoBeanFactoryImpl factory = GWT.create(AutoBeanFactoryImpl.class);
AutoBean<IUser> bean = factory.user(user);
String json = AutoBeanCodex.encode(bean).getPayload();
The output json does not have the friends array. It has the name ok though. Why doesn't the string array get serialized? Do I need something special for that?
Thanks
The problem is that you specified ArrayList instead of just plain List - in order to nicely generate code that fills in the gaps between Java and JSON, autobeans only want to work with interfaces. Aside from getters and setters, AutoBeans have special support for List, Set (which is just a List without duplicates in the JSON), and Map. If you specify a particular implementation, the AutoBean tooling can't always handle it.
Feel free to pass in an ArrayList instance, but declare your getter and setter to be of type List.
As an aside, those collections can be of any type that AutoBeans can otherwise handle - including numbers, booleans, strings, dates, and any other bean-like interface that also conform to what AutoBeans can deal with.

Storing multidimensional arrays with Morphia

I have troubles reading/unmarshalling multidimensional arrays with Morphia.
The following class:
#Entity
class A {
double[][] matrix;
}
is properly marshalled and stored in mongodb, but when reading it I get an exception that the double[][] can not be constructed. I've tried to use a custom TypeConverter but it is not getting invoked for such types.
Similar issues I get when using a member like this:
List<double[]> matrix;
I did not find any annotations that could help morphia figure out what type is expected in the array.
I suspect this is not supported yet.
Any suggestions ?
Thanks in advance.
I haven't used multi-dimensional arrays with Morphia yet, so I can't say much about that.
However, I've done the following for unsupported data types (like BigDecimal):
Define the unsupported data type as transient
Define a supported data type for storing your information
Serialize / unserialize it into a supported data type via #PrePersist and #PostLoad
My code looks something like this:
#Transient
private BigDecimal salary;
private String salaryString;
#PrePersist
public void prePersist(){
if(salary != null){
this.salary = this.salary.setScale(2, BigDecimal.ROUND_HALF_UP);
salaryString = this.salary.toString();
}
}
#PostLoad
public void postLoad(){
if(salary != null){
this.salary = this.salary.setScale(2, BigDecimal.ROUND_HALF_UP);
this.salary = new BigDecimal(salaryString);
}
}