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.BoxApi.java
License:Apache License
@Override public synchronized File downloadFile(@NonNull CFile file, @Nullable String filename) throws RequestFailException { if (TextUtils.isEmpty(mAccessToken)) { throw new RequestFailException("Access token not available"); }//from w ww . j a v a2 s .com // assign filename if (TextUtils.isEmpty(filename)) filename = file.getName(); String fileId = file.getId(); Request request = new Request.Builder().url(API_BASE_URL + "/files/" + fileId + "/content") .header("Authorization", String.format("Bearer %s", mAccessToken)).get().build(); try { Response response = mHttpClient.newCall(request).execute(); if (response.isSuccessful()) { switch (response.code()) { case 200: // redirect to url return downloadFile(response.request(), filename); case 202: // retry after due to file just uploaded delayDownloadFile(file, filename); break; case 302: // redirect to url return downloadFile(response.request(), filename); } } else { throw new RequestFailException(response.message(), response.code()); } } catch (IOException e) { e.printStackTrace(); throw new RequestFailException(e.getMessage()); } return null; }
From source file:com.he5ed.lib.cloudprovider.apis.BoxApi.java
License:Apache License
/** * Download file from redirect request/*from ww w.j ava2 s.c om*/ * * @param request for redirect * @return File * @throws RequestFailException */ private File downloadFile(@NonNull Request request, @NonNull String filename) throws RequestFailException { try { File file = new File(CloudProvider.CACHE_DIR, filename); Response response = mHttpClient.newCall(request).execute(); if (response.isSuccessful()) { if (FilesUtils.getInternalAvailableBytes() < response.body().contentLength()) { // insufficient storage space throw exception throw new RequestFailException("Insufficient storage"); } else { FilesUtils.copyFile(response.body().byteStream(), new FileOutputStream(file)); } } else { throw new RequestFailException(response.message(), response.code()); } return file; } catch (IOException e) { e.printStackTrace(); throw new RequestFailException(e.getMessage()); } }
From source file:com.he5ed.lib.cloudprovider.apis.BoxApi.java
License:Apache License
@Override public synchronized CFile uploadFile(@NonNull 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 . com // create parameter as json final JSONObject params = new JSONObject(); try { params.put("name", file.getName()); params.put("parent", new JSONObject().put("id", parent != null ? parent.getId() : getRoot().getId())); } 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("attributes", params.toString()) .addFormDataPart("file", file.getName(), RequestBody.create(fileType, file)).build(); Request request = new Request.Builder().url(API_UPLOAD_URL + "/files/content") .header("Authorization", String.format("Bearer %s", mAccessToken)).post(multipart).build(); try { Response response = mHttpClient.newCall(request).execute(); if (response.isSuccessful()) { // new file created JSONObject jsonObject = new JSONObject(response.body().string()); JSONArray entries = jsonObject.getJSONArray("entries"); return buildFile(entries.getJSONObject(0)); } 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.BoxApi.java
License:Apache License
@Override public synchronized CFile updateFile(@NonNull CFile file, File content) throws RequestFailException { if (TextUtils.isEmpty(mAccessToken)) { throw new RequestFailException("Access token not available"); }//from ww w .j a v a 2s . c o m // create multipart body MediaType fileType = MediaType.parse(FilesUtils.getFileType(content)); RequestBody multipart = new MultipartBuilder().type(MultipartBuilder.FORM) .addFormDataPart("file", content.getName(), RequestBody.create(fileType, content)).build(); Request request = new Request.Builder().url(API_UPLOAD_URL + "/files/" + file.getId() + "/content") .header("Authorization", String.format("Bearer %s", mAccessToken)).post(multipart).build(); try { Response response = mHttpClient.newCall(request).execute(); if (response.isSuccessful()) { // new file created JSONObject jsonObject = new JSONObject(response.body().string()); JSONArray entries = jsonObject.getJSONArray("entries"); return buildFile(entries.getJSONObject(0)); } 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.BoxApi.java
License:Apache License
@Override public synchronized CFile renameFile(@NonNull CFile file, String name) throws RequestFailException { if (TextUtils.isEmpty(mAccessToken)) { throw new RequestFailException("Access token not available"); }/*from ww 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()); } }; Request request = new Request.Builder().url(API_BASE_URL + "/files/" + file.getId()) .header("Authorization", String.format("Bearer %s", mAccessToken)).put(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.BoxApi.java
License:Apache License
@Override public synchronized CFile moveFile(@NonNull CFile file, @Nullable CFolder folder) throws RequestFailException { if (TextUtils.isEmpty(mAccessToken)) { throw new RequestFailException("Access token not available"); }//from ww w .j a v a 2s .c om // create parameter as json final JSONObject params = new JSONObject(); try { params.put("parent", new JSONObject().put("id", folder != null ? folder.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 + "/files/" + file.getId()) .header("Authorization", String.format("Bearer %s", mAccessToken)).put(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.BoxApi.java
License:Apache License
@Override public synchronized void deleteFile(@NonNull CFile file) throws RequestFailException { if (TextUtils.isEmpty(mAccessToken)) { throw new RequestFailException("Access token not available"); }// w w w .j a v a2s .com String fileId = file.getId(); Request request = new Request.Builder().url(API_BASE_URL + "/files/" + fileId) .header("Authorization", String.format("Bearer %s", mAccessToken)).delete().build(); try { Response response = mHttpClient.newCall(request).execute(); if (response.isSuccessful()) { Log.d(TAG, "File with the id: " + fileId + " 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.BoxApi.java
License:Apache License
/** * Search the cloud for all contents//from ww w . j av a2 s . co m * * @param params for search query * @param parent folder wto search for * @return list of files and folders that match search criteria * @throws RequestFailException */ public synchronized List<Object> search(@NonNull Map<String, Object> params, CFolder parent) throws RequestFailException { List<Object> list = new ArrayList<>(); Uri uri = Uri.parse(API_BASE_URL); Uri.Builder urlBuilder = uri.buildUpon().appendEncodedPath("search"); // pre-defined parameters urlBuilder.appendQueryParameter("limit", "100"); urlBuilder.appendQueryParameter("scope", "user_content"); // add the rest of the user defined parameters params.put("ancestor_folder_ids", parent.getId()); for (Map.Entry<String, Object> param : params.entrySet()) { urlBuilder.appendQueryParameter(param.getKey(), (String) param.getValue()); } Request request = new Request.Builder().url(urlBuilder.toString()) .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()); int total = jsonObject.getInt("total_count"); // return null if no item found if (total == 0) return null; JSONArray entries = jsonObject.getJSONArray("entries"); list.addAll(createFilteredItemsList(entries, parent)); // suspect search result over 100 items if (total > 100 && total - list.size() > 0) { params.put("offset", "100"); list.addAll(search(params, parent)); } 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.BoxApi.java
License:Apache License
@Override public synchronized File getThumbnail(@NonNull CFile file) throws RequestFailException { if (TextUtils.isEmpty(mAccessToken)) { throw new RequestFailException("Access token not available"); }/*from w ww . jav a 2 s . co m*/ Uri uri = Uri.parse(API_BASE_URL); String url = uri.buildUpon().appendEncodedPath("files/" + file.getId() + "/thumbnail.png") .appendQueryParameter("min_height", "100").appendQueryParameter("min_width", "100") .appendQueryParameter("max_height", "256").appendQueryParameter("max_width", "256").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()) { switch (response.code()) { case 200: // redirect to url return downloadFile(response.request(), file.getId() + ".png"); case 202: // retry after due to file just uploaded delayDownloadFile(file, file.getId() + ".png"); break; case 302: // redirect to url return downloadFile(response.request(), file.getId() + ".png"); } } else { throw new RequestFailException(response.message(), response.code()); } } catch (IOException e) { e.printStackTrace(); throw new RequestFailException(e.getMessage()); } return null; }
From source file:com.he5ed.lib.cloudprovider.apis.CloudDriveApi.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 a v a 2 s . co m*/ private void validateAccessToken() { Request request = getEndPointRequest(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) { try { JSONObject jsonObject = new JSONObject(response.body().string()); mContentUrl = jsonObject.getString("contentUrl"); mMetadataUrl = jsonObject.getString("metadataUrl"); if (mPrepareListener != null) mPrepareListener.onPrepareSuccessful(); } catch (JSONException e) { e.printStackTrace(); Log.e(TAG, e.getMessage()); if (mPrepareListener != null) mPrepareListener.onPrepareFail(e); } } else { switch (response.code()) { case 401: // unauthorized refreshAccessToken(); break; default: break; } Log.e(TAG, response.code() + ": " + response.body().string()); } } }); }