List of usage examples for com.squareup.okhttp Response code
int code
To view the source code for com.squareup.okhttp Response code.
Click Source Link
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"); }/*ww w .j av a 2 s . co 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"); }//w ww . j a v a 2s . 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 w w. j a v 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. j a va 2s.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"); }/*from w ww .j a v a 2 s. c om*/ 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//from ww w.j a va2s . co m * * @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()); } }
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"); }/*from ww w . j a va2 s . com*/ 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 ww . j av a2 s . c om*/ // 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"); }/* www . ja v a 2 s .co 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"); }//from w w w. j a v a 2 s . com // 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()); } }