改造不适用于特定版本的机器人

我在运行Android 4.3的模拟器上遇到改造问题,我的设备在Android 4.4.2上,而相同的代码在另一个带有Android 7.1.1的模拟器上正常运行

每次尝试执行请求时,我都会收到一个超时异常。get

java.net.SocketTimeoutException: failed to connect to jsonplaceholder.typicode.com/2606:4700:30::681c:3f5 (port 443) after 10000ms
        at libcore.io.IoBridge.connectErrno(IoBridge.java:159)
        at libcore.io.IoBridge.connect(IoBridge.java:112)
        at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
        at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
        at java.net.Socket.connect(Socket.java:842)
        at okhttp3.internal.platform.AndroidPlatform.connectSocket(AndroidPlatform.java:73)
        at okhttp3.internal.connection.RealConnection.connectSocket(RealConnection.java:246)
        at okhttp3.internal.connection.RealConnection.connect(RealConnection.java:166)
        at okhttp3.internal.connection.StreamAllocation.findConnection(StreamAllocation.java:257)

代码如下


public interface Api {

    String BASE_URL = "https://jsonplaceholder.typicode.com/";


    @GET("posts")

    Call<ArrayList<Post>> getPosts();

}

和对 API 的调用


Retrofit retrofit = new Retrofit.Builder()

        .baseUrl(Api.BASE_URL)

        .addConverterFactory(GsonConverterFactory.create())

        .build();


Api api = retrofit.create(Api.class);

Call<ArrayList<Post>> call = api.getPostes();

Log.i("RequestUrl", call.request().url().toString());

call.enqueue(new Callback<ArrayList<Post>>() {

    @Override

    public void onResponse(Call<ArrayList<Post>> call, Response<ArrayList<Post>> response) {

        mPostsList.setValue(response.body());

    }


    @Override

    public void onFailure(Call<ArrayList<Post>> call, Throwable t) {

        Log.e("Posts", "Error occurred", t);

    }

});


幕布斯7119047
浏览 117回答 3
3回答

撒科打诨

它读取 ,最初建议提高客户端的连接超时值,正如本答案中所解释的那样 - 但是在查看当前的源代码时...这暗示了不兼容的协议。java.net.SocketTimeoutExceptionokhttp3.internal.platform.AndroidPlatform服务器的SSL证书支持 ,因为这是安卓4.x所必需的(他们这边没有问题);问题在于,当前版本的 不再支持,因此握手永远不会发生(这就是为什么它抛出这样的误导而不是)。TLS 1.0OkHttp3TLS 1.0SocketTimeoutExceptionSSLHandshakeException使用 ,仍应支持默认配置OkHttp33.12.xMODERN_TLS&nbsp;-但是可以指示使用配置来代替:OkHttp33.13.xCOMPATIBLE_TLS/* ConnectionSpec.MODERN_TLS is the default value */List tlsSpecs = Arrays.asList(ConnectionSpec.MODERN_TLS);/* providing backwards-compatibility for API lower than Lollipop: */if(Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {&nbsp; &nbsp; tlsSpecs = Arrays.asList(ConnectionSpec.COMPATIBLE_TLS);}OkHttpClient client = new OkHttpClient.Builder()&nbsp; &nbsp; .connectionSpecs(tlsSpecs)&nbsp; &nbsp; .build();还必须将其设置为以下客户端:RetrofitRetrofit&nbsp;retrofit&nbsp;=&nbsp;new&nbsp;Retrofit.Builder() &nbsp;&nbsp;&nbsp;&nbsp;.baseUrl(Api.BASE_URL) &nbsp;&nbsp;&nbsp;&nbsp;.addConverterFactory(GsonConverterFactory.create()) &nbsp;&nbsp;&nbsp;&nbsp;.setClient(client) &nbsp;&nbsp;&nbsp;&nbsp;.build();请参阅&nbsp;TLS 配置历史记录,了解每个版本的可用协议支持。看起来,甚至已经支持 ,因为将来需要 Android 。它甚至可能不需要降级,因为仍然有支撑,而在它已经被移入;仍然不确定。OkHttp33.12.xTLS 1.3QOkHttp3MODERN_TLS3.12.xTLSv13.13.xCOMPATIBLE_TLS3.14.x即使使用 的当前版本,仍然可以将所需的协议添加回去,因为这是一个带有方法的方法 - 没有任何保证,不会有进一步的不兼容性; 可能仍然是支持Android 4.x版本的最佳选择,甚至可能有新功能的后移植。OkHttp3TLS 1.0ConnectionSpec.COMPATIBLE_TLSArrayList.add()3.12.x

森栏

如果您使用安卓 9(饼图)或高于 28 的安卓 SDK,并通过 Api 调用通过改造获得问题。将此行添加到清单改造问题android:usesCleartextTraffic="true"

UYOU

21之前的安卓有一些缺少SSL和改造将不起作用。使用谷歌服务,您可以在HTTP请求工作后更新设备协议&nbsp; &nbsp; //compile 'com.google.android.gms:play-services-base:11.0.0'&nbsp; &nbsp; &nbsp;//remember to add the library in your dependencies&nbsp; &nbsp; &nbsp; &nbsp; //compile 'com.google.android.gms:play-services-base:$currentVersion'&nbsp; &nbsp; &nbsp; &nbsp; ProviderInstaller.installIfNeededAsync(this, new ProviderInstallListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onProviderInstalled() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Do your http request here&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onProviderInstallFailed(int errorCode, Intent recoveryIntent) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //sad face :C is sad&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java