If you are building a basic Java project, add the following lines to the beginning of your code:
AVOSCloud.initialize("{{appid}}", "{{appkey}}");
Basic Java projects can also use LeanCloud realtime-core library.
However, unlike Android, you need to explicitly create the websocket.
AVConnectionManager.getInstance().startConnection(new AVCallback() {
@Override
protected void internalDone0(Object o, AVException e) {
if (e == null) {
System.out.println("WebSocket connection established.");
} else {
System.out.println("Failed to establish WebSocket connection:" + e.getMessage());
}
}
});
If you are building for Android, add the following code into the onCreate function of the Application class:
import cn.leancloud.AVOSCloud;
public class MyLeanCloudApp extends Application {
@Override
public void onCreate() {
super.onCreate();
// Provide this, App ID, and App Key as parameters
AVOSCloud.initialize(this, "{{appid}}", "{{appkey}}");
}
}
Then specify the permissions needed by the SDK and declare the MyLeanCloudApp class in AndroidManifest.xml:
<!-- Basic modules (required) START -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<!-- Basic modules END -->
<application
…
android:name=".MyLeanCloudApp" >
<!-- LeanMessage and push notifications START -->
<!-- Both LeanMessage and push notifications need PushService -->
<service android:name="cn.leancloud.push.PushService"/>
<receiver android:name="cn.leancloud.push.AVBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.USER_PRESENT"/>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
<!-- LeanMessage and push notifications END -->
<!-- Feedback START -->
<activity
android:name="cn.leancloud.feedback.ThreadActivity" >
</activity>
<!-- Feedback END -->
</application>
You can easily trace the problems in your project by turning debug logs on during the development phase. Once enabled, details of every request made by the SDK along with errors will be output to IDE, browser console, or your app's Dashboard > LeanEngine > App logs.
// Place the line before AVOSCloud.initialize()
AVOSCloud.setLogLevel(AVLogger.Level.DEBUG);
Make sure debug logs are turned off before your app is published. Failure to do so may lead to the exposure of sensitive data.
Verifying
First of all, make sure you are able to connect to LeanCloud server. You can test it by running the following command:
curl "https://{{domain}}/1.1/date"
If everything goes well, it will return the current date:
This guide is written for the latest version of our SDK. If you encounter any error, please first make sure you have the latest version installed.
401 Unauthorized
If you get a 401 error or see the following content in network logs:
{
"code": 401,
"error": "Unauthorized."
}
It means that the App ID or App Key might be incorrect or don't match. If you have multiple apps, you might have used the App ID of one app with the App Key of another one, which will lead to such error.
Client Cannot Access the Internet
Make sure you have granted enough permissions for your mobile app.
How to Install the Java SDK
Installing SDK
There are several ways for you to install our SDK and the most convenient one is to use a package manager.
Installing with Package Manager
You can add our SDK into your project with a package manager of your own choice.
LeanStorage
For Maven:
For Ivy:
For SBT:
For Gradle:
If you are building for Android, use the following packages instead:
LeanMessage
For Maven:
For Ivy:
For SBT:
For Gradle:
Installing Manually
Download SDK
Initializing Your Project
To begin with, get your App ID and App Key from your app's Dashboard > Settings > App Keys.
If you are building a basic Java project, add the following lines to the beginning of your code:
Basic Java projects can also use LeanCloud realtime-core library. However, unlike Android, you need to explicitly create the websocket.
If you are building for Android, add the following code into the
onCreate
function of theApplication
class:Then specify the permissions needed by the SDK and declare the
MyLeanCloudApp
class inAndroidManifest.xml
:Since 6.1.0, we also support initializing SDK without using appKey.
If your application uses LeanMessage only without LeanPush, you can disable LeanPush using
AVIMOptions#disableAutoLogin4Push
on initialization:Enabling Debug Logs
You can easily trace the problems in your project by turning debug logs on during the development phase. Once enabled, details of every request made by the SDK along with errors will be output to IDE, browser console, or your app's Dashboard > LeanEngine > App logs.
Make sure debug logs are turned off before your app is published. Failure to do so may lead to the exposure of sensitive data.
Verifying
First of all, make sure you are able to connect to LeanCloud server. You can test it by running the following command:
If everything goes well, it will return the current date:
Then add the following code into your project:
Save the file and run it.
Then go to your app's Dashboard > LeanStorage > Data >
TestObject
. If you see the following content, it means that you have correctly installed the SDK.See Debugging if you're not seeing the content.
Debugging
This guide is written for the latest version of our SDK. If you encounter any error, please first make sure you have the latest version installed.
401 Unauthorized
If you get a
401
error or see the following content in network logs:It means that the App ID or App Key might be incorrect or don't match. If you have multiple apps, you might have used the App ID of one app with the App Key of another one, which will lead to such error.
Client Cannot Access the Internet
Make sure you have granted enough permissions for your mobile app.