Example usage for com.squareup.okhttp Response isSuccessful

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

Introduction

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

Prototype

public boolean isSuccessful() 

Source Link

Document

Returns true if the code is in [200..300), which means the request was successfully received, understood, and accepted.

Usage

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

License:Apache License

@Override
public CFolder getFolderInfo(@NonNull String folderId) throws RequestFailException {
    if (TextUtils.isEmpty(mAccessToken)) {
        throw new RequestFailException("Access token not available");
    }/*w  w w . j a va2 s  . co  m*/

    if (TextUtils.isEmpty(folderId))
        return null;

    Request request = new Request.Builder().url(API_BASE_URL + "/drive/items/" + folderId)
            .header("Authorization", String.format("Bearer %s", mAccessToken)).get().build();

    try {
        Response response = mHttpClient.newCall(request).execute();
        if (response.isSuccessful()) {
            // new file created
            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.OneDriveApi.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 w  w. j  ava 2  s.c  o  m

    // create parameter as json
    final JSONObject params = new JSONObject();
    try {
        params.put("name", name);
        params.put("folder", new JSONObject());
        params.put("@name.conflictBehavior", "fail");
    } 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 + "/drive/items/" + (parent != null ? parent.getId() : getRoot().getId())
                    + "/children")
            .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.OneDriveApi.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");
    }/*  w ww. j a v a 2  s.c  o m*/

    // 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("name", 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 + "/drive/items/" + folder.getId())
            .header("Authorization", String.format("Bearer %s", mAccessToken)).patch(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.OneDriveApi.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");
    }//ww  w. j av a  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("parentReference",
                new JSONObject().put("id", parent != null ? parent.getId() : getRoot().getId()));
    } 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 + "/drive/items/" + folder.getId())
            .header("Authorization", String.format("Bearer %s", mAccessToken)).patch(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.OneDriveApi.java

License:Apache License

@Override
public void deleteFolder(@NonNull CFolder folder) throws RequestFailException {
    if (TextUtils.isEmpty(mAccessToken)) {
        throw new RequestFailException("Access token not available");
    }//w  ww. j  av a 2 s.  c  om

    Request request = new Request.Builder().url(API_BASE_URL + "/drive/items/" + folder.getId())
            .header("Authorization", String.format("Bearer %s", mAccessToken)).delete().build();

    try {
        Response response = mHttpClient.newCall(request).execute();
        if (response.isSuccessful()) {
            Log.d(TAG, "CFolder with the id: " + folder.getName() + " deleted");
        } else {
            throw new RequestFailException(response.message(), response.code());
        }
    } catch (IOException e) {
        e.printStackTrace();
        throw new RequestFailException(e.getMessage());
    }
}

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

License:Apache License

@Override
public CFile getFileInfo(@NonNull String fileId) throws RequestFailException {
    if (TextUtils.isEmpty(mAccessToken)) {
        throw new RequestFailException("Access token not available");
    }/*from www  .  jav a 2 s  .co m*/

    if (TextUtils.isEmpty(fileId))
        return null;

    Request request = new Request.Builder().url(API_BASE_URL + "/drive/items/" + fileId)
            .header("Authorization", String.format("Bearer %s", mAccessToken)).get().build();

    try {
        Response response = mHttpClient.newCall(request).execute();
        if (response.isSuccessful()) {
            // new file created
            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.OneDriveApi.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");
    }// w  ww . j av  a 2  s  . c om

    Uri uri = Uri.parse(API_BASE_URL);
    String url = uri.buildUpon()
            .appendEncodedPath("drive/items/" + (parent != null ? parent.getId() : getRoot().getId())
                    + "/children/" + file.getName() + "/content")
            .appendQueryParameter("@name.conflictBehavior", "fail").build().toString();

    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(url)
            .header("Authorization", String.format("Bearer %s", mAccessToken)).put(fileBody).build();

    try {
        Response response = mHttpClient.newCall(request).execute();
        if (response.isSuccessful()) {
            // new file created
            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.OneDriveApi.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");
    }/*from w ww  . jav  a  2  s.c om*/

    Uri uri = Uri.parse(API_BASE_URL);
    String url = uri.buildUpon().appendEncodedPath("drive/items/" + file.getId() + "/content")
            .appendQueryParameter("@name.conflictBehavior", "replace").build().toString();

    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(url)
            .header("Authorization", String.format("Bearer %s", mAccessToken)).put(fileBody).build();

    try {
        Response response = mHttpClient.newCall(request).execute();
        if (response.isSuccessful()) {
            // new file created
            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.OneDriveApi.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");
    }/*from  w w w.j a va2 s  . c  om*/

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

    // create parameter as json
    final JSONObject params = new JSONObject();
    try {
        params.put("name", 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 + "/drive/items/" + file.getId())
            .header("Authorization", String.format("Bearer %s", mAccessToken)).patch(body).build();

    try {
        Response response = mHttpClient.newCall(request).execute();
        if (response.isSuccessful()) {
            // return file 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());
    }
}

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

License:Apache License

@Override
public CFile moveFile(@NonNull CFile file, @Nullable CFolder folder) throws RequestFailException {
    if (TextUtils.isEmpty(mAccessToken)) {
        throw new RequestFailException("Access token not available");
    }//from ww  w  .j  a  va 2 s  .  c o  m

    // create parameter as json
    final JSONObject params = new JSONObject();
    try {
        params.put("parentReference",
                new JSONObject().put("id", folder != null ? folder.getId() : getRoot().getId()));
    } 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 + "/drive/items/" + file.getId())
            .header("Authorization", String.format("Bearer %s", mAccessToken)).patch(body).build();

    try {
        Response response = mHttpClient.newCall(request).execute();
        if (response.isSuccessful()) {
            // return file 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());
    }
}