毕加索:UnknownServiceException:未为客户端启用 CLEARTEXT 通信

Picasso: UnknownServiceException: CLEARTEXT communication not enabled for client

我在我的应用程序中使用 Picasso 2.5.2。它运行良好,但无法从第三方服务器之一加载图片。当我尝试从该站点加载图片时出现此错误:

java.net.UnknownServiceException: CLEARTEXT communication not enabled for client
at okhttp3.internal.connection.RealConnection.connect(RealConnection.java:98)
at okhttp3.internal.connection.StreamAllocation.findConnection(StreamAllocation.java:196)
at okhttp3.internal.connection.StreamAllocation.findHealthyConnection(StreamAllocation.java:132)
at okhttp3.internal.connection.StreamAllocation.newStream(StreamAllocation.java:101)
at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:42)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:120)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:179)
at okhttp3.RealCall.execute(RealCall.java:63)
at com.jakewharton.picasso.OkHttp3Downloader.load(OkHttp3Downloader.java:136)
at com.squareup.picasso.NetworkRequestHandler.load(NetworkRequestHandler.java:47)
at com.squareup.picasso.BitmapHunter.hunt(BitmapHunter.java:206)
at com.squareup.picasso.BitmapHunter.run(BitmapHunter.java:159)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:423)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
at com.squareup.picasso.Utils$PicassoThread.run(Utils.java:411)

当我在浏览器中打开这张图片时,它加载成功。 URL 看起来像 http://somesite.com/path/to/file/123456.jpg。是毕加索的错误吗?如何解决?

我已经搜索了你的问题。而且我认为您对 okhttp3 有疑问,例如 https://github.com/fabric8io/kubernetes-client/issues/498 中的问题 - 身份验证连接问题。您可以尝试通过自定义 Picasso 下载器来解决问题:

// create Picasso.Builder object
Picasso.Builder picassoBuilder = new Picasso.Builder(context);

// let's change the standard behavior before we create the Picasso instance
// for example, let's switch out the standard downloader for the OkHttpClient
picassoBuilder.downloader(new OkHttpDownloader(new OkHttpClient()));
// or you can try 

(picassoBuilder.downloader(  
    new OkHttpDownloader(
        UnsafeOkHttpClient.getUnsafeOkHttpClient()
    )
);)

// Picasso.Builder creates the Picasso object to do the actual requests
Picasso picasso = picassoBuilder.build();

现在您可以使用 picasso 加载图像了。

picasso  
   .load(linktoimage)
   .into(imageView3);

Is it Picasso bug?

我不这么认为。默认情况下,OkHttp 似乎会阻止非 SSL 通信。我已经很久没有使用 OkHttp 发出纯文本 HTTP 请求了,但这是我在检查与该错误消息相关的代码时看到的结果。

How to fix it?

使用 https URL。

如果一些恶魔般的疯子威胁要炸毁一座小城市,除非你使用普通 http,通过其 Builder 配置 OkHttpClient,包括调用 connectionSpecs() 以表明您愿意支持哪种类型的 HTTP 连接。例如:

.connectionSpecs(Arrays.asList(ConnectionSpec.MODERN_TLS, ConnectionSpec.CLEARTEXT))

将允许 "modern TLS"(不完全确定什么符合条件)和纯 HTTP。

然后,将 OkHttpClient 用于 Picasso,以及您直接使用 OkHttp 所做的任何事情。