Example usage for com.squareup.okhttp Response code

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

Introduction

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

Prototype

int code

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

Click Source Link

Usage

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

License:Apache License

@Override
public CFile updateFile(@NonNull CFile file, File content) throws RequestFailException {
    if (TextUtils.isEmpty(mAccessToken)) {
        throw new RequestFailException("Access token not available");
    }//w  ww  . j a  v a  2 s .  c  o m

    Uri uri = Uri.parse(mContentUrl);
    String url = uri.buildUpon().appendEncodedPath("nodes/" + file.getId() + "/content").build().toString();

    // create multipart body
    MediaType fileType = MediaType.parse(FilesUtils.getFileType(content));
    RequestBody multipart = new MultipartBuilder().type(MultipartBuilder.FORM)
            .addFormDataPart("content", file.getName(), RequestBody.create(fileType, content)).build();

    Request request = new Request.Builder().url(url)
            .header("Authorization", String.format("Bearer %s", mAccessToken)).put(multipart).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.CloudDriveApi.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 .jav a 2 s  .  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("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());
        }
    };

    Uri uri = Uri.parse(mMetadataUrl);
    String url = uri.buildUpon().appendEncodedPath("nodes/" + file.getId()).build().toString();

    Request request = new Request.Builder().url(url)
            .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.CloudDriveApi.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   w ww.j a v a  2 s.  c om*/

    // create parameter as json
    final JSONObject params = new JSONObject();
    try {
        ArrayList<String> parentList = new ArrayList<>();
        parentList.add(folder != null ? folder.getId() : getRoot().getId());
        params.put("parents", new JSONArray(parentList));
    } 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());
        }
    };

    Uri uri = Uri.parse(mMetadataUrl);
    String url = uri.buildUpon().appendEncodedPath("nodes/" + file.getId()).build().toString();

    Request request = new Request.Builder().url(url)
            .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.CloudDriveApi.java

License:Apache License

@Override
public File downloadFile(@NonNull CFile file, String filename) throws RequestFailException {
    if (TextUtils.isEmpty(mAccessToken)) {
        throw new RequestFailException("Access token not available");
    }/*from  w w  w.  ja v  a2  s.c om*/

    Uri uri = Uri.parse(mContentUrl);
    String url = uri.buildUpon().appendEncodedPath("nodes/" + file.getId() + "/content").build().toString();

    Request request = new Request.Builder().url(url)
            .header("Authorization", String.format("Bearer %s", mAccessToken)).get().build();

    try {
        File localFile = new File(mContext.getFilesDir(),
                TextUtils.isEmpty(filename) ? file.getName() : filename);

        Response response = mHttpClient.newCall(request).execute();
        if (response.isSuccessful()) {
            FilesUtils.copyFile(response.body().byteStream(), new FileOutputStream(localFile));
        } else {
            throw new RequestFailException(response.message(), response.code());
        }
        return localFile;
    } catch (IOException e) {
        e.printStackTrace();
        throw new RequestFailException(e.getMessage());
    }
}

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

License:Apache License

@Override
public void deleteFile(@NonNull CFile file) throws RequestFailException {
    if (TextUtils.isEmpty(mAccessToken)) {
        throw new RequestFailException("Access token not available");
    }//from  w  w w.j a  v a 2s. c o m

    Uri uri = Uri.parse(mMetadataUrl);
    String url = uri.buildUpon().appendEncodedPath("trash/" + file.getId()).build().toString();

    // create parameter as json
    final JSONObject params = new JSONObject();
    try {
        params.put("kind", "FOLDER");
    } 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(url)
            .header("Authorization", String.format("Bearer %s", mAccessToken)).put(body).build();

    try {
        Response response = mHttpClient.newCall(request).execute();
        if (response.isSuccessful()) {
            Log.d(TAG, "File with the id: " + file.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.CloudDriveApi.java

License:Apache License

@Override
public File getThumbnail(@NonNull CFile file) throws RequestFailException {
    if (TextUtils.isEmpty(mAccessToken)) {
        throw new RequestFailException("Access token not available");
    }//from  w w w.ja va 2s.c  o  m

    Uri uri = Uri.parse(mMetadataUrl);
    String url = uri.buildUpon().appendEncodedPath("nodes/" + file.getId()).appendQueryParameter("asset", "ALL")
            .appendQueryParameter("tempLink", "true").build().toString();

    Request request = new Request.Builder().url(url)
            .header("Authorization", String.format("Bearer %s", mAccessToken)).get().build();

    try {
        Response response = mHttpClient.newCall(request).execute();
        if (response.isSuccessful()) {
            JSONObject jsonObject = new JSONObject(response.body().string());
            if (jsonObject.has("assets")) {
                JSONArray assets = jsonObject.getJSONArray("assets");
                JSONObject asset = assets.getJSONObject(0);
                if (asset.has("tempLink")) {
                    String fileUrl = asset.getString("tempLink");
                    String filename = asset.getString("name");
                    return downloadThumbnail(fileUrl, filename);
                }
                return null;
            } else {
                return null;
            }
        } else {
            throw new RequestFailException(response.message(), response.code());
        }
    } catch (IOException e) {
        e.printStackTrace();
        throw new RequestFailException(e.getMessage());
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return null;
}

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

License:Apache License

/**
 * Download thumbnail via the temperary url
 *
 * @param url//from  w w w .  j av a2  s  .  c o  m
 * @param filename
 * @return
 * @throws RequestFailException
 */
private File downloadThumbnail(@NonNull String url, String filename) throws RequestFailException {

    Request request = new Request.Builder().url(url).get().build();

    try {
        File localFile = new File(mContext.getFilesDir(), filename);

        Response response = mHttpClient.newCall(request).execute();
        if (response.isSuccessful()) {
            FilesUtils.copyFile(response.body().byteStream(), new FileOutputStream(localFile));
        } else {
            throw new RequestFailException(response.message(), response.code());
        }
        return localFile;
    } catch (IOException e) {
        e.printStackTrace();
        throw new RequestFailException(e.getMessage());
    }
}

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

License:Apache License

/**
 * Ensure that the access token is still valid
 * Access token can be expired or revoked by user
 * Try to refresh the access token if it is expired
 *///from   w  ww.j  a  va 2s  .  c  o  m
private void validateAccessToken() {
    Request request = getUserInfoRequest(mAccessToken);

    mHttpClient.newCall(request).enqueue(new Callback() {
        @Override
        public void onFailure(Request request, IOException e) {
            e.printStackTrace();
            if (mPrepareListener != null)
                mPrepareListener.onPrepareFail(e);
            Log.e(TAG, e.getMessage());
        }

        @Override
        public void onResponse(Response response) throws IOException {
            if (response.isSuccessful() && response.code() == 200) {
                if (mPrepareListener != null)
                    mPrepareListener.onPrepareSuccessful();
            } else {
                switch (response.code()) {
                case 401:
                    // unauthorized
                    resetAccount();
                    break;
                default:
                    break;
                }
                Log.e(TAG, response.code() + ": " + response.body().string());
            }
        }
    });
}

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   ww  w. j  a  v  a 2 s . co  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//from  w  w  w . j a va2s .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());
    }
}