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.CloudDriveApi.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  a  2 s .  c om

    Uri uri = Uri.parse(mMetadataUrl);
    String url = uri.buildUpon().appendEncodedPath("nodes/" + fileId).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());
            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 uploadFile(@NonNull File file, @Nullable CFolder parent) throws RequestFailException {
    if (TextUtils.isEmpty(mAccessToken)) {
        throw new RequestFailException("Access token not available");
    }/*w  w  w. ja v  a 2s.c o  m*/

    Uri uri = Uri.parse(mContentUrl);
    String url = uri.buildUpon().appendEncodedPath("nodes").build().toString();

    // create parameter as json
    final JSONObject params = new JSONObject();
    try {
        params.put("name", file.getName());
        params.put("kind", "FILE");
        ArrayList<String> parentList = new ArrayList<>();
        parentList.add(parent != null ? parent.getId() : getRoot().getId());
        params.put("parents", new JSONArray(parentList));
    } catch (JSONException e) {
        e.printStackTrace();
        throw new RequestFailException(e.getMessage());
    }

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

    Request request = new Request.Builder().url(url)
            .header("Authorization", String.format("Bearer %s", mAccessToken)).post(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 updateFile(@NonNull CFile file, File content) throws RequestFailException {
    if (TextUtils.isEmpty(mAccessToken)) {
        throw new RequestFailException("Access token not available");
    }/*from w  w w  . j  av 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  w w .j  a  va  2  s .  c  o  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");
    }//w w w  . ja v a2  s .  c o  m

    // 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 . j  av a2 s  . c o  m

    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 File getThumbnail(@NonNull CFile file) throws RequestFailException {
    if (TextUtils.isEmpty(mAccessToken)) {
        throw new RequestFailException("Access token not available");
    }//from   w  ww.j  a va2  s  .co  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   ww  w.j  av a  2  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.CloudDriveApi.java

License:Apache License

/**
 * Get root folder id/*from w  w  w. j a  va 2s .  c o  m*/
 *
 * @return root id as String
 */
private String getRootId() {
    Uri uri = Uri.parse(mMetadataUrl);
    String url = uri.buildUpon().appendEncodedPath("nodes")
            .appendQueryParameter("filters", "kind:FOLDER AND isRoot: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());
            JSONArray entries = jsonObject.getJSONArray("data");
            if (entries.length() > 0) {
                JSONObject root = entries.getJSONObject(0);
                return ROOT_ID = root.getString("id");
            }
        } else {
            Log.e(TAG, response.message());
        }
    } catch (JSONException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return null;
}

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  w  w. j av a 2  s  .  com*/
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());
            }
        }
    });
}