okhttp

okhttp3 以上版本在安卓9.0无法请求数据的解决方案

萝らか妹 提交于 2019-12-05 20:03:59
应用官方的说明:在 Android 6.0 中,我们取消了对 Apache HTTP 客户端的支持。 从 Android 9 开始,默认情况下该内容库已从 bootclasspath 中移除且不可用于应用。且Android P 限制了明文流量的网络请求,非加密的流量请求都会被系统禁止掉。 错误日志: CLEARTEXT communication to XXXXXXXX not permitted by network security policy 解决: 在res目录下新建xml文件夹,文件夹中新建文件network_security_config.xml,文件内容如下 <network-security-config> <base-config cleartextTrafficPermitted="true" /> </network-security-config> 在AndroidManifest.xml文件中,Application标签下添加如下属性: android:networkSecurityConfig="@xml/network_security_config" 完美解决 - 来源: oschina 链接: https://my.oschina.net/u/2663142/blog/3062593

Android高版本联网失败报错:Cleartext HTTP traffic to xxx not permitted解决方法

怎甘沉沦 提交于 2019-12-05 20:03:45
前言:为保证用户数据和设备的安全,Google针对下一代 Android 系统(Android P) 的应用程序,将要求默认使用加密连接,这意味着 Android P 将禁止 App 使用所有未加密的连接,因此运行 Android P 系统的安卓设备无论是接收或者发送流量,未来都不能明码传输,需要使用下一代(Transport Layer Security)传输层安全协议,而 Android Nougat 和 Oreo 则不受影响。 因此在Android P 使用HttpUrlConnection进行http请求会出现以下异常 W/System.err: java.io.IOException: Cleartext HTTP traffic to **** not permitted 使用OKHttp请求则出现 java.net.UnknownServiceException: CLEARTEXT communication ** not permitted by network security policy 在Android P系统的设备上,如果应用使用的是非加密的明文流量的http网络请求,则会导致该应用无法进行网络请求,https则不会受影响,同样地,如果应用嵌套了webview,webview也只能使用https请求。 针对这个问题,有以下三种解决方法: (1

NoSuchMethodError if i am using okhttp 2.0 and the latest retrofit?

隐身守侯 提交于 2019-12-05 19:04:19
问题 Could not find method com.squareup.okhttp.OkHttpClient.open, referenced from method retrofit.client.OkClient.openConnection. below is my gradle config compile 'com.squareup.okhttp:okhttp:+' compile 'com.squareup.okhttp:okhttp-urlconnection:+' compile 'com.squareup.retrofit:retrofit:+' 回答1: Ok, square has released 2.0 RC2 on github, but not on maven: https://github.com/square/okhttp But you still need okhttp-urlconnection (RC1 right now) which is only on maven: http://mvnrepository.com

java.net.ProtocolException: unexpected end of stream

女生的网名这么多〃 提交于 2019-12-05 17:15:45
问题 I am facing a strange issue, and I am not able to debug it out. I have implemented a logic for uploading stream of data and am using Volley for the same, I have customized a logic little bit in HurlStack , addBodyIfExists api,so that body of type "application/octet-stream" can be handled. My logic is to post progress to user, so that UI can be updated indicating user progress in upload, below my logic for same. int toRead = length; // File length byte[] data = new byte[4096]; connection

springboot 使用RestTemplate+okhttp连接池发送http消息

瘦欲@ 提交于 2019-12-05 14:10:40
1、简介 OkHttp是一个高效的HTTP客户端,允许所有同一个主机地址的请求共享同一个socket连接;连接池减少请求延时;透明的GZIP压缩减少响应数据的大小;缓存响应内容,避免一些完全重复的请求 当网络出现问题的时候OkHttp依然坚守自己的职责,它会自动恢复一般的连接问题,如果你的服务有多个IP地址,当第一个IP请求失败时,OkHttp会交替尝试你配置的其他IP,OkHttp使用现代TLS技术(SNI, ALPN)初始化新的连接,当握手失败时会回退到TLS 1.0。 1)使用:它的请求/响应 API 使用构造器模式builders来设计,它支持阻塞式的同步请求和带回调的异步请求。 <dependency> <groupId>com.squareup.okhttp3</groupId> <artifactId>okhttp</artifactId> <version>3.10.0</version> </dependency> import okhttp3.ConnectionPool; import okhttp3.OkHttpClient; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;

springboot 使用RestTemplate+httpclient连接池发送http消息

夙愿已清 提交于 2019-12-05 14:06:48
简介 RestTemplate是spring支持的一个请求http rest服务的模板对象,性质上有点像jdbcTemplate 是Spring用于同步client端的核心类,简化了与http服务的通信,并满足RestFul原则,程序代码可以给它提供URL,并提取结果。默认情况下,RestTemplate默认依赖jdk的HTTP连接工具。当然你也可以 通过setRequestFactory属性切换到不同的HTTP源,比如Apache HttpComponents、Netty和OkHttp。本章讨论的就是设置RestTemplate底层使用的httpclient(org.apache.http.client.HttpClient)发送请求 HttpClient可以做连接池,而发送消息的工具类可以使用RestTemplate,所以如果你的项目需求http连接池,RestTemplate+httpclient连接池是一种不错的方式,可以节省开发工作,也可以更优雅的使用。 配置 maven依赖 <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.6</version> </dependency> <dependency>

OKhttp : SSLProtocolException: SSL handshake terminated

旧时模样 提交于 2019-12-05 13:47:19
I'm trying to figure out why sometimes I get this error javax.net.ssl.SSLProtocolException: SSL handshake terminated: ssl=0x60d46c90: Failure in SSL library, usually a protocol error error:1409443E:SSL routines:SSL3_READ_BYTES:tlsv1 alert inappropriate fallback (external/openssl/ssl/s3_pkt.c:1256 0x60d57f40:0x00000003) the code that makes this request: private <T> void send(final String url, final Map<String, String> args, final RequestCallback<T> callback, final Parser<T> pParser, final Context pContext, final HTTP_METHOD method, final Map<String, String> headers, final BODY_TYPE bodyType) {

retrofit 2.0b2 : How to get InputStream from the response?

不羁岁月 提交于 2019-12-05 13:05:51
问题 I'm using Retrofit 2.0b2. After getting a response, I tried getting an InputStream from the response by : Response<JsonNode> response = call.execute(); InputStream is = response.raw().body().byteStream(); but the app keep throwing : java.lang.IllegalStateException: Cannot read raw response body of a converted body. at retrofit.OkHttpCall$NoContentResponseBody.source(OkHttpCall.java:184) at com.squareup.okhttp.ResponseBody.byteStream(ResponseBody.java:43) at ... Despite the response returned

How to access context in a Interceptor?

≯℡__Kan透↙ 提交于 2019-12-05 12:37:18
问题 I would like to save some stuff on the SharedPreferences while being on a Interceptor. I can't find a way to do it because i can't find a way to access the context on the Interceptor (so not possible to use PreferencesManager, etc). public class CookieInterceptor implements Interceptor { @Override public Response intercept(Chain chain) throws IOException { PreferenceManager.getDefaultSharedPreferences(Context ??) }} Any ideas ? I'm using AndroidStudio & last version of OkHttp. Thanks ;) 回答1:

HTTP FAILED: java.net.SocketException: Socket closed; doesn't fire up exception handling methods

て烟熏妆下的殇ゞ 提交于 2019-12-05 10:51:53
I have an RxJava chain request that should release some locks onError(), or onComplete(), so, basically, what my problem is: when I set read, connect and write timeouts to my OkHttpClient, I don't get the desired behavior. I'm using Retrofit2 and OkHttp3.6.0 Here's my simplified client: OkHttpClient.Builder builder = new OkHttpClient.Builder() .readTimeout(30, TimeUnit.SECONDS) .connectTimeout(30, TimeUnit.SECONDS) .writeTimeout(30, TimeUnit.SECONDS) OkHttpClient okHttpClient = builder.build(); Here's a simplified version of the chain request I have: public <T extends Response> Observable<T>