In the Kinvey console click "Add an App", enter the name "TestDrive-java" when prompted.
- Download the TestDrive project.
- In Eclipse, go to File → Import…
- Click Java → Existing Project into Workspace
- Browse… to set Root Directory to the extracted zip from step 1
- In the Projects box, make sure the TestDrive project check box is selected. Then click Finish.
- Download the latest Kinvey library (zip) and extract the downloaded zip file, from: http://devcenter.kinvey.com/java/downloads
- Right click on your project → Build Path → Configure Build Path and on the Libraries tab click Add External Jars. Navigate to where you downloaded the Kinvey Jars and add them all.
- In Eclipse, right click the project → Close Project → right click project (again) → Open Project.
- Specify your app key and master secret in
HelloWorld
constant variables appKey and mastersecret
Take a look at our Getting Started guide for more information.
public class HelloWorld {
public static final String appKey = "my_app_key";
public static final String mastersecret = "my_master_secret";
With Kinvey's Java library you model data using any class that extends the GenericJson
class. The sample project comes with a HelloEntity
class that has a string somedata
, as well as a field labelled id
. When this class is converted to JSON, somedata
will be given the label "somedata", while id
will be given the label _id
which is accomplished by the attribute @Key("_id")
HelloEntity.java
looks like this:
public class HelloEntity extends GenericJson {
@Key("_id")
private String id;
@Key
private String somedata;
public HelloEntity(){}
public HelloEntity(String somedata){
this.somedata = somedata;
}
public String getSomedata() {
return somedata;
}
public void setSomedata(String somedata) {
this.somedata = somedata;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
The following method is called in the main(String[] args)
of the HelloWorld
class. When the app runs, first it will create a client and ping the backend, then it will login a user and next execute the code snippet below.
After creating an entity, the Client is used to access the AppData API, where a call to saveBlocking
will persist the entity with Kinvey. This call is performed syncronously, and a call to .execute()
returns the result.
HelloEntity test = new HelloEntity();
test.setSomedata("hello");
try{
HelloEntity saved = myJavaClient.appData("native", HelloEntity.class).saveBlocking(test).execute();
System.out.println("Client appdata saved -> " + saved.getId());
}catch (IOException e){
System.out.println("Couldn't save! -> " + e);
e.printStackTrace();
}
The AppData
object has a getBlocking
method as a well as a save, and this can be used to load the object by id that was just saved.
try{
HelloEntity loaded = myJavaClient.appData("native", HelloEntity.class).getEntityBlocking().execute();
System.out.println("Client appdata loaded by id -> " + loaded.getId());
}catch (IOException e){
System.out.println("Couldn't load! -> " + e);
e.printStackTrace();
}
This is very similar to Saving data, as the AppData API is accessed through the client. The call to getBlocking
supports a query, so this snippet is just sending a new empty query to retrieve all results.
Run the sample project and watch the logs, operations are performed in order and will output either success or failure.
- Data Store Guide - How to query for data, delete entities
- Security - How to share data or limit read/write access
- User Guide - How to add users to your app and understand authentication
##License
Copyright (c) 2013 Kinvey Inc.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.