okhttp

客户端远程调用Feign

戏子无情 提交于 2021-02-19 10:48:11
客户端远程调用 Feign 什么是Feign? Feign是 Netflix 公司开源的声明式HTTP客户端 Github : Feign 源码 为什么需要Feign? 原代码可读性不高 复杂的URL难以维护( https://user-center/s?wd={userId}&rsv_spt=1&rsv_iqid=0x93bff3cd000cf3da&issp=1&f=8&rsv_bp=1&rsv_idx=2&ie=utf-8&tn=baiduhome_pg&rsv_enter=1&rsv_sug3=4&rsv_sug1=4&rsv_sug7=100&rsv_t=10c2risCimsUZC0RBruMerdnQRN1gRxFI%2BywuD0L3LwGGNd2dR8XE6x%2FyFOjHnR0oEi0&rsv_sug2=0&inputT=1535&rsv_sug4=1535&rsv_sug=2 ) 难以应对需求的快速变化 编码体验和我们写JAVA差异较大 举例重构代码 //替换前 ResponseEntity<UserDTO> userEntity = restTemplate.getForEntity( "http://user-center/users/{userId}", UserDTO.class, userId ); UserDTO userDTO = new

android -------- java.net.UnknownServiceException

我的梦境 提交于 2021-02-19 01:58:30
最近升级了Android的API版本时 ,导致我的网络请求失败了, 出现了这个错误 java.net.UnknownServiceException, 这个错误,我在网上查到这个主要是由于,我们的OkHttp3会默认使用密文传输,而我们的代码中使用Http协议,也就是使用明文传输,所以OkHttp3会主动的报错,然后阻止线程的运行。所以我们现在就是要修改配置文件,使OkHttp3允许使用明文传输,或者我们直接使用Https协议。 解决方法: 在 res 下新建一个 xml 目录,然后创建一个名为:network_security_config.xml 文件 该文件内容如下: <?xml version="1.0" encoding="utf-8"?> <network-security-config> <base-config cleartextTrafficPermitted="true" /> </network-security-config> 然后在 AndroidManifest.xml application 标签内应用上面的xml配置: <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android

OkHttp enable/disable gzip compression on requests

懵懂的女人 提交于 2021-02-18 20:59:42
问题 I'm using Retrofit to manage my requests and want to make some tests to check de request size using or not using gzip. By default does OkHttp performs gzip compression to requests or it must be implemented with an interceptor? I've added @Headers({ "Accept-Encoding: gzip, deflate", "Content-Encoding: gzip" }) or: @Headers({ "Content-Type: application/json;charset=utf-8", "Accept: application/json" }) to my requests and did not see any change on the request length. 回答1: OkHttp will do

Android 在 4G 下访问 IPV6 慢的解决方案

穿精又带淫゛_ 提交于 2021-02-18 17:54:45
Android 在 4G 下访问 IPV6 慢的解决方案 Android 4G ipv6 起因 今天,用户反馈 Android 端加载数据较慢,经 Android 开发人员排查后,发现在公司 wifi 下接口响应时间在 50ms 左右,而在 4G 网络下,接口响应时间在 600ms 左右,甚至于 1s 以上,Android 端认为是服务端问题,遂反馈到服务端 排查 首先检查 nginx 日志,发现不管是 wifi 网络下还是 4G 网络下,服务端的响应时间均在 30ms 左右,所以排除掉了代码的问题 向运维同学咨询了一下服务端的网络架构,原来为了 IOS 的 appstore 审核,服务端增加了 ipv6 支持,而 ipv6 的服务入口在美国,如下图: 网络架构 所以怀疑 Android 端解析域名时解析到两个 IP 后,优先使用 IPV6 连接的后端服务 验证 使用如下代码,验证 DNS 解析的 IP 地址 try { InetAddress[] inetAddresses = InetAddress.getAllByName( "server.xxxx.cn" ); for (InetAddress inetAddress : inetAddresses){ System.out.println(inetAddress.getHostAddress()); } } catch

How to change timeout for a request in okhttp

喜欢而已 提交于 2021-02-18 11:00:39
问题 In general we set timeout for the okHttp client and we use single instance of that client. So, we can't change the timeout for that client once it's generated. How to change the timeout for a particular request ?? Is there anyway to do it without creating new client?? It's very common that some calls take more time atleast 1/2 per app, which needs more timeout than others. it would be great if request can override the default timeout. 回答1: In 3.9 it is possible to set this per request in an

How to change timeout for a request in okhttp

巧了我就是萌 提交于 2021-02-18 11:00:28
问题 In general we set timeout for the okHttp client and we use single instance of that client. So, we can't change the timeout for that client once it's generated. How to change the timeout for a particular request ?? Is there anyway to do it without creating new client?? It's very common that some calls take more time atleast 1/2 per app, which needs more timeout than others. it would be great if request can override the default timeout. 回答1: In 3.9 it is possible to set this per request in an

How to change timeout for a request in okhttp

£可爱£侵袭症+ 提交于 2021-02-18 11:00:28
问题 In general we set timeout for the okHttp client and we use single instance of that client. So, we can't change the timeout for that client once it's generated. How to change the timeout for a particular request ?? Is there anyway to do it without creating new client?? It's very common that some calls take more time atleast 1/2 per app, which needs more timeout than others. it would be great if request can override the default timeout. 回答1: In 3.9 it is possible to set this per request in an

Okhttp check file size without dowloading the file

回眸只為那壹抹淺笑 提交于 2021-02-18 07:42:32
问题 The common examples for okhttp cover the scenarios of get and post. But I need to get the file size of a file with a url. Since I need to inform the the user, and only after getting their approval to download the file. Currently I am using this code URL url = new URL("http://server.com/file.mp3"); URLConnection urlConnection = url.openConnection(); urlConnection.connect(); int file_size = urlConnection.getContentLength(); mentioned in this stackoverflow question How to know the size of a file

Okhttp check file size without dowloading the file

[亡魂溺海] 提交于 2021-02-18 07:42:22
问题 The common examples for okhttp cover the scenarios of get and post. But I need to get the file size of a file with a url. Since I need to inform the the user, and only after getting their approval to download the file. Currently I am using this code URL url = new URL("http://server.com/file.mp3"); URLConnection urlConnection = url.openConnection(); urlConnection.connect(); int file_size = urlConnection.getContentLength(); mentioned in this stackoverflow question How to know the size of a file

OkHttp doesn't redirect POST requests when used with retrofit

偶尔善良 提交于 2021-02-18 06:41:28
问题 Using retrofit I want to make POST request to http://milzinas.lt/oauthsilent/authorize . This URL is special because it redirects you to http://milzinas.e-bros.lt/oauthsilent/authorize . My retrofit setup uses OkHttpClient . If I make request using OkHttpClient only then redirecting works fine, i.e. 401 status code is received. However, when I use the same OkHttpClient with retrofit then response is status code 307. I think this has something to do with OkClient implementation which wraps the