My android application is crashing on start up, because of a small error in my back-end side which is in firebase. A document in my firestore database has a field in which an Image object (id and url) is expected but for some reason it's stored as a long in the database.
I get this error:
java.lang.RuntimeException: Could not deserialize object. Can't convert object of type java.lang.Long to type com..data.entity.gps.tracker.ImageEntity
My ImageEntity class:
public class ImageEntity {
private int id;
private String image_id;
public String getImage_id() {
return image_id;
}
public void setImage_id(String image_id) {
this.image_id = image_id;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
I can't just exclude the cover ImageEntity object, it's an integral part of my app, how can I add a validation step to check if the cover object is a valid object and not a long?
This is where the crash happens:
public List<Model> mapList(QuerySnapshot querySnapshot) {
List<Model> list = new ArrayList<>();
for (QueryDocumentSnapshot queryDocumentSnapshot : querySnapshot) {
Model model = map(queryDocumentSnapshot);
long id = ((Release) model).getId();
if (!addedReleasesIDs.contains(id)) {
list.add(model);
addedReleasesIDs.add(id);
}
}
return list;
}
public Model map(QueryDocumentSnapshot queryDocumentSnapshot) {
// Auto serialization
Entity entity = queryDocumentSnapshot.toObject(getEntityClass()); // CRASH HERE
return map(entity);
}
If you need to take control over the deserializaition of a document snapshot, you should access its fields as a Map<String, Object> using getData() instead of using the Firebase SDKs internal mapper.
Map<String, Object> data = (Map<String, Object>) queryDocumentSnapshot.getData();
With this, you can now get() the individual fields by name out of the data map, check their types, and convert them to whatever other type you want for your model object.
This is also faster in terms of performance than using toObject because it doesn't have to use Java reflection.
I created a family tree application on java and mysql database. Now I am testing an android app for the same. So I converted my mysql database file to JSON format and uploaded it to firebase. When I am inserting records on it, it is working perfectly fine but when I try to fetch the data it is showing the error:
com.google.firebase.database.DatabaseException: Failed to convert a value of type java.lang.String to int
at com.google.android.gms.internal.firebase_database.zzkt.zzb(Unknown Source:180)
What should be the problem? I tried deleting the data from the database which I uploaded through JSON file and then inserted records directly from app into database and fetch them, it worked fine but when I am adding record from JSON file only then It is creating problem.
here is the code from the app for fetching data:
public void clicked(){
mDatabase.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
List<Family> FamilyList = new ArrayList<>();
for (DataSnapshot adSnapshot: dataSnapshot.getChildren()) {
Family f = adSnapshot.getValue(Family.class);
FamilyList.add(f);
// adsList.add(adSnapshot.getValue(Family.class));
}
Toast.makeText(MainActivity.this, "Total Records: "+FamilyList.size(), Toast.LENGTH_SHORT).show();
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
Family Model Class:
public class Family {
int id;
String name;
String fatherName;
int fid;
String city;
String state;
public Family(int id, String name, String fatherName, int fid, String city, String state) {
this.id = id;
this.name = name;
this.fatherName = fatherName;
this.fid = fid;
this.city = city;
this.state = state;
}
public Family()
{
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getFatherName() {
return fatherName;
}
public void setFatherName(String fatherName) {
this.fatherName = fatherName;
}
public int getFid() {
return fid;
}
public void setFid(int fid) {
this.fid = fid;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
}
Here is the JSON Record Sample
It is the records which I entered through App
I think the converted JSON treated id and fid as String, while in mySQL they are int. (am I correct?)
Tell me if any other code is needed.
The problem relies on here
Family f = adSnapshot.getValue(Family.class);
you are trying to get data in an inappropriate type.
You should correct this by checking Family.class and check if the values there are the same as they are in your database structure in firebase, it will be helpful if you put your database structure here, or some images.
Check if for example in Family.class you have in your variable types the same as they are in firebase, with the same name also.
So for example if in firebase there is an string called name you should have in your constructor inside Family.class the same type and name.
String name;
and in Firebase your json key should be name too.
For instance, check this
your Family class for example should have the variables with the same name and type as your database.
Also check, if the value in firebase has "" is an String type and in your POJO you should have a variable with the type String for what you are trying to access, but if the value dosnt have "" it should be a long , int, double or any type of number.
EDIT: check this structure
It has all the values types as String, but in your Family.class you have the values right for this type of structure.
you should change your database at firebase so all your types matches with the ones in your Family.class, either way it won't fetch your values
Note: if you want to fetch all your values like they are at the first image, just change in Family.class from this:
int id;
String name;
String fatherName;
int fid;
String city;
String state;
to this:
String id;
String name;
String fatherName;
String fid;
String city;
String state;
Also change your constructor types and everything to match all Strings
The thing is that firebase creates unique IDs for each element in your database structure, and the types imported from your MySQL database are not the same as the firebase ones, I suggest you to either change your Family.class variable types as I mentioned above, or replicate your MySQL database with firebase and the same variable types.
This is happening because you have a variable of integer datatype in your model but you are returning String from Firebase... either convert your variable to string or return integer from firebase....
Both model and Firebase variable should be of a same data type.
As per your below error:-
com.google.firebase.database.DatabaseException: Failed to convert a value of type java.lang.String to int at com.google.android.gms.internal.firebase_database.zzkt.zzb(Unknown Source:180)
I think, you need to check somewhere you tried to store value in int which is store in firebase in string.
So first check it and if necessary to cast then casting your value using Integer.parseInt or Integer.valueof
for example,
Integer.valueOf(dataSnapShot.getValue());
or
Integer.parseInt(dataSnapShot.getValue());
For more understanding this you can also refer stack overflow's below links:
Firebase DatabaseException: Failed to convert value of type java.lang.Long to String
com.google.firebase.database.DatabaseException: Failed to convert a value of type java.lang.String to double
Firebase android error "Failed to convert value of type "
I am currently developing an android application for a client who is insisting to use Odoo for API. I don't have any idea about it. I am not getting it even after referring to this link. They provide an URL, Database name, username, and password. If anyone did Odoo with Android before, can you give any suggestions?
There are a lot of ways to connect Android to Odoo. Here they are:
Json-RPC
XML-RPC (especially aXMLRPC, this is what I am using)
There is also a framework called Odoo Mobile Framework . I have tried it but found a lot of issues and I was not able to get it work properly. You can find the documentation here.
Odoo has a Web Service API which is available for Python, Ruby, PHP and Java. I strongly recommend to take a look.
For my case, I have cloned the aXMLRPC git repository, created a package in my project and adapted the original package name. But recently I have found this on Stack Overflow explaining how to add aXMLRPC to your Android project using Gradle (I didn't give it a try yet).
Odoo had made available three endpoints:
xmlrpc/2/db to get the list of available databases on your server, it does not require to be authenticated;
xmlrpc/2/common to log in to the server, it does not require to be authenticated;
xmlrpc/2/object, is used to call methods of odoo models via the execute_kw RPC function.
public class OdooConnect {
String url;
private XMLRPCClient client;
public OdooConnect(String serverAddress, String path) {
url = serverAddress + "/xmlrpc/2/" + path;
client = new XMLRPCClient(url);
}
public Object login(String db, String username, String password) {
Object object;
try {
object = client.call("login", db, username, password);
return object;
} catch (XMLRPCException e) {
e.printStackTrace();
}
return null;
}
public Object checkServer() {
Object object;
try {
object = client.call("list", new Object[]{});
return object;
} catch (XMLRPCException e) {
e.printStackTrace();
}
return null;
}
}
In this class, the constructor as arguments the server address (it can be http(s)://your_ip_address:the_port_number) and the path ('db', 'common' or 'object').
The checkServer method returns an object which is actually an array containing the list of available databases.
The login mehtod returns an Integer which is the Id of the authenticated user.
For the Odoo CRUD mehtods (search_read, search_count, search, write, create, unlink) you can take a look to the Odoo Web Service API Java code matching the method you want.
Here is an example of the search_read method. I assume that you've an XMLRPCClient named client.
public Object search_read(String db, int user_id, String password, String object, List conditions, Map<String, List> fields) {
Object result = null;
try {
result = client.call("execute_kw", db, user_id, password, object, "search_read", conditions, fields);
} catch (XMLRPCException e) {
e.printStackTrace();
}
return result;
}
Where
object is an Odoo model for example "res.partner"
conditions is the domain (filter) something like this: Collections.singletonList(Collections.singletonList(Arrays.asList("supplier", "=", true)));
fields, the fields you want to get,
fields = new HashMap() {{put("fields", Arrays.asList("id","name","is_company","street")); }};
You must cast the result of the method to Object[] which will give you an array containing a list of objects each representing a record.
Object[] objects = (Object[]) result;
if (objects.length > 0) {
for (Object object : objects) {
String name= OdooUtil.getString((Map<String, Object>) object, "name");
boolean is_company= OdooUtil.getBoolean((Map<String, Object>) object, "is_company");
String street = OdooUtil.getString((Map<String, Object>) object, "street");
int id= OdooUtil.getInteger((Map<String, Object>) object, "id");
}
}
Here the OdooUtil class
public class OdooUtil {
public static String getString(Map<String, Object> map, String fieldName) {
String res = "";
if (map.get(fieldName) instanceof String) {
res = (String) map.get(fieldName);
}
return res;
}
public static Integer getInteger(Map<String, Object> map, String fieldName) {
Integer res = 0;
if (map.get(fieldName) instanceof Integer) {
res = (Integer) map.get(fieldName);
}
return res;
}
public static Double getDouble(Map<String, Object> map, String fieldName) {
Double res = 0.0;
if (map.get(fieldName) instanceof Double) {
res = (Double) map.get(fieldName);
}
return res;
}
public static Boolean getBoolean(Map<String, Object> map, String fieldName) {
Boolean res = false;
if (map.get(fieldName) instanceof Boolean) {
res = (Boolean) map.get(fieldName);
}
return res;
}
public static Float getFloat(Map<String, Object> map, String fieldName) {
Float res = 0f;
if (map.get(fieldName) instanceof Float) {
res = (Float) map.get(fieldName);
}
return res;
}
}
If you have a many2one field you only have access to the id and the name of the related record. You can use the following class to get the id and the name of the many2one record.
public class Many2One {
private int id;
private String name;
public Many2One() {
}
public static Many2One getMany2One(Map<String, Object> stringObjectMap, String fieldName) {
Integer fieldId = 0;
String fieldValue = "";
Many2One res = new Many2One();
if (stringObjectMap.get(fieldName) instanceof Object[]) {
Object[] field = (Object[]) stringObjectMap.get(fieldName);
if (field.length > 0) {
fieldId = (Integer) field[0];
fieldValue = (String) field[1];
}
}
res.id = fieldId;
res.name = fieldValue;
return res;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
}
Example of usage of Many2One class
String partner_name= Many2One.getMany2One((Map<String, Object>) object, "partner_id").getName();
int partner_id= Many2One.getMany2One((Map<String, Object>) object, "partner_id").getId();
For other remaining CRUD methods, you can easily find a way how they work by reading the Odoo Web Service API documentation.
I hope this gives you some insights.
This is Just an Example did to access contacts/partners from odoo:
#!/usr/bin/env python
import csv
from xmlrpclib import ServerProxy
SERVER = 'http://localhost:8069'
DATABASE = 'testcompany'
USERNAME = 'admin'
PASSWORD = 'password'
FILE_PATH = 'ODOO_clientsMain2_test.csv'
server = ServerProxy('http://localhost:8069/xmlrpc/common')
user_id = server.login(DATABASE, USERNAME, PASSWORD)
server = ServerProxy('http://localhost:8069/xmlrpc/object')
def search(list, key):
for item in list:
return item[key]
reader = csv.reader(open(FILE_PATH,'rb'))
for row in reader:
#print row
partner_template = {
'name': row[0],
#'company_id': row[1],
}
if row[2] is not None and row[2]<>'':
partner_template.update({'email': row[2]})
if row[5] is not None and row[5]<>'':
partner_template.update({'tin': row[5]})
if row[6] is not None and row[6]<>'':
partner_template.update({'ref': row[6]})
if row[8] is not None and row[8]<>'':
partner_template.update({'phone': row[8]})
if row[9] is not None and row[9]<>'':
partner_template.update({'mobile': row[9]})
print partner_template
partner_id = server.execute_kw(DATABASE, user_id, PASSWORD, 'res.partner', 'create', [partner_template])
#create External ID
external_ids = {
'model': 'res.partner',
'name': row[11],
'res_id': partner_id,
}
external_id = server.execute_kw(DATABASE, user_id, PASSWORD, 'ir.model.data', 'create', [external_ids])
# update related fields
if row[7] is not None and row[7]<>'':
#look up and update payment term
payment_term_id = server.execute_kw(DATABASE, user_id, PASSWORD, 'account.payment.term', 'search_read', [[['name','=',row[7]],['active', '=', True]]],{'fields': ['id'], 'limit': 1})
if payment_term_id is not None:
id = server.execute_kw(DATABASE, user_id, PASSWORD, 'res.partner', 'write', [[partner_id],{'property_payment_term': search(payment_term_id,'id')}])
if row[10] is not None and row[10]<>'':
#look up and update pricelist
pricelist_id = server.execute_kw(DATABASE, user_id, PASSWORD, 'product.pricelist', 'search_read', [[['name','=',row[10]],['active', '=', True]]],{'fields': ['id'], 'limit': 1})
if pricelist_id is not None:
id = server.execute_kw(DATABASE, user_id, PASSWORD, 'res.partner', 'write', [[partner_id],{'property_product_pricelist': search(pricelist_id,'id')}])
If you are creating your application from stretch and only required Android API for Odoo, here is open-source API https://github.com/oogbox/odoo-mobile-api (Odoo android api)
To use in android, first add the following dependency to your app level build.gradle
compile 'com.oogbox.api:odoo:1.0.0'
Documentation: https://github.com/oogbox/odoo-mobile-api#getting-started
Thanks
here is my jsonString:
{
"status":1,
"data":[
{
"id":"39",
"friendsInfo":{
"email":"test#gmail.com",
"phone":null,
"language":"en"
}
},
{
"id":"39",
"friendsInfo":{
"email":"test#gmail.com",
"phone":null,
"language":"en"
}
}
],
"message":""
}
here is my receivingClass:
public class mAnswer{
#SerializedName("status")
public int mEnterStatus;
#SerializedName("data")
public List<Data> dataList;
public class Data {
#SerializedName("id")
public int mUserId;
#SerializedName("friendsInfo")
public GetUserDetails getUserDetails;
public class GetUserDetails{
#SerializedName("email")
public int email;
#SerializedName("phone")
public String phone;
#SerializedName("language")
public String language;
}
}
}
and here is my code for successful receiving answer where I am trying to save this data to SQLite db:
private void saveList(){
Vector<ContentValues> cVVector = new Vector<ContentValues>();
int arraySize = mAnswer.dataList.size();
for (int i = 0; i < arraySize; i++){
// **HERE IS PROBLEM**
cVVector.add(friendsValue);
}
if (arraySize > 0 ){
ContentValues[] cvArray = new ContentValues[arraySize];
cVVector.toArray(cvArray);
mContext.getContentResolver().
bulkInsert(J4D_DB_Contract.UserFriendsEntry.CONTENT_URI, cvArray);
}
}
So here is my problem:
how to correct call #SerializedName elements when i am trying to save List data to db?
any ideas?
Will be glad any help! Thanks!
mAnswer.dataList.get(0);
gives back the
"id":"39",
"friendsInfo":{
"email":"test#gmail.com",
"phone":null,
"language":"en"
}
this is what you should say in the loop:
mAnswer.dataList.get(0).getFriendsObject().getEmail();
mAnswer.dataList.get(0).getFriendsObject().getPhone();
I would use this site to generate my POJO, beucase it`s more cleaner I think.
http://www.jsonschema2pojo.org/
This site gives back well defined POJO architecture, what is ready for use.
Just set that you are working with JSON instead of JSON Scheme and using GSON (if you are using retrofit).
After you can copy your java files to your workplace.
You don't need the #SerializedName("jsonName") if the variable has the same name private String jsonName;
I have this Json statement :
{name=Adam Schmidt, id=43}
and I want to extract the value of the name,
trying this code but it didn't work
// parse json data
try {
JSONObject userObject = new JSONObject(result);
userName = userObject.getString("name");
tvName.setText(userName);
} catch (Exception ex) {
ex.printStackTrace();
}
You statement is not valid json. Here's the sample of valid json.
{\"name\":\"Adam Schmidt\", \"id\":43}
Update:
For number value, no quotation mark
Use GSON. Super easy.
Declare a class that represents your JSON structure:
public class Person {
private String name;
private int id;
public String getName() {
return name;
}
public int getId() {
return id;
}
}
Then you can do:
String json = "{\"name\":\"Adam Schmidt\", \"id\":43}";
String userName = new Gson().fromJson(json, Person.class).getName();
tvName.setText(userName);
This is much better than haphazardly parsing chunks with various get methods all over your code. Plus you get a nice object to pass around and use in your object oriented code.