+ Start a Discussion
Stefan FellnerStefan Fellner 

REST JSON 2 JAVA MAPPING

Hello,

I am creating a Android Application and I am using the Salesforce SDK.

As recommended I am using the REST API to get data from salesforce servers. All the examples indicate to hardcode the parameters to work with JSON Objects, for example to get the Id of a contact:

records.getJSONObject(i).getString("Id")

Is there any better way to avoid using the key of the json object as a string?
Normally I am using GSON library to parse the jsonObjects into my java objects. Are there java classes like Contact or Account available?

Thank you for your help!
Stefan

 
BalajiRanganathanBalajiRanganathan
you can try force.com webservices connector. it is java client for SOAP API and Bulk API, but it will have the model classes you are looking for like 
contact and account.

https://developer.salesforce.com/page/Introduction_to_the_Force.com_Web_Services_Connector
Stefan FellnerStefan Fellner
Hello Balaji,

if I have the WSC as a library in my project I would have a class like Contact.java but in the mechanism of SmartStore I am required to specify a key for all the attributes, like Id, Birthday, MobilePhone to be able to store and retrieve single contacts.

How would the WSC library solve this? Get the name of the field with something like this?

Field[] allFields = Contact.class.getFields();
allFields[0].getName();

Is this considered as the best practice? Does anyone has a better idea to handle the key issue on json objects?

Thanks
akhilesh_sfdcakhilesh_sfdc
Stephan, you should probably take a look at the new sample app that we added with our SDK 3.0 release: https://github.com/forcedotcom/SalesforceMobileSDK-Android/blob/master/native/SampleApps/SmartSyncExplorer/

This Sample app uses the new wrappers in SmartSync library to simplify interaction with Salesforce objects. For example checkout this class: https://github.com/forcedotcom/SalesforceMobileSDK-Android/blob/master/native/SampleApps/SmartSyncExplorer/src/com/salesforce/samples/smartsyncexplorer/objects/ContactObject.java

Also, feel free to post your questions on our google+ community at: http://sfdc.co/mobilesdkcommunity
Stefan FellnerStefan Fellner
Ok, thank you for the updated version of the SDK!

I think the solution you are providing is good, but on the long run creating those objects, such as "ContactObject" manually is not very robust in terms of changes to the API.

Are you going to deliver these classes as part of the SDK in future releases?
akhilesh_sfdcakhilesh_sfdc
Since every customer has a different set of objects and fields customizations, we cannot ship the SDK with the objective-c/java representation of those. Because of these limitations one needs to create these classes manually based on application needs.

 
Stefan FellnerStefan Fellner
I can see your point!

But why not shipping the standard objects with its attributes like you can get those from the salesforce workbench. And on top developers can customize these objects depending on their special needs.
An alternative would be to provide a Constant class with all the attributes names, so the usage of the same key in terms of Json objects can be guaranteed. In your example app "SmartSyncExplorer --> MainActivity" you use a hard coded string to store the Contact object attributes.