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.CloudDriveApi.java
License:Apache License
/** * Try to get a fresh access token using the refresh token *//*from ww w.ja v a2 s .com*/ private void refreshAccessToken() { final String refreshToken = mCloudProvider.getUserData(mAccount, Authenticator.KEY_REFRESH_TOKEN); if (!TextUtils.isEmpty(refreshToken)) { Request request = new Request.Builder().url(TOKEN_URL).post(getRefreshTokenBody(refreshToken)).build(); mHttpClient.newCall(request).enqueue(new Callback() { @Override public void onFailure(Request request, IOException e) { e.printStackTrace(); Log.e(TAG, e.getMessage()); resetAccount(); } @Override public void onResponse(Response response) throws IOException { if (response.isSuccessful()) { // convert string into json try { JSONObject jsonObject = new JSONObject(response.body().string()); Map<String, String> tokenInfo = extractAccessToken(jsonObject); mCloudProvider.updateAccount(mAccount, tokenInfo); mAccessToken = tokenInfo.get(Authenticator.KEY_ACCESS_TOKEN); // validate again validateAccessToken(); } catch (JSONException e) { e.printStackTrace(); Log.e(TAG, e.getMessage()); } } else { Log.e(TAG, response.code() + ": " + response.body().string()); resetAccount(); } } }); } else { resetAccount(); } }
From source file:com.he5ed.lib.cloudprovider.apis.CloudDriveApi.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 www . j a v a2 s. com // if folder id is empty set it to root id if (TextUtils.isEmpty(folder.getId())) folder.setId(getRootId()); List<Object> list = new ArrayList<>(); String folderId = folder.getId(); Uri uri = Uri.parse(mMetadataUrl); String url = uri.buildUpon().appendEncodedPath("nodes/" + folderId + "/children").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()); int total = jsonObject.getInt("count"); // return null if no item found if (total == 0) return null; JSONArray entries = jsonObject.getJSONArray("data"); list.addAll(createItemList(entries)); // pagination available if (jsonObject.has("nextToken")) { list.addAll(exploreFolderContinue(folderId, jsonObject.getString("nextToken"))); } return list; } else { switch (response.code()) { case 404: // no item found throw new RequestFailException("No item found"); case 401: // unauthorized throw new RequestFailException("Unauthorized request"); default: break; } } } catch (JSONException e) { e.printStackTrace(); throw new RequestFailException(e.getMessage()); } 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
/** * Get continue folder items/*from w w w .j ava 2 s .co m*/ * * @param folderId of the folder to explore * @param startToken nextToken from previous request for access more content * @return List that contains CFile and CFolder * @throws RequestFailException that content various error types */ public synchronized List<Object> exploreFolderContinue(String folderId, String startToken) throws RequestFailException { if (TextUtils.isEmpty(mAccessToken)) { throw new RequestFailException("Access token not available"); } List<Object> list = new ArrayList<>(); Uri uri = Uri.parse(mMetadataUrl); String url = uri.buildUpon().appendEncodedPath("nodes/" + folderId + "/children") .appendQueryParameter("startToken", startToken).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) { list.addAll(createItemList(entries)); } else { // return null if no item found return null; } // pagination available if (jsonObject.has("nextToken")) { list.addAll(exploreFolderContinue(folderId, jsonObject.getString("nextToken"))); } 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.CloudDriveApi.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 w ww . j a v a 2s.c o m*/ Uri uri = Uri.parse(mMetadataUrl); String url = uri.buildUpon().appendEncodedPath("nodes/" + folderId).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 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.CloudDriveApi.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 ww w . j av a 2 s.com*/ Uri uri = Uri.parse(mMetadataUrl); String url = uri.buildUpon().appendEncodedPath("nodes/").build().toString(); // create parameter as json final JSONObject params = new JSONObject(); try { params.put("name", name); params.put("kind", "FOLDER"); 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()); } 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(url) .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.CloudDriveApi.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"); }//from ww w .j av a2s.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()); } }; Uri uri = Uri.parse(mMetadataUrl); String url = uri.buildUpon().appendEncodedPath("nodes/" + folder.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 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.CloudDriveApi.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 ww . j a v a2 s.c om // 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 { 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()); } 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/" + folder.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 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.CloudDriveApi.java
License:Apache License
@Override public void deleteFolder(@NonNull CFolder folder) throws RequestFailException { if (TextUtils.isEmpty(mAccessToken)) { throw new RequestFailException("Access token not available"); }//from w w w . ja v a 2s. c om Uri uri = Uri.parse(mMetadataUrl); String url = uri.buildUpon().appendEncodedPath("trash/" + folder.getId()).build().toString(); // create parameter as json final JSONObject params = new JSONObject(); try { params.put("kind", "FOLDER"); } 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(url) .header("Authorization", String.format("Bearer %s", mAccessToken)).put(body).build(); try { Response response = mHttpClient.newCall(request).execute(); if (response.isSuccessful()) { Log.d(TAG, "CFolder with the id: " + folder.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.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"); }//from w w w. j a v a2 s . c o m 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 a2 s . 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()); } }