Search in sources :

Example 6 with Response

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

the class BasicAuthenticator method pullOrgUnitsNotBannedCodes.

/**
     * This method returns a String[] whit the Organitation codes
     */
public static String[] pullOrgUnitsNotBannedCodes(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_ID);
    } 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 7 with Response

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

the class BasicAuthenticator method patchDescriptionClosedDate.

static void patchDescriptionClosedDate(String url, String orgUnitUID, String orgUnitDescription) {
    //https://malariacare.psi.org/api/organisationUnits/u5jlxuod8xQ/closedDate
    try {
        String urlPathClosedDescription = getPatchClosedDescriptionUrl(url, orgUnitUID);
        JSONObject data = prepareClosingDescriptionValue(orgUnitDescription);
        Response response = executeCall(data, urlPathClosedDescription, "PATCH");
        if (!response.isSuccessful()) {
            Log.e(TAG, "patchDescriptionClosedDate (" + response.code() + "): " + response.body().string());
            throw new IOException(response.message());
        }
    } catch (Exception e) {
        Log.e(TAG, String.format("patchDescriptionClosedDate(%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 8 with Response

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

the class BasicAuthenticator method isValidProgram.

/**
     * Returns if the given url contains the current program
     */
public static boolean isValidProgram(String url) {
    Log.d(TAG, String.format("isValidProgram(%s) ...", url));
    String programUIDInServer;
    try {
        String urlValidProgram = getIsValidProgramUrl(url);
        Response response = executeCall(null, urlValidProgram, "GET");
        //Error -> null
        if (!response.isSuccessful()) {
            Log.e(TAG, "isValidProgram (" + response.code() + "): " + response.body().string());
            throw new IOException(response.message());
        }
        JSONObject data = parseResponse(response.body().string());
        programUIDInServer = String.valueOf(data.get(TAG_ID));
    } catch (Exception ex) {
        Log.e(TAG, "isValidProgram: " + ex.toString());
        return false;
    }
    boolean valid = getProgramUID() != null && getProgramUID().equals(programUIDInServer);
    Log.d(TAG, String.format("isValidProgram(%s) -> %b (Thread: %d)", url, valid, Thread.currentThread().getId()));
    return valid;
}
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 9 with Response

use of com.squareup.okhttp.Response in project PocketHub by pockethub.

the class RawCommentFragment method onActivityResult.

@Override
public void onActivityResult(final int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == REQUEST_CODE_SELECT_PHOTO && resultCode == Activity.RESULT_OK) {
        showProgressIndeterminate(R.string.loading);
        ImageBinPoster.post(getActivity(), data.getData(), new Callback() {

            @Override
            public void onFailure(Request request, IOException e) {
                dismissProgress();
                showImageError();
            }

            @Override
            public void onResponse(Response response) throws IOException {
                dismissProgress();
                if (response.isSuccessful()) {
                    insertImage(ImageBinPoster.getUrl(response.body().string()));
                } else {
                    showImageError();
                }
            }
        });
    }
}
Also used : Response(com.squareup.okhttp.Response) Callback(com.squareup.okhttp.Callback) Request(com.squareup.okhttp.Request) IOException(java.io.IOException)

Example 10 with Response

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

the class ZalyHttpClient method get.

public byte[] get(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 get url={} error.{}", url, response.message());
    }
    return null;
}
Also used : Response(com.squareup.okhttp.Response) Request(com.squareup.okhttp.Request)

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