Picasso doesn't work if url contains space

I am developing an Android application in which I receive an image from a server and display it as an image using Picasso. Some image URLs do not work, although I can successfully test them in a browser.

For example, this URL works correctly:

http://www.tonightfootballreport.com/\Filebucket\Picture\image\png\20160730011032_BPL.png

But this fails:

http://www.tonightfootballreport.com/\Filebucket\Picture\image\png\20160807025619_Serie A.png

The difference is that the URL with the error contains a space. What do I need to do to make this work?

+4
source share
3 answers
String temp = "http://www.tonightfootballreport.com/\Filebucket\Picture\image\png\20160807025619_Serie A.png";
temp = temp.replaceAll(" ", "%20");
URL sourceUrl = new URL(temp);
+7
source

Encode URL

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

EDIT. # 1:

A problem with the above method, like @Wai Yan Hein, indicates that it encodes all the characters in the URL, including the protocol.

,

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();

# 2

Uri.parse,

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

Check if the URL is valid and if it is not trying to encode it,

if(!Patterns.WEB_URL.matcher(url).matches()){
            URLEncoder.encode(url, "utf-8");
            //Now load via Picasso
        }
else{
      //Proceed with loading via picasso
}
0
source

Source: https://habr.com/ru/post/1655025/


All Articles