Search in sources :

Example 96 with Request

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

the class PushClient method pushData.

/**
 * Pushes data to DHIS Server
 */
private JSONObject pushData(JSONObject data) throws ApiCallException {
    Response response = null;
    final String DHIS_URL = getDhisURL();
    OkHttpClient client = UnsafeOkHttpsClientFactory.getUnsafeOkHttpClient();
    // connect timeout
    client.setConnectTimeout(30, TimeUnit.SECONDS);
    // socket timeout
    client.setReadTimeout(30, TimeUnit.SECONDS);
    // write timeout
    client.setWriteTimeout(30, TimeUnit.SECONDS);
    // Cancel retry on failure
    client.setRetryOnConnectionFailure(false);
    BasicAuthenticator basicAuthenticator = new BasicAuthenticator();
    client.setAuthenticator(basicAuthenticator);
    RequestBody body = RequestBody.create(JSON, data.toString());
    Request request = new Request.Builder().header(basicAuthenticator.AUTHORIZATION_HEADER, basicAuthenticator.getCredentials()).url(DHIS_URL).post(body).build();
    try {
        response = client.newCall(request).execute();
    } catch (IOException e) {
        throw new ApiCallException(e);
    }
    return ServerApiUtils.getApiResponseAsJSONObject(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) RequestBody(com.squareup.okhttp.RequestBody)

Example 97 with Request

use of com.squareup.okhttp.Request 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 98 with Request

use of com.squareup.okhttp.Request 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 99 with Request

use of com.squareup.okhttp.Request 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)

Example 100 with Request

use of com.squareup.okhttp.Request in project actor-platform by actorapp.

the class AndroidHttpProvider method getMethod.

@Override
public Promise<HTTPResponse> getMethod(String url, int startOffset, int size, int totalSize) {
    return new Promise<>(resolver -> {
        final Request request = new Request.Builder().url(url).addHeader("Range", "bytes=" + startOffset + "-" + (startOffset + size)).build();
        Log.d(TAG, "Downloading part: " + request.toString());
        client.newCall(request).enqueue(new Callback() {

            @Override
            public void onFailure(Request request, IOException e) {
                Log.d(TAG, "Downloading part error: " + request.toString());
                e.printStackTrace();
                // TODO: Better error?
                resolver.error(new HTTPError(0));
            }

            @Override
            public void onResponse(Response response) throws IOException {
                Log.d(TAG, "Downloading part response: " + request.toString() + " -> " + response.toString());
                if (response.code() >= 200 && response.code() < 300) {
                    resolver.result(new HTTPResponse(response.code(), response.body().bytes()));
                } else {
                    resolver.error(new HTTPError(response.code()));
                }
            }
        });
    });
}
Also used : Response(com.squareup.okhttp.Response) HTTPResponse(im.actor.runtime.http.HTTPResponse) Promise(im.actor.runtime.promise.Promise) HTTPError(im.actor.runtime.http.HTTPError) Callback(com.squareup.okhttp.Callback) HTTPResponse(im.actor.runtime.http.HTTPResponse) Request(com.squareup.okhttp.Request) IOException(java.io.IOException)

Aggregations

Request (com.squareup.okhttp.Request)109 Response (com.squareup.okhttp.Response)74 IOException (java.io.IOException)61 OkHttpClient (com.squareup.okhttp.OkHttpClient)38 RequestBody (com.squareup.okhttp.RequestBody)31 FormEncodingBuilder (com.squareup.okhttp.FormEncodingBuilder)19 UnsupportedEncodingException (java.io.UnsupportedEncodingException)12 File (java.io.File)11 Callback (com.squareup.okhttp.Callback)10 InputStream (java.io.InputStream)7 Buffer (okio.Buffer)7 HttpUrl (com.squareup.okhttp.HttpUrl)5 MediaType (com.squareup.okhttp.MediaType)5 ResponseBody (com.squareup.okhttp.ResponseBody)5 SocketTimeoutException (java.net.SocketTimeoutException)5 Call (com.squareup.okhttp.Call)4 FileOutputStream (java.io.FileOutputStream)4 Activity (android.app.Activity)3 Intent (android.content.Intent)3 Uri (android.net.Uri)3