Starting Your First App
If you are starting with a fresh Java project or an existing app, follow these steps to add the Kinvey Java library to your app and set up the Kinvey Client
.
Prerequisites
Manual Project Set Up
Eclipse
- In Eclipse, create a new Java project: File → New → Java Project. Name it appropriately (eg, "yourapp"). Set Java execution environment to 1.6. Click Finish.
- Download the latest Kinvey library (zip) and extract the downloaded zip file.
- 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.
Add an App Backend
In the Kinvey console, click Create an App and enter the name of your app when prompted.
You can find your key and secret in the dropdown menu in the environment sidebar.
Copy the key and secret when performing the next steps.
Initialize a Client
The Client.Builder
is used to build and initialize the Client
before making any calls to the Kinvey API.
The Client.Builder
requires the App Key and App Secret obtained from the Kinvey console. Initializing a Client
is usually done when your application first starts.
import com.kinvey.nativejava.Client;
...
final Client mKinveyClient = new Client.Builder(your_app_key, your_app_secret).build();
final Client mKinveyClient = new Client.Builder(your_app_key, your_app_secret).setBaseUrl("<your dedicated host URL>").build();
Verify Set Up
You can use myClient.ping()
to verify that the app has valid credentials. The following code example:
- builds a new client
- pings the backend with the app credentials
import import com.kinvey.nativejava.Client;
...
Client myJavaClient = new Client.Builder(appKey, appSecret).build();
try{
Boolean pingResult = myJavaClient.ping();
System.out.println("Client ping result -> " + pingResult);
}catch(Exception e){
System.out.println("something went wrong!");
e.printStackTrace();
}
Authenticating with Master Secret
While your client-side apps are used by real-life human beings, your server-side code is going to need more control without managing an active user's lifecycle. This can be accomplished by logging in with your app key and master secret, which will be used to authenticate all requests made against your backend.
After building your Client
, you can call:
try{
myJavaClient.user().loginBlocking(MY_APP_KEY, MY_MASTER_SECRET).execute();
System.out.println("Client logged in -> " + myJavaClient.user().isUserLoggedIn());
}catch (IOException e){
System.out.println("Couldn't login -> " + e);
e.printStackTrace();
}
What's next
You are now ready to start building your awesome apps! Next we recommend diving into the User guide or Data store guide to learn more about our service, or explore the sample apps to go straight to working projects.