okhttp

java.lang.NoClassDefFoundError: com.squareup.okhttp.MediaType

亡梦爱人 提交于 2019-12-06 12:45:52
I am getting this error while changing my code from default HTTPClient to OkHTTP . Searched all through the web but it is not getting resolved. I tried the same with OkHTTP library from here and also with dependency but still the error is same. Please help me to resolve this. The Errors are: 01-01 22:03:18.038: E/AndroidRuntime(1460): java.lang.RuntimeException: An error occured while executing doInBackground() 01-01 22:03:18.038: E/AndroidRuntime(1460): at android.os.AsyncTask$3.done(AsyncTask.java:300) 01-01 22:03:18.038: E/AndroidRuntime(1460): at java.util.concurrent.FutureTask

Android - OKHttp: how to enable gzip for POST

ⅰ亾dé卋堺 提交于 2019-12-06 12:07:36
问题 In our Android App, I'm sending pretty large files to our (NGINX) server so I was hoping to use gzip for my Retrofit POST message. There are many documentations about OkHttp using gzip transparently or changing the headers in order to accept gzip (i.e. in a GET message). But how can I enable this feature for sending gzip http POST messages from my device? Do I have to write a custom Intercepter or something? Or simply add something to the headers? 回答1: According to the following recipe: The

OKHTTP newCall() hangs when calling restAPI

試著忘記壹切 提交于 2019-12-06 11:27:36
Trying to post to a restAPI using JAVA OKHttp. My code looks as follows: try { loggingInterceptor = new HttpLoggingInterceptor(); loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY); client = new OkHttpClient.Builder() .connectTimeout(10, TimeUnit.SECONDS) .addInterceptor(loggingInterceptor) .build(); MediaType mediaType = MediaType.parse("app.lication/json"); RequestBody body = RequestBody.create(mediaType, FileUtils.readFileToString(file)); Request request = new Request.Builder() .url(restAPIUrl) .post(body) .addHeader("content-type", "application/json") .addHeader("cache-control"

When using http2 in okhttp, why multi requests to the same host didn't use just one connectoin

岁酱吖の 提交于 2019-12-06 07:24:05
问题 I would like to test okhttp's http2 function. And I make multi requsts to same host in async style. But, I found that, it involved multi connections, since the protocol is h2, It should use just one connection, right? The code is below. Ah, I'm using okhttp2.5 public class Performance { private final OkHttpClient client = new OkHttpClient(); private final Dispatcher dispatcher = new Dispatcher(); private final int times = 20; public Performance(){ dispatcher.setMaxRequestsPerHost(2); client

Callback failure for canceled call in OkHttp

别说谁变了你拦得住时间么 提交于 2019-12-06 06:31:36
Canceling enqueued calls in OkHttp (version 2.4) client sometimes throws Callback failure for canceled call to http://... and neither of callback methods gets executed. Since I have to provide either positive or negative feedback to the user, having callback eaten up like that is not acceptable. How can I get around that error? I have created test application with two buttons (one for starting download request, another for canceling it), text view and image view, that shows error most of the time. Originally, I have caught error clicking on Cancel button, but using auto-click with handler is

Android4种网络连接方式HttpClient、HttpURLConnection、OKHttp和Volley优缺点

半世苍凉 提交于 2019-12-06 04:11:10
.. 1.HttpClient 优点:apache httpclient高效稳定,有很多API 缺点:由于API太多,很难在不破坏兼容性的情况下对它进行升级和扩展,维护成本高,故android 开发团队不愿意在维护该库而是转投更为轻便的httpurlconnection Apache HttpClient早就不推荐httpclient,5.0之后干脆废弃,后续会删除。6.0删除了HttpClient。Java开发用HttpClient,官方推荐Android开发用HttpUrlConnection。 2.HttpURLConnection 优点:HttpURLConnection是一种多用途、轻量极的HTTP客户端,使用它来进行HTTP操作可以适用于大多数的应用程序。虽然HttpURLConnection的API提供的比较简单,但是同时这也使得我们可以更加容易地去使用和扩展它。比较轻便,灵活,易于扩展。 缺点:在Android 2.2版本之前,HttpURLConnection一直存在着一些令人厌烦的bug。比如说对一个可读的InputStream调用close()方法时,就有可能会导致连接池失效了。那么我们通常的解决办法就是直接禁用掉连接池的功能。 在android 2.2及以下版本中HttpUrlConnection存在着一些bug,所以建议在android 2

Spring Cloud OpenFeign 切换为 Okhttp3 提升 QPS

妖精的绣舞 提交于 2019-12-06 02:21:19
Spring Cloud OpenFeign 切换为 Okhttp3 提升 QPS 直接进入干货区: # 引入 okhttp3 compile("io.github.openfeign:feign-okhttp") compile("com.squareup.okhttp3:okhttp") # 配置 application.yml feign: # feign启用hystrix,才能熔断、降级 # hystrix: # enabled: true # 启用 okhttp 关闭默认 httpclient httpclient: enabled: false okhttp: enabled: true # 请求与响应的压缩以提高通信效率 compression: request: enabled: true min-request-size: 2048 mime-types: text/xml,application/xml,application/json response: enabled: true # okhttp3 参数配置 /** * 配置 okhttp 与连接池 * ConnectionPool 默认创建5个线程,保持5分钟长连接 * * @author http://mybatis.plus * @since 2019-08-19 */ @Configuration

老外写的关于协程的性能文章-主打http协议

99封情书 提交于 2019-12-06 00:11:25
https://dzone.com/articles/high-concurrency-http-clients-on-the-jvm HTTP is probably the most popular application-level protocol and there are many libraries that implement it on top of network I/O, which is a special (stream-oriented) case of general I/O. Since all I/O has a much in common, let’s start with some discussion about it. I’ll concentrate on I/O cases with a lots of concurrent HTTP requests, for example micro-services, where a set of higher-level HTTP services invoke several lower-level ones, some concurrently and some sequentially due to data dependencies. When serving many such

android课程表控件、悬浮窗、Todo应用、MVP框架、Kotlin完整项目源码

独自空忆成欢 提交于 2019-12-05 23:47:26
Android精选源码 Android游戏2048 MVP Kotlin项目(RxJava+Rerotfit+OkHttp+Glide) Android基于自定义Span的富文本编辑器 android课程表控件效果源码 Dagger、Clean、MVP框架搭建,快速开发~ Andorid 任意界面悬浮窗,实现悬浮窗如此简单 android模仿QQ登录后保存账号和密码效果源码 Android简洁清爽的Todo清单工具(MVP+okhttp3+retrofit+gson) Android优质博客 Kotlin Android Extensions+Android MVP项目 Kotlin编写MVP案例使用Kotlin编程实现一个MVP案例,实现电影列表功能:前期准备:项目中,在Gradle中引入框架的配置如下和使用Kotlin Android扩展:apply plugin: 'com.android.application'apply plugin: 'kotlin-android'app... 阅读原文 适配可折叠设备,您的应用准备好了吗? 作者: Leo Sei, Android 产品经理我们在 11 月初举办了 Android 开发者峰会,并在会上宣布 Android 将对折叠屏设备 (Foldable) 提供原生支持,帮助开发者为三星等硬件合作伙伴即将推出的新设备做好准备

How can I extract the raw JSON string from an OkHttp Response object?

廉价感情. 提交于 2019-12-05 22:29:10
private static final String URL = "http://www.livroandroid.com.br/livro/carros/carros_{tipo}.json"; public static List<Carro> getCarros(Context context, String tipo) throws IOException { String url = URL.replace("{tipo}", tipo); OkHttpClient okHttpClient = new OkHttpClient(); Request request = new Request.Builder() .url(URL) .build(); Response response = okHttpClient.newCall(request).execute(); String json = response.body().toString(); List<Carro> carros = parserJSON(context, json); return carros; } If I print out the value of the json variable when calling the getCarros method, I see the