Example usage for com.squareup.okhttp Response isSuccessful

List of usage examples for com.squareup.okhttp Response isSuccessful

Introduction

In this page you can find the example usage for com.squareup.okhttp Response isSuccessful.

Prototype

public boolean isSuccessful() 

Source Link

Document

Returns true if the code is in [200..300), which means the request was successfully received, understood, and accepted.

Usage

From source file:com.he5ed.lib.cloudprovider.apis.OneDriveApi.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 a2  s .c  o  m*/

    // assign filename
    if (TextUtils.isEmpty(filename))
        filename = file.getName();

    Request request = new Request.Builder().url(API_BASE_URL + "/drive/items/" + file.getId() + "/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 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.OneDriveApi.java

License:Apache License

/**
 * Download file from redirect request//from   ww w.j a  va2 s .  c o m
 *
 * @param request for redirect
 * @return File
 * @throws RequestFailException
 */
public synchronized File downloadFile(@NonNull Request request, String filename) throws RequestFailException {
    try {
        File file = new File(mContext.getFilesDir(), TextUtils.isEmpty(filename) ? "Untitled" : filename);

        Response response = mHttpClient.newCall(request).execute();
        if (response.isSuccessful()) {
            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.OneDriveApi.java

License:Apache License

@Override
public void deleteFile(@NonNull CFile file) throws RequestFailException {
    if (TextUtils.isEmpty(mAccessToken)) {
        throw new RequestFailException("Access token not available");
    }/*from w w w  . j a  v a 2 s  . com*/

    Request request = new Request.Builder().url(API_BASE_URL + "/drive/items/" + file.getId())
            .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: " + 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.OneDriveApi.java

License:Apache License

@Override
public List<Object> search(@NonNull String keyword, CFolder folder) throws RequestFailException {
    if (TextUtils.isEmpty(mAccessToken)) {
        throw new RequestFailException("Access token not available");
    }/*from  w w w  .  j  a  va  2  s. c o m*/

    List<Object> list = new ArrayList<>();

    Uri uri = Uri.parse(API_BASE_URL);
    String url = uri.buildUpon().appendEncodedPath("drive/items/" + folder.getId() + "/view.search")
            .appendQueryParameter("q", keyword).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("value");
            if (entries.length() > 0) {
                list.addAll(createFilteredItemsList(entries, folder));
            } else {
                // return null if no item found
                return null;
            }
            // pagination available
            if (jsonObject.has("@odata.nextLink")) {
                list.addAll(searchContinue(jsonObject.getString("@odata.nextLink"), folder));
            }
            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 search items//  www  .  java2s.c  om
 *
 * @param url for the next page of items
 * @param folder where the search is looking at
 * @return List that contains CFile and CFolder
 * @throws RequestFailException that content various error types
 */
public synchronized List<Object> searchContinue(String url, CFolder folder) 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, folder));
            } else {
                // return null if no item found
                return null;
            }
            // pagination available
            if (jsonObject.has("@odata.nextLink")) {
                list.addAll(searchContinue(jsonObject.getString("@odata.nextLink"), folder));
            }
            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 File getThumbnail(@NonNull CFile file) throws RequestFailException {
    if (TextUtils.isEmpty(mAccessToken)) {
        throw new RequestFailException("Access token not available");
    }/*from www . j  a  v a 2  s. co  m*/

    Request request = new Request.Builder()
            .url(API_BASE_URL + "/drive/items/" + file.getId() + "/thumbnails/0/medium/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(), file.getId() + ".jpg");
            case 302:
                // redirect to url
                return downloadFile(response.request(), file.getId() + ".jpg");
            }
        } 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.auth.OAuth2Fragment.java

License:Apache License

/**
 * Get API access token by sending POST request with params build from AuthHelper
 *
 * @param body parameter to be passed with the request
 *//*from w ww.j  a  va 2 s  .c o m*/
private void getAccessToken(RequestBody body) {
    String url;
    try {
        url = AuthHelper.getAccessTokenUri(mCloudApi).toString();
    } catch (MalformedURLException e) {
        e.printStackTrace();
        // register event to listener
        if (mListener != null)
            mListener.onAuthError(getString(R.string.auth_error_malformed_url));
        return;
    }

    Request request = new Request.Builder().url(url).post(body).build();

    mHttpClient.newCall(request).enqueue(new Callback() {
        @Override
        public void onFailure(Request request, IOException e) {
            // register event to listener
            if (mListener != null)
                mListener.onAuthError(getString(R.string.auth_error_access_token_fail));
        }

        @Override
        public void onResponse(Response response) throws IOException {
            if (response.isSuccessful()) {
                try {
                    JSONObject jsonObject = new JSONObject(response.body().string());
                    mTokenInfo = AuthHelper.extractAccessToken(mCloudApi, jsonObject);
                    if (mTokenInfo != null) {
                        getUserInfo(mTokenInfo.get(Authenticator.KEY_ACCESS_TOKEN));
                    } else {
                        // register event to listener
                        if (mListener != null)
                            mListener.onAuthError(getString(R.string.auth_error_access_token_fail));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                    // register event to listener
                    if (mListener != null)
                        mListener.onAuthError(getString(R.string.auth_error_access_token_fail));
                }

            } else {
                // register event to listener
                if (mListener != null)
                    mListener.onAuthError(getString(R.string.auth_error_access_token_fail));
            }
        }
    });

}

From source file:com.he5ed.lib.cloudprovider.auth.OAuth2Fragment.java

License:Apache License

private void getUserInfo(final String accessToken) {

    Request request = AuthHelper.getUserInfoRequest(mCloudApi, accessToken);

    mHttpClient.newCall(request).enqueue(new Callback() {
        @Override//from   ww w  . ja v a 2s . c  o m
        public void onFailure(Request request, IOException e) {
            // register event to listener
            if (mListener != null)
                mListener.onAuthError(getString(R.string.auth_error_user_info_fail));
        }

        @Override
        public void onResponse(Response response) throws IOException {
            if (response.isSuccessful()) {
                try {
                    JSONObject jsonObject = new JSONObject(response.body().string());

                    addAccount(AuthHelper.extractUser(mCloudApi, jsonObject));
                } catch (JSONException e) {
                    e.printStackTrace();
                    // register event to listener
                    if (mListener != null)
                        mListener.onAuthError(getString(R.string.auth_error_user_info_fail));
                }
            } else {
                // register event to listener
                if (mListener != null)
                    mListener.onAuthError(getString(R.string.auth_error_user_info_fail));
            }
        }
    });
}

From source file:com.hileone.restretrofit.response.RestResponse.java

License:Apache License

/**
 * Create a successful response from {@code rawResponse} with {@code body} as the deserialized
 * body.//from w  ww.  j  a va  2s. com
 */
public static <T> RestResponse<T> success(T body, com.squareup.okhttp.Response rawResponse) {
    if (rawResponse == null) {
        throw new NullPointerException("rawResponse == null");
    }
    if (!rawResponse.isSuccessful()) {
        throw new IllegalArgumentException("rawResponse must be successful response");
    }
    return new RestResponse<>(rawResponse, body, null);
}

From source file:com.hkm.root.Tasks.upload_data.java

License:Open Source License

protected String OCokHttpPostData(final String url, final String json) throws IOException {
    RequestBody body = RequestBody.create(JSON, json);
    Request request = new Request.Builder().url(url).post(body).build();
    OkHttpClient use_client = client.clone();
    Response response = use_client.newCall(request).execute();
    if (response.isSuccessful()) {
        return response.body().string();
    } else//from w w w.  ja  va  2  s . c o  m
        throw new IOException("not success on HTTP request.");
}