List of usage examples for com.squareup.okhttp RequestBody RequestBody
RequestBody
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"); }//from w w w . j av a 2 s.c o 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 va2s . com // 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()); } }
From source file:com.he5ed.lib.cloudprovider.apis.DropboxApi.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"); }//www . j a va 2 s .co m // create parameter as json final JSONObject params = new JSONObject(); try { params.put("from_path", file.getPath()); params.put("to_path", (folder != null ? folder.getPath() : getRoot().getPath()) + "/" + file.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 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.DropboxApi.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"); }/*w w w. j a v a 2 s .c o m*/ // create parameter as json final JSONObject params = new JSONObject(); try { params.put("path", file.getId()); } catch (JSONException e) { e.printStackTrace(); throw new RequestFailException(e.getMessage()); } // need to create blank body to use post method RequestBody body = new RequestBody() { @Override public MediaType contentType() { return null; } @Override public void writeTo(BufferedSink sink) throws IOException { } }; Request request = new Request.Builder().url(API_CONTENT_URL + "/files/download") .header("Authorization", String.format("Bearer %s", mAccessToken)) .header("Dropbox-API-Arg", params.toString()).post(body).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.DropboxApi.java
License:Apache License
@Override public void deleteFile(@NonNull CFile file) throws RequestFailException { if (TextUtils.isEmpty(mAccessToken)) { throw new RequestFailException("Access token not available"); }// www . jav a2 s. c om // create parameter as json final JSONObject params = new JSONObject(); try { params.put("path", file.getPath()); } 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/delete") .header("Authorization", String.format("Bearer %s", mAccessToken)).post(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.DropboxApi.java
License:Apache License
/** * Search the cloud for folder//from w ww.jav a 2 s.c o m * * @param params for search query * @return list of files and folders that match search criteria * @throws RequestFailException */ public List<Object> search(@NonNull Map<String, Object> params) throws RequestFailException { List<Object> list = new ArrayList<>(); // create parameter as json final JSONObject jsonParams = new JSONObject(); try { jsonParams.put("start", 0); jsonParams.put("max_results", 100); jsonParams.put("mode", "filename"); for (Map.Entry<String, Object> param : params.entrySet()) { jsonParams.put(param.getKey(), param.getValue()); } } 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(jsonParams.toString()); } }; Request request = new Request.Builder().url(API_BASE_URL + "/files/search") .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("matches"); JSONArray filterEntries = new JSONArray(); for (int i = 0; i < entries.length(); i++) { filterEntries.put(entries.getJSONObject(i).getJSONObject("metadata")); } if (filterEntries.length() > 0) { list.addAll(createItemList(filterEntries)); } else { // return null if no item found return null; } // expect more search result if (jsonObject.getBoolean("more")) { int start = jsonObject.getInt("start"); params.put("start", start); list.addAll(search(params)); } 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 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*/ // create parameter as json final JSONObject params = new JSONObject(); try { params.put("path", file.getId()); params.put("format", "jpeg"); params.put("size", "w128h128"); } catch (JSONException e) { e.printStackTrace(); throw new RequestFailException(e.getMessage()); } // need to create blank body to use post method RequestBody body = new RequestBody() { @Override public MediaType contentType() { return null; } @Override public void writeTo(BufferedSink sink) throws IOException { } }; Request request = new Request.Builder().url(API_CONTENT_URL + "/files/get_thumbnail") .header("Authorization", String.format("Bearer %s", mAccessToken)) .header("Dropbox-API-Arg", params.toString()).post(body).build(); try { File localFile = new File(mContext.getFilesDir(), file.getId() + ".jpg"); 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.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"); }// www . jav a 2s . co 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"); }/*ww w . 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 a v 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()); } }