Replace some special characters with browser code - android

I am facing a strange situation here.
I am parsing an image link through json and getting the url:
http://developersatwork.com/projects/g4/tools/wp-content/uploads/2015/05/Belbin’s-Team-Roles-100x100.jpg
Whenever I paste this in browser then the image is showing,but whenever I paste it in a notepad then the following url is showing up:
http://developersatwork.com/projects/g4/tools/wp-content/uploads/2015/05/Belbin%E2%80%99s-Team-Roles-100x100.jpg
As you can apostrophe is replaced by %E2%80%99 and Json cannot detect the
apostrophe and as a result it is throwing exception:
06-15 14:01:16.955: W/System.err(10146): java.io.FileNotFoundException: http://developersatwork.com/projects/g4/tools/wp-content/uploads/2015/05/Belbin’s-Team-Roles-100x100.jpg
How can I replace such special characters with their corresponding code so that Json can detect them??
I tried UTF-8 encoding and decoding but it is not working.Please give a generalised solution but not for a specific character.
Another one:
http://www.tools4management.dreamhosters.com/wp-content/uploads/2015/06/The-POSITIVE-Model-of-Coaching-–-An-8-step-Outline-to-Motivate-Goal-Accomplishment-100x100.jpg
But when I paste the link from browser to a notepad it shows:
http://www.tools4management.dreamhosters.com/wp-content/uploads/2015/06/The-POSITIVE-Model-of-Coaching-%E2%80%93-An-8-step-Outline-to-Motivate-Goal-Accomplishment-100x100.jpg
SOLUTION
The only workaround was to use a Webview instead of ImageView.

You need to decode the url.
url = java.net.URLDecoder.decode(url, "UTF-8");

Related

Jsoup connect doesn't work correctly when there is an absolute link?

I'm using Jsoup to get html from web sites. for example I have the links in my HTML page look like this:
String url="http://kitchen.sayidaty.net/node/8544/كوكيزبالشوفان/حلويات";
Document doc=Jsoup.connect(url).get();
this link parse NOTHING and throws IOException, but when I open this link manually in browser and take it back to my code it get changed and works fine ! like this :
String url="http://kitchen.sayidaty.net/node/8544/%D9%83%D98%B2-%D8%A7%D9%86/%D8%AD%D9%84%D9%88%D9%8A%D8%A7%D8%AA#ingredients";
Document doc=Jsoup.connect(url).get();
is there any way to get the absolute link (the second one) , I tried this but same result :'(
link.attr("abs:href")
The problem you're facing has to do with the charset specification http protocol handle when you do transactions like POST and GET. You must use a application/x-www-form-urlencoded MIME format (For more information about HTML form encoding, consult the HTML specification).
In your case, for this to work you have to use Android URLencoder, but only at the end of the address you want to use, to avoid problems. So transform:
String url="http://kitchen.sayidaty.net/node/8544/كوكيزبالشوفان/حلويات";
Into:
String auxUrl= URLEncoder.encode("كوكيزبالشوفان/حلويات", "utf-8");
String url="http://kitchen.sayidaty.net/node/8544/" + auxUrl;
Then proceed with your
Document doc=Jsoup.connect(url).get();

Symbols show up as � after i parse an html webpage using jsoup and display contents in android listview

I would like to understand this problem i have been having.
Im parsing an html source page and displaying the content i want in a list view in android.
I parse the page using this command.
doc = Jsoup.connect(myURL).get();
Symbols such as é or “ ” show up as �.
I understand they are not being recognized by the encoding mechanism but is it because of jsoup or android?
Android default encoding im using is Utf-8 should it not support that?
If it should not how and what should i change it to?
Thank you for you help.
é in ISO-8859-1 (extend ASCII) is the value 233 but in UTF-8 it is the value 195 folowed by 169.
You need to know in what encoding the caracters are saved in because only the values are saved and then interpreted.
Thank you guys for the help.
making the jsoup call like this :
Document document = Jsoup.parse(new URL(url).openStream(), "ISO-8859-1", url);
was the way to go i then had to find out the real encoding of the webpage in chrome you can find it in 'more tools' and in my case it was
windowns-1252. One line of code solved the problem:
doc = Jsoup.parse(new URL(url).openStream(), "windows-1252", url);

How to check if an image/image link is present in text in android

I am creating an android application. I am getting the response from server in json format. I ma parsing the json response. When I get the content it may contain image or video link. How can I check whether image or video link is present in the content and download the corresponding image or video and display it in my application. I am aware f downloading images and displaying them, but I am not aware of how to check for the link.
My response is in the following format:
<p class='para-inside-post'> cool panda <a class='handler_name' href='/#12'>#12</a> </p><img class=\"post-img-tag\" postcreatorid=\"56332edfad441746cbd15000\" src=\"https://image.jpg\" height=\"430px\" width=\"430px\">"
I am parsing the text as shown below:
postContentSplit = Html.fromHtml(content).toString();
Similarly, how can I do the same for images and videos?
All suggestions are welcome. Please help me come out of this issue.
Use Patterns in order to check url validity
Patterns.WEB_URL.matcher(potentialUrl).matches()
It will return True if URL is valid and false if URL is invalid.

Work with the link with special character (e.g. traditional chinese , empty space) in android

I am working with special character in URL for android
However, I encounter two problem
1) empty space
If I have a query inside url
e.g. test.php?test=aaa bbbb cccc
Then the query will not include bbbb and cccc, I learnt that I should replace the " " to %20, however, instead of using replace(" ","%20"), how can I do it in more standard way?
2) traditional chinese in url
I have an image url like this:
http://oshc.zizsoft.com/wp-content/uploads/2014/04/1-職安健資訊產品目錄2013-220x300.png
If I directly pass to android, it fail. but if I copy the link to my desktop browser , it change to like this, then I paste it on android, it works
"http://oshc.zizsoft.com/wp-content/uploads/2014/04/1-%E8%81%B7%E5%AE%89%E5%81%A5%E8%B3%87%E8%A8%8A%E7%94%A2%E5%93%81%E7%9B%AE%E9%8C%842013-220x300.png";
What should I do to encode to this?
Thanks for helping
Update:
it change to
http%3A%2F%2Foshc.zizsoft.com%2Fwp-content%2Fuploads%2F2013%2F12%2FSQ1-351x300.jpg
How can I fix that? Thanks
1) Try trimming the url, it wil remove the spaces from url. I guess its bit more staanddard way to solve this issue.
2) This problem is eactly due to the encoding issues. Our Android default encoding is cp1252...and those strings will not be encoded in the same way so when it used in the code its automatically changes to some other symbols. So try changing the encoding of the project and string to the same like UTF-8 or something. You can change project encoding by
Properties> Resources> Encoding
Hope my suggesions will help you a bit.
Try this links also
1. Trimming
2. Encoding
If you want to programmatically encode and decode URLs then use URLEncoder and URLDecoder class available in Java as well as Android.
// To encode URL
URLEncoder.encode(url, charset);
// to decode url
URLDecoder.decode(url, charset);

Why is WebView unable to open some local URLs (Android)?

I have a WebView that I'm using to open some files stored in the assets/ directory of my project. It works fine for most of the files, but there's one in particular (and I'm sure others I haven't found) that it just will not open.
The file I'm having problems with is named:
"assets/ContentRoot/Photos/XXX Software Logo - jpg - 75%.JPG"
When I pass it to WebView, and it shows the error page, it shows it as:
"file:///android_asset/ContentRoot/Photos/XXX%20Software%20Logo%20-%20jpg%20-%2075%.JPG"
I then tried running URLEncoder.encode() on it and got the error page with the URL presented as:
"file:///android_asset/ContentRoot/Photos/XXX+Software+Logo+-+jpg+-+75%.JPG"
Neither of these URLs were able to open the file (and they both look okay to me). Anyone have any ideas?
UPDATE: If I encode the % by hand (using %25, as commonsware.com suggested) then it loads the image, but it tries to parse it as text, not as an image, so I just get a lot of (basically) garbage.
Also, referring to the image in an HTML document with a relative URL isn't working (probably because it's not being parsed as an image?):
<img src="../Photos/XXX%20Software%20Logo%20-%20jpg%20-%2075%.JPG" />
<img src="../Photos/XXX%20Software%20Logo%20-%20jpg%20-%2075%25.JPG" />
Okay, after spending way too long on this, I've figured out what's going on. Basically, if images stored in the assets/ directory contain a space (e.g., " ") in their file name, they won't render as images.
myWebView.loadUrl("file:///android_asset/testimage.jpg");
works fine. However,
myWebView.loadUrl("file:///android_asset/test+image.jpg");
just throws a not found error and
myWebView.loadUrl("file:///android_asset/test image.jpg");
// and
myWebView.loadUrl("file:///android_asset/test%20image.jpg");
show it improperly displayed (as text... see screenshot in question).
This unexpected behaviour is present on (at least) 1.5, 1.6, and 2.0 and I filed a bug report.
Try getting rid of the % in the filename. Or, escape it as %25.
I would guess that WebView only understands text related content types so it faithfully treating your JPG as base64 encoding, decodes and displays resulted gobble-goop as text. I don't really know if it's possible to set content type for WebView but as workaround you can try to throw img tag inside html tag and load resultet page. Also you probably can only use WebView#loadDataWithBaseUrl

Categories

Resources