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;
}
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();
}
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);
}
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();
}
}
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();
}
}
Aggregations