Picasso not working if url contains space

只谈情不闲聊 提交于 2019-12-05 17:37:27
String temp = "http://www.tonightfootballreport.com/\Filebucket\Picture\image\png\20160807025619_Serie A.png";
temp = temp.replaceAll(" ", "%20");
URL sourceUrl = new URL(temp);

Encode the URL,

String url = "http://www.tonightfootballreport.com/Filebucket/Picture/image/png/20160807025619_Serie A.png";
String encodedUrl = URLEncoder.encode(url, "utf-8");

EDIT #1 :

The problem with above method as @Wai Yan Hein, pointed is that it encode all the characters in the url including the protocol.

The following code solves that issue,

String urlStr = "http://www.tonightfootballreport.com/Filebucket/Picture/image/png/20160807025619_Serie A.png";
URL url = new URL(urlStr);
URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef());
url = uri.toURL();

Edit #2

Alternate solution using Uri.parse,

String urlStr = "http://www.tonightfootballreport.com/Filebucket/Picture/image/png/20160807025619_Serie A.png";
String url = Uri.parse(urlStr)
                .buildUpon()
                .build()
                .toString();

Check if the URL is indeed valid and if not try encoding it,

if(!Patterns.WEB_URL.matcher(url).matches()){
            URLEncoder.encode(url, "utf-8");
            //Now load via Picasso
        }
else{
      //Proceed with loading via picasso
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!