Example usage for com.squareup.okhttp Response body

List of usage examples for com.squareup.okhttp Response body

Introduction

In this page you can find the example usage for com.squareup.okhttp Response body.

Prototype

ResponseBody body

To view the source code for com.squareup.okhttp Response body.

Click Source Link

Usage

From source file:com.he5ed.lib.cloudprovider.apis.DropboxApi.java

License:Apache License

@Override
public synchronized List<Object> exploreFolder(@NonNull CFolder folder, int offset)
        throws RequestFailException {
    if (TextUtils.isEmpty(mAccessToken)) {
        throw new RequestFailException("Access token not available");
    }//from w  w  w .  jav  a 2s.c o m

    List<Object> list = new ArrayList<>();

    // create parameter as json
    final JSONObject params = new JSONObject();
    try {
        params.put("path", folder.getPath());
        params.put("recursive", false);
        params.put("include_media_info", false);
        params.put("include_deleted", false);
    } catch (JSONException e) {
        e.printStackTrace();
        throw new RequestFailException(e.getMessage());
    }

    RequestBody body = new RequestBody() {
        @Override
        public MediaType contentType() {
            return JSON;
        }

        @Override
        public void writeTo(BufferedSink sink) throws IOException {
            sink.writeUtf8(params.toString());
        }
    };

    Request request = new Request.Builder().url(API_BASE_URL + "/files/list_folder")
            .header("Authorization", String.format("Bearer %s", mAccessToken)).post(body).build();

    try {
        Response response = mHttpClient.newCall(request).execute();
        if (response.isSuccessful()) {
            JSONObject jsonObject = new JSONObject(response.body().string());
            JSONArray entries = jsonObject.getJSONArray("entries");
            if (entries.length() > 0) {
                list.addAll(createItemList(entries));
            } else {
                // return null if no item found
                return null;
            }
            // expect more search result
            if (jsonObject.getBoolean("has_more")) {
                list.addAll(exploreFolderContinue(jsonObject.getString("cursor")));
            }
            return list;
        } else {
            throw new RequestFailException(response.message(), response.code());
        }
    } catch (JSONException e) {
        e.printStackTrace();
        throw new RequestFailException(e.getMessage());
    } catch (IOException e) {
        e.printStackTrace();
        throw new RequestFailException(e.getMessage());
    }
}

From source file:com.he5ed.lib.cloudprovider.apis.DropboxApi.java

License:Apache License

/**
 * Get continue folder items//  w w  w .j  a v  a 2 s.  c o m
 *
 * @param cursor id from the previous request
 * @return List that contains CFile and CFolder
 * @throws RequestFailException that content various error types
 */
public synchronized List<Object> exploreFolderContinue(String cursor) throws RequestFailException {
    if (TextUtils.isEmpty(mAccessToken)) {
        throw new RequestFailException("Access token not available");
    }

    List<Object> list = new ArrayList<>();

    // create parameter as json
    final JSONObject params = new JSONObject();
    try {
        params.put("cursor", cursor);
    } catch (JSONException e) {
        e.printStackTrace();
        throw new RequestFailException(e.getMessage());
    }

    RequestBody body = new RequestBody() {
        @Override
        public MediaType contentType() {
            return JSON;
        }

        @Override
        public void writeTo(BufferedSink sink) throws IOException {
            sink.writeUtf8(params.toString());
        }
    };

    Request request = new Request.Builder().url(API_BASE_URL + "/files/list_folder/continue")
            .header("Authorization", String.format("Bearer %s", mAccessToken)).post(body).build();

    try {
        Response response = mHttpClient.newCall(request).execute();
        if (response.isSuccessful()) {
            JSONObject jsonObject = new JSONObject(response.body().string());
            JSONArray entries = jsonObject.getJSONArray("entries");
            if (entries.length() > 0) {
                list.addAll(createItemList(entries));
            } else {
                // return null if no item found
                return null;
            }
            // expect more search result
            if (jsonObject.getBoolean("has_more")) {
                list.addAll(exploreFolderContinue(jsonObject.getString("cursor")));
            }
            return list;
        } else {
            throw new RequestFailException(response.message(), response.code());
        }
    } catch (JSONException e) {
        e.printStackTrace();
        throw new RequestFailException(e.getMessage());
    } catch (IOException e) {
        e.printStackTrace();
        throw new RequestFailException(e.getMessage());
    }
}

From source file:com.he5ed.lib.cloudprovider.apis.DropboxApi.java

License:Apache License

@Override
public CFolder getFolderInfo(@NonNull String folderId) throws RequestFailException {
    if (TextUtils.isEmpty(mAccessToken)) {
        throw new RequestFailException("Access token not available");
    }//  www  .j  av  a 2 s  .  c  o  m

    // create parameter as json
    final JSONObject params = new JSONObject();
    try {
        params.put("path", folderId);
        params.put("include_media_info", false);
    } catch (JSONException e) {
        e.printStackTrace();
        throw new RequestFailException(e.getMessage());
    }

    RequestBody body = new RequestBody() {
        @Override
        public MediaType contentType() {
            return JSON;
        }

        @Override
        public void writeTo(BufferedSink sink) throws IOException {
            sink.writeUtf8(params.toString());
        }
    };

    Request request = new Request.Builder().url(API_BASE_URL + "/files/get_metadata")
            .header("Authorization", String.format("Bearer %s", mAccessToken)).post(body).build();

    try {
        Response response = mHttpClient.newCall(request).execute();
        if (response.isSuccessful()) {
            JSONObject jsonObject = new JSONObject(response.body().string());
            return buildFolder(jsonObject);
        } else {
            throw new RequestFailException(response.message(), response.code());
        }
    } catch (JSONException e) {
        e.printStackTrace();
        throw new RequestFailException(e.getMessage());
    } catch (IOException e) {
        e.printStackTrace();
        throw new RequestFailException(e.getMessage());
    }
}

From source file:com.he5ed.lib.cloudprovider.apis.DropboxApi.java

License:Apache License

@Override
public CFolder createFolder(@NonNull String name, @Nullable CFolder parent) throws RequestFailException {
    if (TextUtils.isEmpty(mAccessToken)) {
        throw new RequestFailException("Access token not available");
    }/*from   w ww  .  j a  v a 2 s  .  c o m*/

    // create parameter as json
    final JSONObject params = new JSONObject();
    try {
        params.put("path", (parent != null ? parent.getPath() : getRoot().getPath()) + "/" + name);
    } catch (JSONException e) {
        e.printStackTrace();
        throw new RequestFailException(e.getMessage());
    }

    RequestBody body = new RequestBody() {
        @Override
        public MediaType contentType() {
            return JSON;
        }

        @Override
        public void writeTo(BufferedSink sink) throws IOException {
            sink.writeUtf8(params.toString());
        }
    };

    Request request = new Request.Builder().url(API_BASE_URL + "/files/create_folder")
            .header("Authorization", String.format("Bearer %s", mAccessToken)).post(body).build();

    try {
        Response response = mHttpClient.newCall(request).execute();
        if (response.isSuccessful()) {
            JSONObject jsonObject = new JSONObject(response.body().string());
            return buildFolder(jsonObject);
        } else {
            throw new RequestFailException(response.message(), response.code());
        }
    } catch (JSONException e) {
        e.printStackTrace();
        throw new RequestFailException(e.getMessage());
    } catch (IOException e) {
        e.printStackTrace();
        throw new RequestFailException(e.getMessage());
    }
}

From source file:com.he5ed.lib.cloudprovider.apis.DropboxApi.java

License:Apache License

@Override
public CFolder renameFolder(@NonNull CFolder folder, String name) throws RequestFailException {
    if (TextUtils.isEmpty(mAccessToken)) {
        throw new RequestFailException("Access token not available");
    }/*from  w w w . j av  a 2s .c om*/

    // exit if root or same name
    if (folder.isRoot() || folder.getName().equals(name))
        return folder;

    // create parameter as json
    final JSONObject params = new JSONObject();
    try {
        params.put("from_path", folder.getPath());
        params.put("to_path", renameLastPathSegment(folder.getPath(), name));
    } catch (JSONException e) {
        e.printStackTrace();
        throw new RequestFailException(e.getMessage());
    }

    RequestBody body = new RequestBody() {
        @Override
        public MediaType contentType() {
            return JSON;
        }

        @Override
        public void writeTo(BufferedSink sink) throws IOException {
            sink.writeUtf8(params.toString());
        }
    };

    Request request = new Request.Builder().url(API_BASE_URL + "/files/move")
            .header("Authorization", String.format("Bearer %s", mAccessToken)).post(body).build();

    try {
        Response response = mHttpClient.newCall(request).execute();
        if (response.isSuccessful()) {
            // return folder object
            return buildFolder(new JSONObject(response.body().string()));
        } else {
            throw new RequestFailException(response.message(), response.code());
        }
    } catch (JSONException e) {
        e.printStackTrace();
        throw new RequestFailException(e.getMessage());
    } catch (IOException e) {
        e.printStackTrace();
        throw new RequestFailException(e.getMessage());
    }
}

From source file:com.he5ed.lib.cloudprovider.apis.DropboxApi.java

License:Apache License

@Override
public CFolder moveFolder(@NonNull CFolder folder, @Nullable CFolder parent) throws RequestFailException {
    if (TextUtils.isEmpty(mAccessToken)) {
        throw new RequestFailException("Access token not available");
    }/*w ww. java  2 s  .  c  o  m*/

    // exit if root or same name
    if (folder.isRoot() || parent != null && folder.getId().equals(parent.getId()))
        return folder;

    // create parameter as json
    final JSONObject params = new JSONObject();
    try {
        params.put("from_path", folder.getPath());
        params.put("to_path", renameLastPathSegment((parent != null ? parent.getPath() : getRoot().getPath()),
                folder.getName()));
    } catch (JSONException e) {
        e.printStackTrace();
        throw new RequestFailException(e.getMessage());
    }

    RequestBody body = new RequestBody() {
        @Override
        public MediaType contentType() {
            return JSON;
        }

        @Override
        public void writeTo(BufferedSink sink) throws IOException {
            sink.writeUtf8(params.toString());
        }
    };

    Request request = new Request.Builder().url(API_BASE_URL + "/files/move")
            .header("Authorization", String.format("Bearer %s", mAccessToken)).post(body).build();

    try {
        Response response = mHttpClient.newCall(request).execute();
        if (response.isSuccessful()) {
            // return folder object
            return buildFolder(new JSONObject(response.body().string()));
        } else {
            throw new RequestFailException(response.message(), response.code());
        }
    } catch (JSONException e) {
        e.printStackTrace();
        throw new RequestFailException(e.getMessage());
    } catch (IOException e) {
        e.printStackTrace();
        throw new RequestFailException(e.getMessage());
    }
}

From source file:com.he5ed.lib.cloudprovider.apis.DropboxApi.java

License:Apache License

@Override
public CFile getFileInfo(@NonNull String fileId) throws RequestFailException {
    if (TextUtils.isEmpty(mAccessToken)) {
        throw new RequestFailException("Access token not available");
    }/*  w  w w  .  j av  a2  s  .  c o m*/

    // create parameter as json
    final JSONObject params = new JSONObject();
    try {
        params.put("path", fileId);
        params.put("include_media_info", false);
    } catch (JSONException e) {
        e.printStackTrace();
        throw new RequestFailException(e.getMessage());
    }

    RequestBody body = new RequestBody() {
        @Override
        public MediaType contentType() {
            return JSON;
        }

        @Override
        public void writeTo(BufferedSink sink) throws IOException {
            sink.writeUtf8(params.toString());
        }
    };

    Request request = new Request.Builder().url(API_BASE_URL + "/files/get_metadata")
            .header("Authorization", String.format("Bearer %s", mAccessToken)).post(body).build();

    try {
        Response response = mHttpClient.newCall(request).execute();
        if (response.isSuccessful()) {
            JSONObject jsonObject = new JSONObject(response.body().string());
            return buildFile(jsonObject);
        } else {
            throw new RequestFailException(response.message(), response.code());
        }
    } catch (JSONException e) {
        e.printStackTrace();
        throw new RequestFailException(e.getMessage());
    } catch (IOException e) {
        e.printStackTrace();
        throw new RequestFailException(e.getMessage());
    }
}

From source file:com.he5ed.lib.cloudprovider.apis.DropboxApi.java

License:Apache License

@Override
public CFile uploadFile(@NonNull final File file, @Nullable CFolder parent) throws RequestFailException {
    if (TextUtils.isEmpty(mAccessToken)) {
        throw new RequestFailException("Access token not available");
    }/*from   w  ww .j av a2 s  . c o m*/
    // create parameter as json
    final JSONObject params = new JSONObject();
    try {
        params.put("path", (parent != null ? parent.getPath() : getRoot().getPath()) + "/" + file.getName());
        params.put("mode", "add");
        params.put("autorename", false);
        params.put("mute", false);
    } catch (JSONException e) {
        e.printStackTrace();
        throw new RequestFailException(e.getMessage());
    }

    RequestBody fileBody = new RequestBody() {
        @Override
        public MediaType contentType() {
            return null;
        }

        @Override
        public void writeTo(BufferedSink sink) throws IOException {
            // copy file into RequestBody
            FilesUtils.copyFile(new FileInputStream(file), sink.outputStream());
        }
    };

    Request request = new Request.Builder().url(API_CONTENT_URL + "/files/upload ")
            .header("Authorization", String.format("Bearer %s", mAccessToken))
            .header("Dropbox-API-Arg", params.toString()).header("Content-Type", "application/octet-stream")
            .post(fileBody).build();

    try {
        Response response = mHttpClient.newCall(request).execute();
        if (response.isSuccessful()) {
            JSONObject jsonObject = new JSONObject(response.body().string());
            return buildFile(jsonObject);
        } else {
            throw new RequestFailException(response.message(), response.code());
        }
    } catch (JSONException e) {
        e.printStackTrace();
        throw new RequestFailException(e.getMessage());
    } catch (IOException e) {
        e.printStackTrace();
        throw new RequestFailException(e.getMessage());
    }
}

From source file:com.he5ed.lib.cloudprovider.apis.DropboxApi.java

License:Apache License

@Override
public CFile updateFile(@NonNull CFile file, final File content) throws RequestFailException {
    if (TextUtils.isEmpty(mAccessToken)) {
        throw new RequestFailException("Access token not available");
    }/*  w  w  w  . ja  v a  2  s . co  m*/
    // create parameter as json
    final JSONObject params = new JSONObject();
    try {
        params.put("path", file.getPath());
        params.put("mode", "overwrite");
        params.put("autorename", false);
        params.put("mute", false);
    } catch (JSONException e) {
        e.printStackTrace();
        throw new RequestFailException(e.getMessage());
    }

    RequestBody fileBody = new RequestBody() {
        @Override
        public MediaType contentType() {
            return null;
        }

        @Override
        public void writeTo(BufferedSink sink) throws IOException {
            // copy file into RequestBody
            FilesUtils.copyFile(new FileInputStream(content), sink.outputStream());
        }
    };

    Request request = new Request.Builder().url(API_CONTENT_URL + "/files/upload ")
            .header("Authorization", String.format("Bearer %s", mAccessToken))
            .header("Dropbox-API-Arg", params.toString()).header("Content-Type", "application/octet-stream")
            .post(fileBody).build();

    try {
        Response response = mHttpClient.newCall(request).execute();
        if (response.isSuccessful()) {
            JSONObject jsonObject = new JSONObject(response.body().string());
            return buildFile(jsonObject);
        } else {
            throw new RequestFailException(response.message(), response.code());
        }
    } catch (JSONException e) {
        e.printStackTrace();
        throw new RequestFailException(e.getMessage());
    } catch (IOException e) {
        e.printStackTrace();
        throw new RequestFailException(e.getMessage());
    }
}

From source file:com.he5ed.lib.cloudprovider.apis.DropboxApi.java

License:Apache License

@Override
public CFile renameFile(@NonNull CFile file, String name) throws RequestFailException {
    if (TextUtils.isEmpty(mAccessToken)) {
        throw new RequestFailException("Access token not available");
    }/*  w ww .  j  a v a  2s.  co  m*/

    // exist if same filename
    if (file.getName().equals(name))
        return file;

    // create parameter as json
    final JSONObject params = new JSONObject();
    try {
        params.put("from_path", file.getPath());
        params.put("to_path", renameLastPathSegment(file.getPath(), name));
    } catch (JSONException e) {
        e.printStackTrace();
        throw new RequestFailException(e.getMessage());
    }

    RequestBody body = new RequestBody() {
        @Override
        public MediaType contentType() {
            return JSON;
        }

        @Override
        public void writeTo(BufferedSink sink) throws IOException {
            sink.writeUtf8(params.toString());
        }
    };

    Request request = new Request.Builder().url(API_BASE_URL + "/files/move")
            .header("Authorization", String.format("Bearer %s", mAccessToken)).post(body).build();

    try {
        Response response = mHttpClient.newCall(request).execute();
        if (response.isSuccessful()) {
            // return folder object
            return buildFile(new JSONObject(response.body().string()));
        } else {
            throw new RequestFailException(response.message(), response.code());
        }
    } catch (JSONException e) {
        e.printStackTrace();
        throw new RequestFailException(e.getMessage());
    } catch (IOException e) {
        e.printStackTrace();
        throw new RequestFailException(e.getMessage());
    }
}