Android 9.0 Pie下的Picasso图片加载问题

16

我无法在安卓9.0 Pie中使用Picasso库加载图像。实际上,它对于版本低于此是可以正常工作的。 没有显示任何错误消息。 有人在Github上分享了他的日志。

Picasso.get().setLoggingEnabled(true);

他有消息记录:

2018-10-19 13:13:20.467 24840-24862/com.xyz.test.testpicasso D/ViewContentFactory: initViewContentFetcherClass
2018-10-19 13:13:20.467 24840-24862/com.xyz.test.testpicasso I/ContentCatcher: ViewContentFetcher : ViewContentFetcher
2018-10-19 13:13:20.467 24840-24862/com.xyz.test.testpicasso D/ViewContentFactory: createInterceptor took 0ms
2018-10-19 13:13:20.468 24840-24862/com.xyz.test.testpicasso I/ContentCatcher: Interceptor : Catcher list invalid for com.xyz.test.testpicasso@com.xyz.test.testpicasso.MainActivity@147874166
2018-10-19 13:13:20.468 24840-24862/com.xyz.test.testpicasso I/ContentCatcher: Interceptor : Get featureInfo from config pick_mode
2018-10-19 13:13:20.485 24840-24840/com.xyz.test.testpicasso D/Picasso: Main        created      [R1] Request{https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png}
2018-10-19 13:13:20.492 24840-24864/com.xyz.test.testpicasso D/Picasso: Dispatcher  enqueued     [R1]+6ms 
2018-10-19 13:13:20.492 24840-24866/com.xyz.test.testpicasso D/Picasso: Hunter      executing    [R1]+7ms 
2018-10-19 13:13:20.555 1531-1684/? I/ActivityManager: Displayed com.xyz.test.testpicasso/.MainActivity: +114ms
2018-10-19 13:13:20.555 5475-5603/? D/PowerKeeper.Event: notifyActivityLaunchTime: com.xyz.test.testpicasso/.MainActivity totalTime: 114
2018-10-19 13:13:20.709 735-816/? W/SurfaceFlinger: Attempting to set client state on removed layer: Splash Screen com.xyz.test.testpicasso#0
2018-10-19 13:13:20.710 735-816/? W/SurfaceFlinger: Attempting to destroy on removed layer: Splash Screen com.xyz.test.testpicasso#0
2018-10-19 13:13:20.775 1531-1684/? I/Timeline: Timeline: Activity_windows_visible id: ActivityRecord{821c51 u0 com.xyz.test.testpicasso/.MainActivity t4372} time:9356677
2018-10-19 13:13:21.003 24840-24864/com.xyz.test.testpicasso D/Picasso: Dispatcher  retrying     [R1]+518ms 
2018-10-19 13:13:21.004 24840-24872/com.xyz.test.testpicasso D/Picasso: Hunter      executing    [R1]+519ms 
2018-10-19 13:13:21.513 24840-24864/com.xyz.test.testpicasso D/Picasso: Dispatcher  retrying     [R1]+1027ms 
2018-10-19 13:13:21.514 24840-24877/com.xyz.test.testpicasso D/Picasso: Hunter      executing    [R1]+1028ms 
2018-10-19 13:13:21.516 24840-24864/com.xyz.test.testpicasso D/Picasso: Dispatcher  batched      [R1]+1030ms for error
2018-10-19 13:13:21.717 24840-24864/com.xyz.test.testpicasso D/Picasso: Dispatcher  delivered    [R1]+1232ms

1
尝试在清单文件的Application标签中使用android:usesCleartextTraffic="true"。 - Intsab Haider
1
嘿,你救了我的一天...非常感谢你 ☺️ - tejraj
1
你能否请说明原因? - tejraj
3个回答

48

尝试在Manifest文件的Application标签中使用android:usesCleartextTraffic="true"!我在使用Android Volley时遇到了相同的问题!

根据安卓文档:

指示应用程序是否打算使用明文网络流量,例如明文HTTP。针对 API级别为27或更低级别 的应用的默认值为"true"。针对 API级别为28或更高级别 的应用,则默认为"false"

当该属性设置为"false"时,平台组件(例如HTTP和FTP堆栈,DownloadManager,MediaPlayer)将拒绝应用程序使用明文流量的请求。强烈鼓励第三方库也遵守此设置。避免明文流量的主要原因是缺乏机密性、真实性和防篡改保护:网络攻击者可以窃听传输的数据并修改其内容而不被发现。链接


2
在现代应用程序中允许HTTP流量真的不是一个好主意。这可能会导致许多安全问题(除了调试原因)。至少你需要像@QuentinKlein的回答中那样限制域名。如果需要完整支持,可以在这里进行检查。 - Antoine Draune
1
@Intsab Haider 感谢您的回答。这对我也有效。 - tech.mohit.garg
1
我已经提交了一份编辑,以更新此答案中的Android文档。在API级别28或更高版本上,默认值变为false。 - John Pang

21

我知道使用android:usesCleartextTraffic="true"可以解决问题,但是这将允许所有的连接都是http而不是https,我认为这不符合2018年的要求。

如果您知道您访问的域名是http且您信任它,则最好使用网络安全配置

res/xml/network_security_config.xml中定义一个xml文件。

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">secure.example.com</domain>
    </domain-config>
</network-security-config>

请查看clearTextTrafficPermitted="true",只针对于secure.example.com和它的子域名。

然后在你的AndroidManifest.xml中,添加android:networkSecurityConfig="@xml/network_security_config"

你可以添加多个域名及其配置,确保其中一些是https或相反的协议。 我认为这样更加安全。


2

在我的情况下,我只是将图片的URL从http改为https,在API 28上工作,并没有向我的清单文件添加任何内容。


针对API级别28或更高版本的应用程序,默认情况下android:usesCleartextTraffic为“false”。 - John Pang

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接