List of usage examples for com.squareup.okhttp Response isSuccessful
public boolean isSuccessful()
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 w w .j a v a 2 s . c om*/ // 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"); }//from w w w . j av a2s. 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 w w . j a v a 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("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"); }/*from w w w .j a v a 2 s .c om*/ // 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"); }//from w w w.j a v a2 s . c om // 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"); }/* w ww . j ava 2 s . c o m*/ // 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/* w w w . j a v a2 s . c om*/ * * @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 . j av a 2 s . c om // 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 synchronized List<Object> exploreFolder(@NonNull CFolder folder, int offset) throws RequestFailException { if (TextUtils.isEmpty(mAccessToken)) { throw new RequestFailException("Access token not available"); }/*w w w .ja v a 2 s . c o m*/ List<Object> list = new ArrayList<>(); Request request = new Request.Builder().url(API_BASE_URL + "/drive/items/" + folder.getId() + "/children") .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("value"); if (entries.length() > 0) { list.addAll(createFilteredItemsList(entries, null)); } else { // return null if no item found return null; } // pagination available if (jsonObject.has("@odata.nextLink")) { list.addAll(exploreFolderContinue(jsonObject.getString("@odata.nextLink"))); } 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.OneDriveApi.java
License:Apache License
/** * Get continue folder items/*www . j a v a 2s .c om*/ * * @param url for the next page of items * @return List that contains CFile and CFolder * @throws RequestFailException that content various error types */ public synchronized List<Object> exploreFolderContinue(String url) throws RequestFailException { if (TextUtils.isEmpty(mAccessToken)) { throw new RequestFailException("Access token not available"); } List<Object> list = new ArrayList<>(); 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) { list.addAll(createFilteredItemsList(entries, null)); } else { // return null if no item found return null; } // pagination available if (jsonObject.has("@odata.nextLink")) { list.addAll(exploreFolderContinue(jsonObject.getString("@odata.nextLink"))); } 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()); } }