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.auth0.api.internal.SimpleRequest.java

License:Open Source License

@Override
public void onResponse(Response response) throws IOException {
    Log.d(TAG, String.format("Received response from request to %s with status code %d",
            response.request().urlString(), response.code()));
    final InputStream byteStream = response.body().byteStream();
    if (!response.isSuccessful()) {
        Throwable throwable;//w w  w .  j a  va  2 s . c  o  m
        try {
            Map<String, Object> payload = errorReader.readValue(byteStream);
            throwable = new APIClientException("Request failed with response " + payload, response.code(),
                    payload);
        } catch (IOException e) {
            throwable = new APIClientException("Request failed", response.code(), null);
        }
        postOnFailure(throwable);
        return;
    }

    try {
        Log.d(TAG, "Received successful response from " + response.request().urlString());
        T payload = getReader().readValue(byteStream);
        postOnSuccess(payload);
    } catch (IOException e) {
        postOnFailure(new APIClientException("Request failed", response.code(), null));
    }
}

From source file:com.auth0.api.internal.VoidRequest.java

License:Open Source License

@Override
public void onResponse(Response response) throws IOException {
    Log.d(TAG, String.format("Received response from request to %s with status code %d",
            response.request().urlString(), response.code()));
    final InputStream byteStream = response.body().byteStream();
    if (!response.isSuccessful()) {
        Throwable throwable;//ww  w . j  a  v a2s  .  c  o  m
        try {
            Map<String, Object> payload = errorReader.readValue(byteStream);
            throwable = new APIClientException("Request failed with response " + payload, response.code(),
                    payload);
        } catch (IOException e) {
            throwable = new APIClientException("Request failed", response.code(), null);
        }
        postOnFailure(throwable);
        return;
    }

    postOnSuccess(null);
}

From source file:com.auth0.request.internal.SimpleRequest.java

License:Open Source License

@Override
public void onResponse(Response response) throws IOException {
    if (!response.isSuccessful()) {
        APIException exception = parseUnsuccessfulResponse(response);
        postOnFailure(exception);//w  w w. j  a  va 2 s.  c  o  m
        return;
    }

    try {
        final InputStream byteStream = response.body().byteStream();
        T payload = getReader().readValue(byteStream);
        postOnSuccess(payload);
    } catch (IOException e) {
        postOnFailure(new Auth0Exception("Failed to parse response to request to " + url, e));
    }
}

From source file:com.auth0.request.internal.SimpleRequest.java

License:Open Source License

@Override
public T execute() throws Auth0Exception {
    Request request = doBuildRequest(newBuilder());

    Response response;
    try {//  ww w.  j ava  2s  .  c om
        response = client.newCall(request).execute();
    } catch (IOException e) {
        throw new Auth0Exception("Failed to execute request to " + url, e);
    }

    if (!response.isSuccessful()) {
        throw parseUnsuccessfulResponse(response);
    }

    try {
        final InputStream byteStream = response.body().byteStream();
        return getReader().readValue(byteStream);
    } catch (IOException e) {
        throw new Auth0Exception("Failed to parse response to request to " + url, e);
    }
}

From source file:com.auth0.request.internal.VoidRequest.java

License:Open Source License

@Override
public void onResponse(Response response) throws IOException {
    if (!response.isSuccessful()) {
        APIException exception = parseUnsuccessfulResponse(response);
        postOnFailure(exception);/*from  w w w.  j  a v  a 2  s  .  co m*/
        return;
    }

    postOnSuccess(null);
}

From source file:com.auth0.request.internal.VoidRequest.java

License:Open Source License

@Override
public Void execute() throws Auth0Exception {
    Request request = doBuildRequest(newBuilder());

    Response response;
    try {//  w ww. j a  va 2s.  c o m
        response = client.newCall(request).execute();
    } catch (IOException e) {
        throw new Auth0Exception("Failed to execute request to " + url.toString(), e);
    }

    if (!response.isSuccessful()) {
        throw parseUnsuccessfulResponse(response);
    }
    return null;
}

From source file:com.battlelancer.seriesguide.thetvdbapi.TheTVDB.java

License:Apache License

/**
 * Downloads the XML or ZIP file from the given URL, passing a valid response to {@link
 * Xml#parse(InputStream, android.util.Xml.Encoding, ContentHandler)} using the given {@link
 * ContentHandler}.//from   w w w.  ja v  a 2s .c  om
 */
private static void downloadAndParse(Context context, ContentHandler handler, String urlString,
        boolean isZipFile) throws TvdbException {
    Request request = new Request.Builder().url(urlString).build();

    Response response;
    try {
        response = ServiceUtils.getCachingOkHttpClient(context).newCall(request).execute();
    } catch (IOException e) {
        throw new TvdbException(e.getMessage() + " " + urlString, e);
    }

    int statusCode = response.code();
    if (statusCode == 404) {
        // special case: item does not exist (any longer)
        throw new TvdbException(response.code() + " " + response.message() + " " + urlString, true, null);
    }
    if (!response.isSuccessful()) {
        // other non-2xx response
        throw new TvdbException(response.code() + " " + response.message() + " " + urlString);
    }

    try {
        final InputStream input = response.body().byteStream();
        if (isZipFile) {
            // We downloaded the compressed file from TheTVDB
            final ZipInputStream zipin = new ZipInputStream(input);
            zipin.getNextEntry();
            try {
                Xml.parse(zipin, Xml.Encoding.UTF_8, handler);
            } finally {
                zipin.close();
            }
        } else {
            try {
                Xml.parse(input, Xml.Encoding.UTF_8, handler);
            } finally {
                if (input != null) {
                    input.close();
                }
            }
        }
    } catch (SAXException | IOException | AssertionError e) {
        throw new TvdbException(e.getMessage() + " " + urlString, e);
    }
}

From source file:com.braisgabin.fbstats.Api.java

License:Apache License

public <T> T download(String url, Class<T> clazz) throws JsonParseException, JsonMappingException, IOException {
    Request request = new Request.Builder().url(url).addHeader("Authorization", "OAuth " + accessToken).build();

    Response response = client.newCall(request).execute();
    if (!response.isSuccessful()) {
        throw new RuntimeException(response.body().string());
    }/* w  ww. j av a  2  s . c o  m*/
    return mapper.readValue(response.body().byteStream(), clazz);
}

From source file:com.capstone.transit.trans_it.TripPlannerActivity.java

License:Open Source License

private void getDirections(LatLng origin, LatLng destination) {

    final String directionsURL = getDirectionsUrl(origin, destination);
    Log.v(TAG, directionsURL);/*  w  ww  . j  av  a 2 s  .c om*/

    if (isNetworkAvailable()) {

        Thread T = new Thread() {
            public void run() {
                OkHttpClient client = new OkHttpClient();
                Request request = new Request.Builder().url(directionsURL).build();

                Call call = client.newCall(request);
                try {
                    Response response = call.execute();
                    String jsonData = response.body().string();

                    Log.v(TAG, jsonData);

                    if (response.isSuccessful()) {

                        mStep = getSteps(jsonData);

                    } else {
                        alertUserAboutError();
                    }
                } catch (JSONException | IOException e) {
                    e.printStackTrace();
                }

            }
        };
        T.start();

        try {
            T.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    } else {
        Toast.makeText(this, "unavailable network", Toast.LENGTH_LONG).show();
    }
}

From source file:com.coinomi.wallet.ExchangeRatesProvider.java

License:Open Source License

@Nullable
private JSONObject requestExchangeRatesJson(final URL url) {
    // Return null if no connection
    final NetworkInfo activeInfo = connManager.getActiveNetworkInfo();
    if (activeInfo == null || !activeInfo.isConnected())
        return null;

    final long start = System.currentTimeMillis();

    OkHttpClient client = NetworkUtils.getHttpClient(getContext().getApplicationContext());
    Request request = new Request.Builder().url(url).build();

    try {/* www. ja  v  a  2 s .co  m*/
        Response response = client.newCall(request).execute();
        if (response.isSuccessful()) {
            log.info("fetched exchange rates from {} ({}), {} chars, took {} ms", url,
                    System.currentTimeMillis() - start);
            return new JSONObject(response.body().string());
        } else {
            log.warn("Error HTTP code '{}' when fetching exchange rates from {}", response.code(), url);
        }
    } catch (IOException e) {
        log.warn("Error '{}' when fetching exchange rates from {}", e.getMessage(), url);
    } catch (JSONException e) {
        log.warn("Could not parse exchange rates JSON: {}", e.getMessage());
    }
    return null;
}