The official pCloud Java SDK repository
The official pCloud SDK for Java & Android for integrating with pCloud's API.
Login to the pCloud Developer Site
Create an application in the pCloud App Console Page.
Take note of the app key(client ID) of your application once you create it.
Redirect URIs
field in your application configuration page.The SDK expects
pcloud-oauth://{your.application.package}
custom Uri to be added in the Application configuration page.
Allow implicit grant
option.Grab via Maven:
<dependency>
<groupId>com.pcloud.sdk</groupId>
<artifactId>java-core</artifactId>
<version>1.9.1</version>
<type>pom</type>
</dependency>
or Gradle:
implementation 'com.pcloud.sdk:java-core:1.9.1'
In addition to the functionality provided by the java-core
module, Android applications can benefit from platform-specific features.
The android
module provides a built-in authorization Activity that handles the application authorization requests.
For details on usage, refer to the AuthorizationActivity documentation.
Grab via Maven:
<dependency>
<groupId>com.pcloud.sdk</groupId>
<artifactId>android</artifactId>
<version>1.9.1</version>
<type>pom</type>
</dependency>
or Gradle:
implementation 'com.pcloud.sdk:android:1.9.1'
ApiClient apiClient = PCloudSdk.newClientBuilder()
.authenticator(Authenticators.newOAuthAuthenticator(<your OAuth access token here>))
// Other configuration...
.create();
ApiClient
instances should be reused as much as possible. Avoid creating multiple instances.ApiClient
instances can be re-configured by calling ApiClient.newBuilder()
and creating a new instance.ApiClient
instances can be 'killed' by calling ApiClient.shutdown()
.Call
:Call<RemoteFolder> call = apiClient.listFolder(RemoteFolder.ROOT_FOLDER_ID);
Call
instances does not by itself make any API request, treat the objects as a declaration of intent. See the following sections for more information on how to execute calls.Call
and obtaining the result on the same thread:RemoteFolder folder = call.execute();
Call.execute()
will execute the call on the calling thread, blocking it until a response is delivered, a timeout is reached or an error occurs.Call.execute()
will throw an IOException
on a networking error and ApiError
on error returned by pCloud's API.ApiClient.Builder
Call.execute()
from the UI thread, as it can potentially block for an extended period of time. On Android applications targetting API11+ , calling this method from the main thread will cause a NetworkOnMainThreadException
.
Call
asynchronously:call.enqueue(new Callback<RemoteFolder>() {
@Override
public void onResponse(Call<RemoteFolder> call, RemoteFolder response) {
// Successful response
}
@Override
public void onFailure(Call<RemoteFolder> call, Throwable t) {
// Call failed with an error.
}
});
Call.enqueue()
will return immediately, the actual work will scheduled on another thread.Callback.onResponse()
will be called on a successful response.Callback.onFailure()
will be called if an error occurs during the execution of the call.Callback
methods will be called on an arbitrary thread, to control this behavior see ApiClient.Builder.callbackExecutor(Executor)
Call
instances:Call.execute()
or 'Call.enqueue()' after the former have been already called, will lead to a runtime exception.Call.clone()
that will create a new, identical object, that can be used to make a new API request.Call<RemoteFolder> newCall = call.clone();
// Get direct children.
RemoteFolder folder = apiClient.listFolder(<The folder id of target folder>);
// Get direct children and other files recursively. (slower and potentialy memory-intensive).
RemoteFolder folder = apiClient.listFolder(<The folder id of target folder>, true);
or
RemoteFolder folder = ...;
// Update folder and children information.
RemoteFolder updatedFolder = folder.update();
// Update folder and children information, including all child entries.
RemoteFolder updatedFolder = folder.update(true);
RemoteFile file = ...;
RemoteFolder destinationFolder = ...
// Copy file to folder, will block.
RemoteFile copiedFile = file.copy(destinationFolder);
or
ApiClient apiClient =...;
RemoteFile file = ...;
RemoteFolder destinationFolder = ...
//Execute call immediately, call will block;
RemoteFile copiedFile = apiClient.copyFile(file, destinationFolder).execute();
// Execute call asynchronously
apiClient.copyFile(file, destinationFolder).enqueue(new Callback<RemoteFile>() {
@Override
public void onResponse(Call<RemoteFile> call, RemoteFile copiedFile) {
// Successful response
}
@Override
public void onFailure(Call<RemoteFile> call, Throwable t) {
// Call failed with an error.
}
});
ApiClient apiClient =...;
File localFile = ...;
RemoteFile uploadedFile = apiClient.createFile(
RemoteFolder.ROOT_FOLDER_ID,
localFile.getName(),
DataSource.create(localFile),
new Date(localFile.lastModified())
.execute();
ApiClient apiClient =...;
File localFile = ...;
ProgressListener listener = new ProgressListener() {
public void onProgress(long done, long total) {
System.out.format("\rUploading... %.1f\n", ((double) done / (double) total) * 100d);
}
};
RemoteFile uploadedFile = apiClient.createFile(
RemoteFolder.ROOT_FOLDER_ID,
localFile.getName(),
DataSource.create(localFile),
new Date(localFile.lastModified(),
listener)
.execute();
RemoteFile remoteFile = ...;
File localFolder = ...;
File localFile = new File(localFolder, remoteFile.name());
remoteFile.download(DataSink.create(localFile));
RemoteFile remoteFile = ...;
File localFolder = ...;
File localFile = new File(localFolder, remoteFile.name());
ProgressListener listener = new ProgressListener() {
public void onProgress(long done, long total) {
System.out.format("\Downloading... %.1f\n", ((double) done / (double) total) * 100d);
}
};
remoteFile.download(DataSink.create(localFile), listener);
RemoteFile remoteFile = ...;
remoteFile.download(new DataSink() {
@Override
public void readAll(BufferedSource source) throws IOException {
// Read bytes from source.
source.readByte();
// Read bytes with an InputStream:
source.inputStream().read();
}});
See the code in the sample
module or the sample application here. The sample explains briefly:
sample-android
module or the sample application here.#License 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.