Search in sources :

Example 26 with OkHttpClient

use of com.squareup.okhttp.OkHttpClient 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 27 with OkHttpClient

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

the class WebContentGetTask method doInBackground.

@Override
protected String doInBackground(String... params) {
    OkHttpClient client = new OkHttpClient();
    Cache cache;
    File tmpFile = new File(GlobalContext.getInstance().getCacheDir().getPath(), ".webview_cache");
    try {
        cache = new Cache(tmpFile, 10 * 60 * 60);
        client.setCache(cache);
    } catch (Exception e) {
    }
    if (params[0] == null) {
        throw new IllegalStateException("set url before execute!");
    }
    Request request = new Request.Builder().url(params[0]).build();
    try {
        return client.newCall(request).execute().body().string();
    } catch (IOException e) {
        return null;
    }
}
Also used : OkHttpClient(com.squareup.okhttp.OkHttpClient) Request(com.squareup.okhttp.Request) IOException(java.io.IOException) File(java.io.File) IOException(java.io.IOException) Cache(com.squareup.okhttp.Cache)

Example 28 with OkHttpClient

use of com.squareup.okhttp.OkHttpClient in project Android-CleanArchitecture by android10.

the class ApiConnection method connectToApi.

private void connectToApi() {
    OkHttpClient okHttpClient = this.createClient();
    final Request request = new Request.Builder().url(this.url).addHeader(CONTENT_TYPE_LABEL, CONTENT_TYPE_VALUE_JSON).get().build();
    try {
        this.response = okHttpClient.newCall(request).execute().body().string();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : OkHttpClient(com.squareup.okhttp.OkHttpClient) Request(com.squareup.okhttp.Request) IOException(java.io.IOException)

Example 29 with OkHttpClient

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

the class ImageBinPoster method post.

/**
     * Post the image to ImageBin
     *
     * @param bytes Bytes of the image to post
     * @param callback Request callback
     */
public static void post(byte[] bytes, Callback callback) {
    RequestBody requestBody = new MultipartBuilder().type(MultipartBuilder.FORM).addFormDataPart("file", "test", RequestBody.create(MediaType.parse("image/*"), bytes)).build();
    Request request = new Request.Builder().url("https://imagebin.ca/upload.php").post(requestBody).build();
    OkHttpClient client = new OkHttpClient();
    Call call = client.newCall(request);
    call.enqueue(callback);
}
Also used : Call(com.squareup.okhttp.Call) OkHttpClient(com.squareup.okhttp.OkHttpClient) Request(com.squareup.okhttp.Request) MultipartBuilder(com.squareup.okhttp.MultipartBuilder) RequestBody(com.squareup.okhttp.RequestBody)

Example 30 with OkHttpClient

use of com.squareup.okhttp.OkHttpClient in project FloatingSearchView by renaudcerrato.

the class RetrofitModule method provideHttpClient.

@Provides
@Singleton
OkHttpClient provideHttpClient() {
    HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
    logging.setLevel(HttpLoggingInterceptor.Level.BODY);
    OkHttpClient httpClient = new OkHttpClient();
    httpClient.interceptors().add(logging);
    return httpClient;
}
Also used : OkHttpClient(com.squareup.okhttp.OkHttpClient) HttpLoggingInterceptor(com.squareup.okhttp.logging.HttpLoggingInterceptor) Singleton(javax.inject.Singleton) Provides(dagger.Provides)

Aggregations

OkHttpClient (com.squareup.okhttp.OkHttpClient)47 Request (com.squareup.okhttp.Request)22 Response (com.squareup.okhttp.Response)18 IOException (java.io.IOException)15 RequestBody (com.squareup.okhttp.RequestBody)8 Provides (dagger.Provides)6 Gson (com.google.gson.Gson)5 Cache (com.squareup.okhttp.Cache)5 Singleton (javax.inject.Singleton)5 SpringAndroidSpiceRequest (com.octo.android.robospice.request.springandroid.SpringAndroidSpiceRequest)4 File (java.io.File)4 RestAdapter (retrofit.RestAdapter)4 OkClient (retrofit.client.OkClient)4 SSLContext (javax.net.ssl.SSLContext)3 X509TrustManager (javax.net.ssl.X509TrustManager)3 HttpResponse (org.apache.http.HttpResponse)3 Intent (android.content.Intent)2 SharedPreferences (android.content.SharedPreferences)2 Call (com.squareup.okhttp.Call)2 Dispatcher (com.squareup.okhttp.Dispatcher)2