0

So I'm running a json request in my android project that contains something like this for url strings to images "http:\/\/s2.dmcdn.net\/qhQ5Y.jpg"

On my AS AVD API 25 it auto deletes the "\" character and loads the image properly (using picasso for network image loading), but in the AVD API 27 it throws an error. I'm assuming because the "\" isn't automatically ignored, as the same url without the "\" loads the image properly in any web browser.

Question: How can I streamline this to work across all the newest (23+) android devices? Is it a problem with picasso or a difference in the way the Android versions load the network calls?

Juan
  • 5,525
  • 2
  • 15
  • 26
Mr.Drew
  • 939
  • 2
  • 9
  • 30
  • 1
    Backslash is an escape character in java. For the back slash to be taken as a backslash you have to double it. \\ instead of \. Anyway how are you decoding the json string, because libraries take care or doing that correctly. – Juan Oct 07 '18 at 15:13
  • I made my own utils that saves the string to an object and Picasso loads that image by getting the string from the saved object. So the url is being saved as "http:\/\/s2.dmcdn.net\/qhQ5Y.jpg" which doesn't load correctly in the browser, but Picasso loads it fine in API 25. – Mr.Drew Oct 07 '18 at 15:20

1 Answers1

0

I fixed the issue by changing the saved string url to one with out backslashes

String thumburl = (videoJsonObj.getString("thumbnail_url"));
                thumburl = thumburl.replace("\'", "");

This now works across all my AVD API levels. I still want to know why Picasso corrected this by default on API 25, but not API 27 or 28. What is the difference in the way those Android versions and Picasso work together to load URL resources?

UPDATE: I spoke too soon... it appears to still not be working on the new Android version (Pie) on the AS emulator. After some digging and lots of logging I realized that a couple of things were happening:

First, the JSONObject.getString() method seems to automatically remove the "\" when it retrieves the url value from the JSON using the key. As logging the value after this extraction shows the url without backticks, even without the above code. (Still nice to have it just in case that doesn't happen automatically by the system.)

Thus the real problem was in Android Pie's new feature blocking all http traffic by default. You can bypass this feature in your app as shown here

OR you can do a bunch of string manipulation to keep your app's network calls more secure by default assuming the site you are accessing has https addresses, but for some reason returns http results by default in their JSON responses, like in my case.

I chose to keep it to https as that is the future by adding the 's' if it wasn't present in my results with the below string manipulation:

String thumburl = (videoJsonObj.getString("thumbnail_url"));
thumburl = thumburl.replace("\'", "");
StringBuilder imageUrlStringBuilder = new StringBuilder(thumburl);
if(imageUrlStringBuilder.charAt(4) != 's') {
   imageUrlStringBuilder.insert(4, 's');
}
thumburl = imageUrlStringBuilder.toString();
video.setVideoThumbnailUrl(thumburl);

I believe this string manipulation is the better way to go as it won't break if the API updates to return https instead of it's current return of http addresses because it checks for the 's' character at the right place; however, it will also force load https version (as this is the future trend of web) and this might not work with outdated sites/data sources. (This worked for me because in my case this site. https://s1.dmcdn.net/p0i-5.jpg and http://s1.dmcdn.net/p0i-5.jpg both return the same result.)

Mr.Drew
  • 939
  • 2
  • 9
  • 30