天道酬勤,学无止境

okhttp3

OKHttp handle 302

Every time I perform a OKHttp Post request on this site, the response code is 302 and the response body is: <html> <head> <title>Object moved</title> </head> <body> <h2>Object moved to <a href="/GradebookSummary.aspx">here</a>. </h2> </body> </html> Here is my code: OkHttpClient client = new OkHttpClient().newBuilder() .followRedirects(false) .followSslRedirects(false) .build(); MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded"); RequestBody body = RequestBody.create(mediaType, "checkCookiesEnabled=true&checkMobileDevice=false&checkStandaloneMode=false&checkTabletDevice

2022-01-18 08:49:27    分类:问答    java   android   okhttp   okhttp3   http-status-code-302

How to send string array data with okhttp MultipartBuilder data

I am an android developer. I develop an application. I have a problem. I want to send string array data with okhttp multipart data. But I can not do it. Because "addFormDataPart" method is required string variable. I can not use that method(addFormDataPart). I want to send string array data. What can I do? Please help me. My source code: private void String sendData(String serverURL, String[] arrayData){ RequestBody requestBody; requestBody = new MultipartBuilder() .type(MultipartBuilder.FORM) .addFormDataPart("arraydata", arrayData) .build(); }

2022-01-18 03:15:02    分类:问答    android   okhttp3

Caching error with Retrofit 2 and okhttp 3

I'm trying to cache HTTP responses from my company API but it looks like the app cannot access the cache directory: W/System.err: remove failed: ENOENT (No such file or directory) : /data/user/0/com.appname/cache/cache_file/journal.tmp W/System.err: java.net.UnknownHostException: Unable to resolve host "www.domain.com": No address associated with hostname I have followed this tutorial. Here is how I setup Retrofit (2.1.0): import lu.CompanyName.R; import lu.CompanyName.interfaces.CompanyNameAPI; import okhttp3.Cache; import okhttp3.Interceptor; import okhttp3.OkHttpClient; import okhttp3

2022-01-17 18:04:06    分类:问答    android   caching   retrofit2   okhttp3   offline-caching

使用 okhttp 分块的传输编码仅提供完整结果(Transfer Encoding chunked with okhttp only delivers full result)

问题 我正在尝试对分块端点获得一些见解,因此计划逐块打印服务器发送给我的内容。 我没有这样做,所以我写了一个测试,看看 OkHttp/Retrofit 是否按我的预期工作。 以下测试应该向控制台提供一些块,但我得到的只是完整的响应。 我什至让 OkHttp3 的 MockWebServer 向我发送块,我有点失去了我所缺少的东西。 我发现了这个改造问题条目,但答案对我来说有点模棱两可:Chunked Transfer Encoding Response class ChunkTest { @Rule @JvmField val rule = RxImmediateSchedulerRule() // custom rule to run Rx synchronously @Test fun `test Chunked Response`() { val mockWebServer = MockWebServer() mockWebServer.enqueue(getMockChunkedResponse()) val retrofit = Retrofit.Builder() .baseUrl(mockWebServer.url("/")) .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .client

2022-01-15 19:01:24    分类:技术分享    retrofit   rx-java2   okhttp3   chunked

Transfer Encoding chunked with okhttp only delivers full result

I am trying to get some insights on a chunked endpoint and therefore planned to print what the server sends me chunk by chunk. I failed to do so so I wrote a test to see if OkHttp/Retrofit are working as I expect it. The following test should deliver some chunks to the console but all I get is the full response. I am a bit lost what I am missing to even make the MockWebServer of OkHttp3 sending me chunks. I found this retrofit issue entry but the answer is a bit ambiguous for me: Chunked Transfer Encoding Response class ChunkTest { @Rule @JvmField val rule = RxImmediateSchedulerRule() //

2022-01-15 11:46:05    分类:问答    retrofit   rx-java2   okhttp3   chunked

OkHttp如何设置最大连接池大小(不是最大空闲连接)(OkHttp how to set maximum connection pool size (not max idle connections))

问题 在 OkHttp 中,我找不到设置硬性最大连接池大小的方法。 从文档 https://square.github.io/okhttp/3.x/okhttp/okhttp3/ConnectionPool.html 很明显,您可以设置最大空闲连接数,但不能设置整体最大值。 这意味着在高负载下它可以增长到超出任何限制。 有没有办法最大化池? 如果不是,为什么不呢? 回答1 连接要么处于活动状态并由特定的进行中的呼叫保持,要么处于空闲状态并在池中。 通过限制执行 HTTP 调用的线程数来限制连接总数。 如果您使用Call.execute() (同步),只需适当调整线程池的大小。 如果您使用的是Call.enqueue() (异步),则限制在Dispatcher中。

2022-01-15 08:42:36    分类:技术分享    connection-pooling   okhttp   okhttp3

改造将标签添加到原始请求对象(Retrofit Adding tag to the original request object)

问题 我正在尝试解决一个问题,我将进行几个异步调用,并根据原始请求执行一项任务。 为了解决这个问题,我尝试为每个请求添加一个标签,然后在成功响应后,我可以获取标签并根据标签采取行动。 在这里,我只使用 TAG 来识别原始请求。 问题 在调用 enqueue 方法之前,我将标记设置为原始请求。 但是当我在成功的回调中得到响应时,我得到了我没有设置的不同标签。 不知何故,请求对象本身作为标记对象出现在那里。 我不确定,怎么做??? 请检查下面的代码 - GitHubService gitHubService = GitHubService.retrofit.create(GitHubService.class); final Call<List<Contributor>> call = gitHubService.repoContributors("square", "retrofit"); // Set the string tag to the original request object. call.request().newBuilder().tag("hello").build(); call.enqueue(new Callback<List<Contributor>>() { @Override public void onResponse(Call<List

2022-01-15 08:05:32    分类:技术分享    android   retrofit2   okhttp3

OkHttp how to set maximum connection pool size (not max idle connections)

In OkHttp I cannot find a way to set a hard maximum connection pool size. From the documentation https://square.github.io/okhttp/3.x/okhttp/okhttp3/ConnectionPool.html it is clear that you can set a maximum idle connections, but not an overall max. That means that with high load it can grow beyond any limit. Is there a way to maximize the pool? If not, why not?

2022-01-15 05:20:07    分类:问答    connection-pooling   okhttp   okhttp3

Retrofit Adding tag to the original request object

I'm trying to solve a problem where I'll be making a couple of asynchronous calls and based on the original request, I'm performing a task. To solve this issue, I'm trying to add a TAG to each request and then on successful response, I can get the tag and take action based on the tag. Here, I'm using TAG only to identify the original request. Problem Before calling the enqueue method, I'm setting the tag to the original request. But when I get the response in the successful callback, I'm getting different tag that I didn't set. Somehow the request object itself is coming as the tag object

2022-01-15 04:07:57    分类:问答    android   retrofit2   okhttp3

几个新的 Proguard 在 Multidex 项目中发布 Google Play 服务 v10.2.6 到 v11.0.0(Several new Proguard issues Google Play services v10.2.6 to v11.0.0 in a Multidex project)

问题 我更新了我们的应用程序以使用最新的 Android Firebase SDK v11.0.0 (https://firebase.google.com/support/release-notes/android#20170607),突然我们出现了一堆新的 proguard 错误,仅供参考在使用proguard之前,一切都运行良好。 前: compile 'com.google.firebase:firebase-messaging:10.2.6' compile 'com.google.android.gms:play-services-vision:10.2.6' 后: compile 'com.google.firebase:firebase-messaging:11.0.0' compile 'com.google.android.gms:play-services-vision:11.0.0' 问题是它似乎不是一个简单的新缺失的 proguard 条目,但它似乎破坏了整个事情,突然其他几个库似乎受到了这种变化的影响。 这些是与 GMS 相关的 Proguard 问题: Note: the configuration keeps the entry point 'com.google.android.gms.ads.identifier

2022-01-14 17:43:43    分类:技术分享    android   firebase   google-play-services   android-proguard   okhttp3