Search in sources :

Example 46 with HttpUrl

use of okhttp3.HttpUrl in project gh4a by slapperwan.

the class HttpImageGetter method loadImageForUrl.

private Drawable loadImageForUrl(String source) {
    HttpUrl url = source != null ? HttpUrl.parse(source) : null;
    Bitmap bitmap = null;
    if (!mDestroyed && url != null) {
        File output = null;
        InputStream is = null;
        Request request = new Request.Builder().url(url).build();
        try (Response response = mClient.newCall(request).execute()) {
            is = response.body().byteStream();
            if (is != null) {
                MediaType mediaType = response.body().contentType();
                String mime = mediaType != null ? mediaType.toString() : null;
                if (mime == null) {
                    mime = URLConnection.guessContentTypeFromName(source);
                }
                if (mime == null) {
                    mime = URLConnection.guessContentTypeFromStream(is);
                }
                if (mime != null && mime.startsWith("image/svg")) {
                    bitmap = renderSvgToBitmap(mContext.getResources(), is, mWidth, mHeight);
                } else {
                    boolean isGif = mime != null && mime.startsWith("image/gif");
                    if (!isGif || canLoadGif()) {
                        output = File.createTempFile("image", ".tmp", mCacheDir);
                        if (FileUtils.save(output, is)) {
                            if (isGif) {
                                GifDrawable d = new GifDrawable(output);
                                d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
                                return d;
                            } else {
                                bitmap = getBitmap(output, mWidth, mHeight);
                            }
                        }
                    }
                }
            }
        } catch (IOException e) {
        // fall through to showing the error bitmap
        } finally {
            if (output != null) {
                output.delete();
            }
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                // ignored
                }
            }
        }
    }
    synchronized (this) {
        if (mDestroyed && bitmap != null) {
            bitmap.recycle();
            bitmap = null;
        }
    }
    if (bitmap == null) {
        return mErrorDrawable;
    }
    BitmapDrawable drawable = new LoadedBitmapDrawable(mContext.getResources(), bitmap);
    drawable.setBounds(0, 0, bitmap.getWidth(), bitmap.getHeight());
    return drawable;
}
Also used : InputStream(java.io.InputStream) Request(okhttp3.Request) GifDrawable(pl.droidsonroids.gif.GifDrawable) IOException(java.io.IOException) BitmapDrawable(android.graphics.drawable.BitmapDrawable) HttpUrl(okhttp3.HttpUrl) Response(okhttp3.Response) Bitmap(android.graphics.Bitmap) MediaType(okhttp3.MediaType) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

Example 47 with HttpUrl

use of okhttp3.HttpUrl in project ProxerLibJava by proxer.

the class ProxerTest method setUp.

@Before
public void setUp() throws IOException {
    server = new MockWebServer();
    client = new OkHttpClient.Builder().addInterceptor(chain -> {
        final HttpUrl oldUrl = chain.request().url();
        final HttpUrl serverUrl = server.url(oldUrl.encodedPath());
        final HttpUrl newUrl = oldUrl.newBuilder().scheme(serverUrl.scheme()).host(serverUrl.host()).port(serverUrl.port()).build();
        return chain.proceed(chain.request().newBuilder().url(newUrl).build());
    }).build();
    api = constructApi().build();
    server.start();
}
Also used : OkHttpClient(okhttp3.OkHttpClient) MockWebServer(okhttp3.mockwebserver.MockWebServer) HttpUrl(okhttp3.HttpUrl) Before(org.junit.Before)

Example 48 with HttpUrl

use of okhttp3.HttpUrl in project ProxerLibJava by proxer.

the class NotificationAdapter method fromJson.

@FromJson
Notification fromJson(final IntermediateNotification json) {
    final String base = ProxerUrls.webBase().toString();
    final HttpUrl properContentLink = HttpUrl.parse(base.substring(0, base.length() - 1) + json.contentLink);
    if (properContentLink == null) {
        throw new JsonDataException("Invalid link: " + json.contentLink);
    }
    return new Notification(json.id, json.type, json.contentId, properContentLink, json.text, json.date, json.additionalDescription);
}
Also used : JsonDataException(com.squareup.moshi.JsonDataException) HttpUrl(okhttp3.HttpUrl) Notification(me.proxer.library.entity.notifications.Notification) FromJson(com.squareup.moshi.FromJson)

Example 49 with HttpUrl

use of okhttp3.HttpUrl in project lzc_app_lib by httplzc.

the class HttpUtil method download.

// 下载文件
public static void download(String url, RequestParams params, FileDownloadCallBack fileDownloadCallBack, Object tag) {
    if (!validUrl(url)) {
        fileDownloadCallBack.onFailure(Invalid, null);
        return;
    }
    HttpUrl httpUrl = formatUrl(params, url);
    if (httpUrl == null) {
        fileDownloadCallBack.onFailure(Invalid, null);
        return;
    }
    try {
        Request.Builder builder = new Request.Builder().url(httpUrl).tag(tag);
        if (params != null)
            for (Map.Entry<String, String> stringStringEntry : params.getHeads().entrySet()) {
                builder.addHeader(stringStringEntry.getKey(), stringStringEntry.getValue());
            }
        Request request = builder.build();
        OkHttpInstance.getClient().newCall(request).enqueue(fileDownloadCallBack);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : Request(okhttp3.Request) HttpUrl(okhttp3.HttpUrl)

Example 50 with HttpUrl

use of okhttp3.HttpUrl in project lzc_app_lib by httplzc.

the class HttpUtil method get.

/**
 * get 有参数
 *
 * @param urlString
 * @param params
 * @param responseHandler
 */
public static void get(String urlString, RequestParams params, Callback responseHandler, Object tag) {
    HttpUrl httpUrl = formatUrl(params, urlString);
    if (httpUrl == null) {
        responseHandler.onFailure(null, null);
        return;
    }
    try {
        Request.Builder builder = new Request.Builder().url(httpUrl).tag(tag);
        if (params != null) {
            for (Map.Entry<String, String> stringStringEntry : params.getHeads().entrySet()) {
                builder.addHeader(stringStringEntry.getKey(), stringStringEntry.getValue());
            }
        }
        Request request = builder.build();
        OkHttpInstance.getClient().newCall(request).enqueue(responseHandler);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : Request(okhttp3.Request) Map(java.util.Map) HttpUrl(okhttp3.HttpUrl)

Aggregations

HttpUrl (okhttp3.HttpUrl)302 Request (okhttp3.Request)130 Test (org.junit.Test)86 Response (okhttp3.Response)69 IOException (java.io.IOException)61 MockResponse (okhttp3.mockwebserver.MockResponse)40 Cookie (okhttp3.Cookie)35 ArrayList (java.util.ArrayList)28 OkHttpClient (okhttp3.OkHttpClient)27 MockWebServer (okhttp3.mockwebserver.MockWebServer)26 InputStream (java.io.InputStream)21 InputStreamReader (java.io.InputStreamReader)20 HashMap (java.util.HashMap)19 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)19 RequestBody (okhttp3.RequestBody)18 JsonParseException (com.google.gson.JsonParseException)13 File (java.io.File)12 OAuthRequest (com.github.scribejava.core.model.OAuthRequest)10 List (java.util.List)10 Test (org.junit.jupiter.api.Test)10