Search in sources :

Example 71 with Response

use of com.squareup.okhttp.Response in project pictureapp by EyeSeeTea.

the class BasicAuthenticator method getServerVersion.

/**
     * Returns the version of the given server.
     * Null if something went wrong
     */
public static String getServerVersion(String url) {
    String serverVersion;
    try {
        String urlServerInfo = url + DHIS_SERVER_INFO;
        Response response = executeCall(null, urlServerInfo, "GET");
        //Error -> null
        if (!response.isSuccessful()) {
            Log.e(TAG, "getServerVersion (" + response.code() + "): " + response.body().string());
            throw new IOException(response.message());
        }
        JSONObject data = parseResponse(response.body().string());
        serverVersion = data.getString(TAG_VERSION);
    } catch (Exception ex) {
        Log.e(TAG, "getServerVersion: " + ex.toString());
        serverVersion = "";
    }
    Log.i(TAG, String.format("getServerVersion(%s) -> %s", url, serverVersion));
    return serverVersion;
}
Also used : Response(com.squareup.okhttp.Response) JSONObject(org.json.JSONObject) IOException(java.io.IOException) JSONException(org.json.JSONException) ShowException(org.eyeseetea.malariacare.views.ShowException) IOException(java.io.IOException)

Example 72 with Response

use of com.squareup.okhttp.Response in project pictureapp by EyeSeeTea.

the class BasicAuthenticator method pullOrgUnitsCodes.

/**
     * This method returns a String[] whit the Organitation codes
     */
public static String[] pullOrgUnitsCodes(String url) {
    try {
        String orgUnitsURL = getDhisOrgUnitsURL(url);
        Response response = executeCall(null, orgUnitsURL, "GET");
        //Error -> null
        if (!response.isSuccessful()) {
            Log.e(TAG, "pullOrgUnitsCodes (" + response.code() + "): " + response.body().string());
            throw new IOException(response.message());
        }
        //{"organisationUnits":[{}]}
        JSONArray orgUnitsArray = parseResponse(response.body().string()).getJSONArray(TAG_ORGANISATIONUNITS);
        //0 matches -> Error
        if (orgUnitsArray.length() == 0) {
            throw new Exception("Found 0 matches");
        }
        return Utils.jsonArrayToStringArray(orgUnitsArray, TAG_CODE);
    } catch (Exception ex) {
        Log.e(TAG, String.format("pullOrgUnitsCodes(%url): %s", url, ex.getMessage()));
        String[] value = new String[1];
        value[0] = "";
        return value;
    }
}
Also used : Response(com.squareup.okhttp.Response) JSONArray(org.json.JSONArray) IOException(java.io.IOException) JSONException(org.json.JSONException) ShowException(org.eyeseetea.malariacare.views.ShowException) IOException(java.io.IOException)

Example 73 with Response

use of com.squareup.okhttp.Response in project pictureapp by EyeSeeTea.

the class BasicAuthenticator method patchClosedDate.

/**
     * Updates the orgUnit adding a closedDate
     */
static void patchClosedDate(String url, String orgUnitUID) {
    //https://malariacare.psi.org/api/organisationUnits/u5jlxuod8xQ/closedDate
    try {
        String urlPathClosedDate = getPatchClosedDateUrl(url, orgUnitUID);
        JSONObject data = prepareTodayDateValue();
        Response response = executeCall(data, urlPathClosedDate, "PATCH");
        if (!response.isSuccessful()) {
            Log.e(TAG, "closingDatePatch (" + response.code() + "): " + response.body().string());
            throw new IOException(response.message());
        }
    } catch (Exception e) {
        Log.e(TAG, String.format("patchClosedDate(%s,%s): %s", url, orgUnitUID, e.getMessage()));
    }
}
Also used : Response(com.squareup.okhttp.Response) JSONObject(org.json.JSONObject) IOException(java.io.IOException) JSONException(org.json.JSONException) ShowException(org.eyeseetea.malariacare.views.ShowException) IOException(java.io.IOException)

Example 74 with Response

use of com.squareup.okhttp.Response in project weex-example by KalicyZhou.

the class DefaultWebSocketAdapter method connect.

@Override
public void connect(String url, @Nullable String protocol, EventListener listener) {
    this.eventListener = listener;
    OkHttpClient okHttpClient = new OkHttpClient();
    Request.Builder builder = new Request.Builder();
    if (protocol != null) {
        builder.addHeader(HEADER_SEC_WEBSOCKET_PROTOCOL, protocol);
    }
    builder.url(url);
    WebSocketCall.create(okHttpClient, builder.build()).enqueue(new WebSocketListener() {

        @Override
        public void onOpen(WebSocket webSocket, Request request, Response response) throws IOException {
            ws = webSocket;
            eventListener.onOpen();
        }

        @Override
        public void onMessage(BufferedSource payload, WebSocket.PayloadType type) throws IOException {
            eventListener.onMessage(payload.readUtf8());
            payload.close();
        }

        @Override
        public void onPong(Buffer payload) {
        }

        @Override
        public void onClose(int code, String reason) {
            eventListener.onClose(code, reason, true);
        }

        @Override
        public void onFailure(IOException e) {
            e.printStackTrace();
            if (e instanceof EOFException) {
                eventListener.onClose(WebSocketCloseCodes.CLOSE_NORMAL.getCode(), WebSocketCloseCodes.CLOSE_NORMAL.name(), true);
            } else {
                eventListener.onError(e.getMessage());
            }
        }
    });
}
Also used : Buffer(okio.Buffer) WebSocketListener(com.squareup.okhttp.ws.WebSocketListener) OkHttpClient(com.squareup.okhttp.OkHttpClient) Request(com.squareup.okhttp.Request) IOException(java.io.IOException) WebSocket(com.squareup.okhttp.ws.WebSocket) Response(com.squareup.okhttp.Response) EOFException(java.io.EOFException) BufferedSource(okio.BufferedSource)

Example 75 with Response

use of com.squareup.okhttp.Response in project ignite by apache.

the class IgniteCamelStreamerTest method sendMessages.

/**
 * @throws IOException
 * @return HTTP response payloads.
 */
private List<String> sendMessages(int fromIdx, int cnt, boolean singleMessage) throws IOException {
    List<String> responses = Lists.newArrayList();
    if (singleMessage) {
        StringBuilder sb = new StringBuilder();
        for (int i = fromIdx; i < fromIdx + cnt; i++) sb.append(i).append(",").append(TEST_DATA.get(i)).append("\n");
        Request request = new Request.Builder().url(url).post(RequestBody.create(TEXT_PLAIN, sb.toString())).build();
        Response response = httpClient.newCall(request).execute();
        responses.add(response.body().string());
    } else {
        for (int i = fromIdx; i < fromIdx + cnt; i++) {
            String payload = i + "," + TEST_DATA.get(i);
            Request request = new Request.Builder().url(url).post(RequestBody.create(TEXT_PLAIN, payload)).build();
            Response response = httpClient.newCall(request).execute();
            responses.add(response.body().string());
        }
    }
    return responses;
}
Also used : Response(com.squareup.okhttp.Response) Request(com.squareup.okhttp.Request) Endpoint(org.apache.camel.Endpoint)

Aggregations

Response (com.squareup.okhttp.Response)109 Request (com.squareup.okhttp.Request)75 IOException (java.io.IOException)70 OkHttpClient (com.squareup.okhttp.OkHttpClient)31 RequestBody (com.squareup.okhttp.RequestBody)23 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 ShowException (org.eyeseetea.malariacare.views.ShowException)7 Buffer (okio.Buffer)6 MediaType (com.squareup.okhttp.MediaType)5 InputStream (java.io.InputStream)5 HashMap (java.util.HashMap)5 Call (com.squareup.okhttp.Call)4 ApiException (io.kubernetes.client.ApiException)4