Search in sources :

Example 96 with Response

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

the class ServerAPIController method getOrgUnitData.

/**
 * Returns the orgunit data from the given server according to its current version
 */
static JSONObject getOrgUnitData(String url, String orgUnitNameOrCode) throws ApiCallException {
    // Version is required to choose which field to match
    String serverVersion = getServerVersion(url);
    // No version -> No data
    if (serverVersion == null) {
        return null;
    }
    String urlOrgUnitData = getOrgUnitDataUrl(url, serverVersion, orgUnitNameOrCode);
    Response response = ServerApiCallExecution.executeCall(null, urlOrgUnitData, "GET");
    // {"organisationUnits":[{}]}
    JSONObject jsonResponse = ServerApiUtils.getApiResponseAsJSONObject(response);
    JSONArray orgUnitsArray = null;
    try {
        orgUnitsArray = (JSONArray) jsonResponse.get(TAG_ORGANISATIONUNITS);
    } catch (JSONException e) {
        throw new ApiCallException(e);
    }
    // 0| >1 matches -> Error
    if (orgUnitsArray.length() == 0 || orgUnitsArray.length() > 1) {
        Log.e(TAG, String.format("getOrgUnitData(%s,%s) -> Found %d matches", url, orgUnitNameOrCode, orgUnitsArray.length()));
        return null;
    }
    try {
        return (JSONObject) orgUnitsArray.get(0);
    } catch (JSONException e) {
        throw new ApiCallException(e);
    }
}
Also used : Response(com.squareup.okhttp.Response) JSONObject(org.json.JSONObject) ApiCallException(org.eyeseetea.malariacare.domain.exception.ApiCallException) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException)

Example 97 with Response

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

the class ServerAPIController method getOrganisationUnitsByCode.

public static OrganisationUnit getOrganisationUnitsByCode(String code) throws ApiCallException {
    // Version is required to choose which field to match
    String serverVersion = getServerVersion(PreferencesState.getInstance().getDhisURL());
    // No version -> No data
    if (serverVersion == null) {
        return null;
    }
    try {
        String urlOrgUnitData = getOrganisationUnitsCredentialsUrl(code);
        if (!isNetworkAvailable()) {
            throw new NetworkException();
        }
        Response response = ServerApiCallExecution.executeCall(null, urlOrgUnitData, "GET");
        JSONObject body = ServerApiUtils.getApiResponseAsJSONObject(response);
        // {"organisationUnits":[{}]}
        JSONArray orgUnitsArray = (JSONArray) body.get(TAG_ORGANISATIONUNITS);
        // 0| >1 matches -> Error
        if (orgUnitsArray.length() == 0 || orgUnitsArray.length() > 1) {
            Log.e(TAG, String.format("getOrgUnitData(%s) -> Found %d matches", code, orgUnitsArray.length()));
            return null;
        }
        JSONObject orgUnitJO = (JSONObject) orgUnitsArray.get(0);
        return parseOrgUnit(orgUnitJO);
    } catch (NetworkException e) {
        throw new ApiCallException(e);
    } catch (Exception ex) {
        throw new ApiCallException(ex);
    }
}
Also used : Response(com.squareup.okhttp.Response) JSONObject(org.json.JSONObject) ApiCallException(org.eyeseetea.malariacare.domain.exception.ApiCallException) JSONArray(org.json.JSONArray) NetworkException(org.eyeseetea.malariacare.domain.exception.NetworkException) NetworkException(org.eyeseetea.malariacare.domain.exception.NetworkException) ApiCallException(org.eyeseetea.malariacare.domain.exception.ApiCallException) ConfigJsonIOException(org.eyeseetea.malariacare.domain.exception.ConfigJsonIOException) PullConversionException(org.eyeseetea.malariacare.domain.exception.PullConversionException) JSONException(org.json.JSONException)

Example 98 with Response

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

the class BasicAuthenticator method executeCall.

/**
 * Call to DHIS Server
 */
static Response executeCall(JSONObject data, String url, String method) throws ApiCallException {
    final String DHIS_URL = url;
    Log.d(method, DHIS_URL);
    OkHttpClient client = UnsafeOkHttpsClientFactory.getUnsafeOkHttpClient();
    BasicAuthenticator basicAuthenticator = new BasicAuthenticator();
    client.setAuthenticator(basicAuthenticator);
    Request.Builder builder = new Request.Builder().header(basicAuthenticator.AUTHORIZATION_HEADER, basicAuthenticator.getCredentials()).url(DHIS_URL);
    switch(method) {
        case "POST":
            RequestBody postBody = RequestBody.create(JSON, data.toString());
            builder.post(postBody);
            break;
        case "PUT":
            RequestBody putBody = RequestBody.create(JSON, data.toString());
            builder.put(putBody);
            break;
        case "PATCH":
            RequestBody patchBody = RequestBody.create(JSON, data.toString());
            builder.patch(patchBody);
            break;
        case "GET":
            builder.get();
            break;
    }
    Request request = builder.build();
    Response response = null;
    try {
        response = client.newCall(request).execute();
    } catch (IOException ex) {
        throw new ApiCallException(ex);
    }
    return response;
}
Also used : Response(com.squareup.okhttp.Response) OkHttpClient(com.squareup.okhttp.OkHttpClient) ApiCallException(org.eyeseetea.malariacare.domain.exception.ApiCallException) Request(com.squareup.okhttp.Request) IOException(java.io.IOException) ConfigJsonIOException(org.eyeseetea.malariacare.domain.exception.ConfigJsonIOException) RequestBody(com.squareup.okhttp.RequestBody)

Example 99 with Response

use of com.squareup.okhttp.Response in project nmid-headline by miao1007.

the class WebViewFragment method trySetupWebview.

private void trySetupWebview() {
    // http://202.202.43.205:8086/api/android/newscontent?category=1&id=194
    url = HeadlineService.END_POINT + "/api/android/newscontent?id=" + feed.getIdmember() + "&category=" + feed.getCategory();
    WebSettings settings = mWebView.getSettings();
    mWebView.setWebContentsDebuggingEnabled(true);
    settings.setTextZoom(WebViewPref.getWebViewTextZoom(getActivity()));
    switch(WebViewPref.isAutoLoadImages(getActivity())) {
        case 0:
            settings.setLoadsImagesAutomatically(false);
            break;
        case 1:
            break;
        case 2:
            if (!NetworkUtils.isWifiAviliable(getActivity())) {
                settings.setLoadsImagesAutomatically(false);
            }
            break;
    }
    OkHttpClient client = new OkHttpClient();
    Request request = new Request.Builder().url(url).build();
    client.newCall(request).enqueue(new Callback() {

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

        @Override
        public void onResponse(Response response) throws IOException {
            String htmlData;
            if (ThemePref.isNightMode(getActivity())) {
                // Webview will use asserts/style_night.css
                htmlData = "<link rel=\"stylesheet\" type=\"text/css\" href=\"style_night.css\" /> <body class= \"gloable\"> " + response.body().string() + "</body>";
            } else {
                // Webview will use asserts/style.css
                htmlData = "<link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\" /> <body class= \"gloable\"> " + response.body().string() + "</body>";
            }
            Log.d(TAG, Thread.currentThread().getName());
            getActivity().runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    mWebView.loadDataWithBaseURL("file:///android_asset/", htmlData, MIME_TYPE, ENCODING, null);
                }
            });
        }
    });
}
Also used : Response(com.squareup.okhttp.Response) OkHttpClient(com.squareup.okhttp.OkHttpClient) Callback(com.squareup.okhttp.Callback) WebSettings(android.webkit.WebSettings) Request(com.squareup.okhttp.Request) IOException(java.io.IOException)

Example 100 with Response

use of com.squareup.okhttp.Response in project glide by bumptech.

the class OkHttpStreamFetcher method loadData.

@Override
public void loadData(@NonNull Priority priority, @NonNull final DataCallback<? super InputStream> callback) {
    Request.Builder requestBuilder = new Request.Builder().url(url.toStringUrl());
    for (Map.Entry<String, String> headerEntry : url.getHeaders().entrySet()) {
        String key = headerEntry.getKey();
        requestBuilder.addHeader(key, headerEntry.getValue());
    }
    Request request = requestBuilder.build();
    client.newCall(request).enqueue(new com.squareup.okhttp.Callback() {

        @Override
        public void onFailure(Request request, IOException e) {
            if (Log.isLoggable(TAG, Log.DEBUG)) {
                Log.d(TAG, "OkHttp failed to obtain result", e);
            }
            callback.onLoadFailed(e);
        }

        @Override
        public void onResponse(Response response) throws IOException {
            responseBody = response.body();
            if (response.isSuccessful()) {
                long contentLength = responseBody.contentLength();
                stream = ContentLengthInputStream.obtain(responseBody.byteStream(), contentLength);
                callback.onDataReady(stream);
            } else {
                callback.onLoadFailed(new HttpException(response.message(), response.code()));
            }
        }
    });
}
Also used : Response(com.squareup.okhttp.Response) Request(com.squareup.okhttp.Request) HttpException(com.bumptech.glide.load.HttpException) IOException(java.io.IOException) Map(java.util.Map)

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