Search in sources :

Example 11 with Response

use of com.squareup.okhttp.Response in project openzaly by akaxincom.

the class ZalyHttpClient method run.

public byte[] run(String url) throws Exception {
    Request request = new Request.Builder().url(url).build();
    Response response = httpClient.newCall(request).execute();
    if (response.isSuccessful()) {
        return response.body().bytes();
    } else {
        logger.error("http post error.{}", response.message());
    }
    return null;
}
Also used : Response(com.squareup.okhttp.Response) Request(com.squareup.okhttp.Request)

Example 12 with Response

use of com.squareup.okhttp.Response in project openzaly by akaxincom.

the class HttpTestPost method post.

static String post(String url, String json) throws IOException {
    RequestBody body = RequestBody.create(JSON, json);
    Request request = new Request.Builder().url(url).post(body).build();
    Response response = client.newCall(request).execute();
    System.out.println("response = " + response.isSuccessful());
    if (response.isSuccessful()) {
        return response.body().string();
    } else {
        throw new IOException("Unexpected code " + response);
    }
}
Also used : Response(com.squareup.okhttp.Response) Request(com.squareup.okhttp.Request) IOException(java.io.IOException) RequestBody(com.squareup.okhttp.RequestBody)

Example 13 with Response

use of com.squareup.okhttp.Response in project twister2 by DSC-SPIDAL.

the class KubernetesController method createStatefulSetJob.

/**
 * create the given service on Kubernetes master
 */
public boolean createStatefulSetJob(String namespace, V1beta2StatefulSet statefulSet) {
    String statefulSetName = statefulSet.getMetadata().getName();
    try {
        Response response = beta2Api.createNamespacedStatefulSetCall(namespace, statefulSet, null, null, null).execute();
        if (response.isSuccessful()) {
            LOG.log(Level.INFO, "StatefulSet [" + statefulSetName + "] is created for the same named job.");
            return true;
        } else {
            LOG.log(Level.SEVERE, "Error when creating the StatefulSet [" + statefulSetName + "]: " + response);
            LOG.log(Level.SEVERE, "Submitted StatefulSet Object: " + statefulSet);
            return false;
        }
    } catch (IOException e) {
        LOG.log(Level.SEVERE, "Exception when creating the StatefulSet: " + statefulSetName, e);
    } catch (ApiException e) {
        LOG.log(Level.SEVERE, "Exception when creating the StatefulSet: " + statefulSetName, e);
    }
    return false;
}
Also used : Response(com.squareup.okhttp.Response) IOException(java.io.IOException) ApiException(io.kubernetes.client.ApiException)

Example 14 with Response

use of com.squareup.okhttp.Response in project twister2 by DSC-SPIDAL.

the class KubernetesController method deleteStatefulSetJob.

/**
 * delete the given StatefulSet from Kubernetes master
 */
public boolean deleteStatefulSetJob(String namespace, String statefulSetName) {
    try {
        V1DeleteOptions deleteOptions = new V1DeleteOptions();
        deleteOptions.setGracePeriodSeconds(0L);
        deleteOptions.setPropagationPolicy(KubernetesConstants.DELETE_OPTIONS_PROPAGATION_POLICY);
        Response response = beta2Api.deleteNamespacedStatefulSetCall(statefulSetName, namespace, deleteOptions, null, null, null, null, null, null).execute();
        if (response.isSuccessful()) {
            LOG.log(Level.INFO, "StatefulSet for the Job [" + statefulSetName + "] is deleted.");
            return true;
        } else {
            if (response.code() == 404 && response.message().equals("Not Found")) {
                LOG.log(Level.SEVERE, "There is no StatefulSet for the Job [" + statefulSetName + "] to delete on Kubernetes master. It may have already terminated.");
                return true;
            }
            LOG.log(Level.SEVERE, "Error when deleting the StatefulSet of the job [" + statefulSetName + "]: " + response);
            return false;
        }
    } catch (ApiException e) {
        LOG.log(Level.SEVERE, "Exception when deleting the the StatefulSet of the job: " + statefulSetName, e);
        return false;
    } catch (IOException e) {
        LOG.log(Level.SEVERE, "Exception when deleting the the StatefulSet of the job: " + statefulSetName, e);
        return false;
    }
}
Also used : Response(com.squareup.okhttp.Response) V1DeleteOptions(io.kubernetes.client.models.V1DeleteOptions) IOException(java.io.IOException) ApiException(io.kubernetes.client.ApiException)

Example 15 with Response

use of com.squareup.okhttp.Response in project xDrip by NightscoutFoundation.

the class WixelReader method readHttpJson.

// read from http source like cloud hosted parakeet receiver.cgi / json.get
private static List<TransmitterRawData> readHttpJson(String url, int numberOfRecords) {
    final List<TransmitterRawData> trd_list = new LinkedList<>();
    int processNumberOfRecords = numberOfRecords;
    // TODO make this work on preference option for the feature
    if (true)
        numberOfRecords = numberOfRecords + 1;
    long newest_timestamp = 0;
    try {
        if (httpClient == null) {
            httpClient = new OkHttpClient();
            // suitable for GPRS
            httpClient.setConnectTimeout(30, TimeUnit.SECONDS);
            httpClient.setReadTimeout(60, TimeUnit.SECONDS);
            httpClient.setWriteTimeout(20, TimeUnit.SECONDS);
        }
        // simple HTTP GET request
        // n=numberOfRecords for backfilling
        // r=sequence number to avoid any cache
        // expecting json reply like the standard json server in dexterity / python pi usb / parakeet
        final Request request = new Request.Builder().header("User-Agent", "Mozilla/5.0").header("Connection", "close").url(url + "?n=" + Integer.toString(numberOfRecords) + "&r=" + Long.toString((System.currentTimeMillis() / 1000) % 9999999)).build();
        final Response response = httpClient.newCall(request).execute();
        // if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
        if (response.isSuccessful()) {
            String[] lines = response.body().string().split("\\r?\\n");
            for (String data : lines) {
                if (data == null) {
                    Log.d(TAG, "received null continuing");
                    continue;
                }
                if (data.equals("")) {
                    Log.d(TAG, "received \"\" continuing");
                    continue;
                }
                final TransmitterRawData trd = gson.fromJson(data, TransmitterRawData.class);
                trd.CaptureDateTime = System.currentTimeMillis() - trd.RelativeTime;
                // Versions of the Python USB script after 20th May 2016 will
                // submit a bogus geolocation in the middle of the ocean to differentiate
                // themselves from actual parakeet data even though both can coexist on the
                // parakeet web service.
                // if (JoH.ratelimit("parakeet-check-notification", 9)) {
                ParakeetHelper.checkParakeetNotifications(trd.CaptureDateTime, trd.GeoLocation);
                // }
                if ((trd.GeoLocation != null)) {
                    if (!trd.GeoLocation.equals("-15,-15")) {
                        try {
                            MapsActivity.newMapLocation(trd.GeoLocation, trd.CaptureDateTime);
                        } catch (Exception e) {
                            Log.e(TAG, "Exception with maps activity: " + e.toString());
                        }
                    } else {
                        // look a little further if we see usb-wixel data on parakeet app engine
                        processNumberOfRecords = numberOfRecords + 1;
                    }
                }
                if (newest_timestamp < trd.getCaptureDateTime()) {
                    statusLog(url, JoH.hourMinuteString() + " OK data from:", trd.getCaptureDateTime());
                    newest_timestamp = trd.CaptureDateTime;
                }
                trd_list.add(0, trd);
                // System.out.println( trd.toTableString());
                if (trd_list.size() == processNumberOfRecords) {
                    // We have the data we want, let's get out
                    break;
                }
            }
            Log.i(TAG, "Success getting http json with end size: " + Integer.toString(trd_list.size()));
        }
    } catch (Exception e) {
        Log.e(TAG, "caught Exception in reading http json data " + e.toString());
    }
    return trd_list;
}
Also used : Response(com.squareup.okhttp.Response) OkHttpClient(com.squareup.okhttp.OkHttpClient) BgGraphBuilder(com.eveningoutpost.dexdrip.UtilityModels.BgGraphBuilder) Request(com.squareup.okhttp.Request) LinkedList(java.util.LinkedList) SocketTimeoutException(java.net.SocketTimeoutException) IOException(java.io.IOException)

Aggregations

Response (com.squareup.okhttp.Response)108 Request (com.squareup.okhttp.Request)75 IOException (java.io.IOException)72 OkHttpClient (com.squareup.okhttp.OkHttpClient)33 RequestBody (com.squareup.okhttp.RequestBody)24 JSONException (org.json.JSONException)16 JSONObject (org.json.JSONObject)15 FormEncodingBuilder (com.squareup.okhttp.FormEncodingBuilder)14 UnsupportedEncodingException (java.io.UnsupportedEncodingException)10 ApiCallException (org.eyeseetea.malariacare.domain.exception.ApiCallException)10 Callback (com.squareup.okhttp.Callback)9 SocketTimeoutException (java.net.SocketTimeoutException)8 File (java.io.File)7 Buffer (okio.Buffer)7 ShowException (org.eyeseetea.malariacare.views.ShowException)7 MediaType (com.squareup.okhttp.MediaType)5 InputStream (java.io.InputStream)5 ResponseBody (com.squareup.okhttp.ResponseBody)4 ApiException (io.kubernetes.client.ApiException)4 FileOutputStream (java.io.FileOutputStream)4