How to get url from JSON text - java

Wondering if it is possible to get url's from the text, wich I parse from JSON. For example I have some JSON object which is in the JSON array and called "text". This "text" contains strings and images (url's) like a img src=\u0022http:\/\/www.dostup1.ru\/netcat_files\/Image\/2014\/08\/04\/dzd-2.jpg
but "img" or "src" are not in quotation marks so how can I get them and then parse this images to show?
item.setContent(post.getString("text"));
This is how I parse text and when it loads, it show me text with strange green rectangles instead of images (because of method doesn't know about how to parse image from url which just is in the text)

Lets assume that you have this piece of String from the result of your JSON.
<img src="http:\/\/www.dostup1.ru\/netcat_files\/Image\/2014\/08\/04\/dzd-2.jpg">
Then you just need to use the split method of the String class to extract the url between qoutes.
sample:
This is just a sample java program that split the URL from the JSON response you got
String s = "<img src=\"http://www.dostup1.ru/netcat_files/Image/2014/08/04/dzd-2.jpg\">";
String [] result = s.split("\"");
System.out.println(result[1]);
result:
http://www.dostup1.ru/netcat_files/Image/2014/08/04/dzd-2.jpg

Related

Split text within span if there is an img tag between them - selenium - java

I'm having a scenario where within a span tag I have two strings, separated by an img tag.
<span>
text
<img/>
text
</span>
When I'm trying to find this span using selenium and Xpath, I found it - but getText() method of the span element returning "texttext". My intention is to get "text text".
driver.findElement(By.xpath("MY_XPATH_TO_FIND_THAT_SPAN").getText();
My Xpath is fine (because I'm getting the right web element, but how can I get the string as I note here? I want to append a space whenever there is an img tag.
Will be glad for your help,
Thanks!
There is no direct way to do it using .getText(). You can use .getAttribute("innerHTML") and then you will need to replace whatever is between the two "text" strings (IMG, etc.) with a space.
Here's a simple example based on your HTML that will probably work.
String s = driver.findElement(By.xpath("MY_XPATH_TO_FIND_THAT_SPAN").getAttribute("innerHTML"); // <span>text<img/>text</span>
s = s.replaceAll("<img.*?/>", " ");
System.out.println(s);
This prints
<span>text text</span>
To retrieve the text text from the first child node and the text text from third child node you can use the getAttribute("innerHTML") method and then use split() method and finally print text text inserting a space between them accordingly as follows :
String my_string = driver.findElement(By.xpath("MY_XPATH_TO_FIND_THAT_SPAN")).getAttribute("innerHTML");
String[] stringParts = my_string.split("\n");
String partA = stringParts[0];
String partB = stringParts[2];
System.out.println(partA + " " + partB);

How to get image path from HTML by tag (using Jsoup)

I'm tring to get image url src from tag <img>
E.g I have this html data from facebook:
<img class="profilePic img" alt="Facebook Developers" src="https://fbcdn-profile-a.akamaihd.net/hprofile-ak-xpf1/v/t1.0-1/p320x320/9988_10151403325753553_1486509350_n.png?oh=ecdfcf4b449779941db77b52950843b3&oe=568F1F42&__gda__=1453778308_a1ffaea01e68e9dade86f1b11989a50d">
How can I get only image src with the class="profilePic img" attribute or class name? Any idea how do I get it? I'm using Jsoup library.
You can get all the images by calling getElementsByTag('img') and then call select(".your_class_name") to get only the images with the specified class (or any other query)
e.g:
Jsoup.connect("http://stackexchange.com").get().getElementsByTag("img").select(".favicon")
Try it
Document document = Jsoup.connect("yourLink").get();
String img_url = document.select("img[class=profilePic img]").first().attr("src");
Log.d('Src image: ', img_url);
Remember: solve it in other thread, not main thread :)
JSoup CSS offers multiple class selection through concatenation. The CSS selector for the classes are .profilePic and .img. Selecting both classes means concatenating: .profilePic.img. So this should be your code:
document.select("img.profilePic.img")
This is better than img[class=profilePic img], because the latter will look for exactly the string "profilePic img". Classes however may appear in different order or with more spaces in the document you parse.
To get to the src attributes of all img elements you need to loop over the results:
Elements imgs = document.select("img.profilePic.img");
for (Element img : imgs){
String srcStr = img.attr("src");
//do what ever you need to do with srcStr
}

converting HTML to String without TextView

I am having Problems filling my TextView.
I have an HTML String that needs to be converted from HTML to String and the replace some characters.
Problem is: I can convert it directly with:
TextView.setText(Html.fromHtml(sampleText);
But I need to alter the converted sampleText before giving it to the TextView.
E.g.:
String sampleText = "<b>Some Text</b>"
newSampleText = Html.fromHtml(sampleText);
newSampleText.replace(char1, char2);
TextView.setText(newSampletext);
Does anyone know how to convert the HTML saved inside the String?
if you don't need formatting, use Html.fromHtml(sampleText).toString()
otherwise, you need to extract text from html with jsoup to find and change text like here
please try this one:
You need to use Html.fromHtml() to use HTML in your XML Strings. Simply referencing a String with HTML in your layout XML will not work.
DEMO
Try use This version of setText and use SPANNABLE buffer type
DEMO1

Get content from javascript onClick hyperlink

I'm trying to get the content from a website, that uses "onClick" instead of "href" in hyperlinks, so the url is always the same, despite of the page you are seeing.
http://www.sas.ul.pt/index.php
This is the website, and the content i'm trying to get is inside "Alimentação" > "Estudantes".
Estudantes
Is this possible with Jsoup?
Jsoup.connect(url).data("nav", "index#4;02", "opt", "4;02", "chvP", "127").post();
You can get the value of onclick with jsoup
http://jsoup.org/cookbook/extracting-data/attributes-text-html
Just replace the line
String linkHref = link.attr("href");
with this
String handler = link.attr("onclick");
However after that there is no way to construct the URL unless you can somehow map the magic number to 4,02

Box characters within JSON response

This is my JSON String which is generated using Java:
[{\"userFirstNm\":\"Tamás\",\"userAsscId\":\"37732\",\"userLastNm\":\"Török\",\"userLanId\":\"a37732\"}]
Using an alert in JavaScript displays boxes inside string and breaks my eval function.
Please find details in attached screen shot.
alt text http://www.freeimagehosting.net/uploads/336da972f3.png
its invalid json
try this.
[{"userFirstNm":"Tamás","userAsscId":"37732","userLastNm":"Török","userLanId":"a37732"}]

Categories

Resources