How do you use JSoup to find elements (HTML parsing)? - android

I know that in Jsoup when you want to find a certain Element with a link in it, you can do this:
Document doc = Jsoup.parse(text);
Element links = doc.select("[href]");
This however, takes all links to every website in the page...
But what if I have multiple links, and I only want to retrieve the ones specifically linking to google. For instance:
Google
Bing
Another Google
And I want it to take only those with google in it. I tried doing something like this:
Element links = doc.select("[href=\"http://www.google.com\"]");
But this doesn't work... does anyone have a suggestion?

Have you tried simply this:
Element links = doc.select("[href=http://www.google.com]");
//Or,
Element links = doc.select("a[href=http://www.google.com]");
//Or with the 'attribute contains' form, the most likely to work:
Element links = doc.select("a[href*=google]");

Related

Android: Firebase Dynamic Links open browser first before opening app

I'm having an issue with Firebase Deep Links on Android.
I have 3 Firebase dynamic links, which are for 3 build flavors of one app, they are like this:
https://my_subdomain.page.link?link=https://my_subdomain.page.link/**app1**?param1=aaa&param2=bbb&apn=**app1_package_name**
https://my_subdomain.page.link?link=https://my_subdomain.page.link/**app2**?param1=aaa&param2=bbb&apn=**app2_package_name**
https://my_subdomain.page.link?link=https://my_subdomain.page.link/**app3**?param1=aaa&param2=bbb&apn=**app3_package_name**
Now what I want when I click on those links is: my corresponding apps
will open directly.
But the actual result is: the links redirect me to the browser first,
then after about 1 second, open the app.
Does anybody know what I'm doing wrong? Please help me.
I've found the answer myself.
Create 3 different URL prefixes on Firebase: subdomain1.page.link, subdomain2.page.link, subdomain3.page.link.
Declare a new intent filter in AndroidManifest, which looks like this:
Also remember to declare a variable named "app_subdomain" with respective values (subdomain1, subdomain2, subdomain3) for each app in build.gradle. For example:
manifestPlaceholders["app_subdomain"] = "subdomain1"
Type yourself these 3 links and use the parameters the way you like:
https://subdomain1.page.link?link=https://my_subdomain.page.link?param1=aaa&param2=bbb&apn=app1_package_name
https://subdomain2.page.link?link=https://my_subdomain.page.link?param1=aaa&param2=bbb&apn=app2_package_name
https://subdomain3.page.link?link=https://my_subdomain.page.link/?param1=aaa&param2=bbb&apn=app3_package_name
That should do it!

Best way to display first doc in a view using the UnpBootFormViewer

I am trying to display the first document in a view using the "UnpBootFormViewer".
What I have currently done is include the "UnpBootFlatView" control that references a view which contains only one (1) document (app settings in this instance). I have it in a "" with the style set to "display: none;".
This works great on a computer using a standard browser, but when the same app is opened on a mobile device (Nexus 5) it does nto work.
Is there a better/easier way to do what I'm trying?
I assume you use native document data source. To initialize it with first document from some view you must provide UNID of that document. Example:
var v:View = database.getView("view name");
var vec:ViewEntryCollection = v.getAllEntries();
var ve:ViewEntry = vec.getFirstEntry();
var unid = ve.getUniversalID();
return unid;
I recommend to use your preferred error catching technique (try/catch, OpenLog...)
Why so complicated - ViewEntryCollection and ViewNavigator collections keep documents sorted as in view. Common beginner's mistake is to use view.getFirstDocument() - this document is the first from internal list of documents so it need not to be the first document shown in the view.
I think what you are trying to do is possible and I think it's demostrated in this page from the sample app provided by Teamstudio
My Activities Page
This uses the UnpBootFlatView to look up the activities based you your username. It then uses the UnpBootFormViewer to display the first document in the view.
The XPage to reference is MyActivities
The documentations also points out that from release 1.3 of the XControls 'openfirstdocument' is defaulted to 'yes'. If you have this set to anything else the first document in the view will not load.
Flat View Documentation
If that's all fine on the standard browser as you suggest and it just doesn't work on the Android device, then I'd suggest contacting Teamstudio Support.
Tim C. ;o)
[Disclosure] Tim Clark works for Teamstudio

How to access linkify number

I need to make all numbers in a string become links.
The expected action when any of these links is clicked is to append the clicked number to an existing string.
I managed to linkify the numbers by using the following code:
Pattern myMatcher = Pattern.compile("[0-9]*");
Linkify.addLinks(myString, myMatcher, null);
How can I access and retrieve the clicked number in this case?
I tried looking in other questions related to Linkify but seems all are describing ways to have an action that opens an activity or open the default app for that link type (email address/web URL/etc.)
Thanks in advance for you help :)
You can Customize Linkify to append any predefiened string(scheme) into that.
Take a look at the following post Android Developer Blogspot (Search for "Custom Linkify")
For clarity I am describing a portion of that post here:
Linkify will automatically append whatever is matched to a scheme that
is supplied to it, so for the sake of argument let's assume we have a
ContentProvider that matches the following content URI:
content://com.google.android.wikinotes.db.wikinotes/wikinotes/WikiWord
The WikiWord part will be appended by Linkify when it finds a match,
so we just need the part before that as our scheme.
Now that we have these two things, we use Linkify to connect them up:
Pattern wikiWordMatcher = Pattern.compile("\\b[A-Z]+[a-z0-9]+[A-Z][A-Za-z0-9]+\\b");
String wikiViewURL = "content://com.google.android.wikinotes.db.wikinotes/wikinotes/";
Linkify.addLinks(noteView, wikiWordMatcher, wikiViewURL);
Linkify can be used multiple times on the same view to add more links,
so using this after the Default Linkify call means that the existing
active links will be maintained and the new WikiWords will be added.
You could define more Linkify actions and keep applying them to the
same TextView if you wanted to.
Now, if we have a WikiWord in the TextView, let's say MyToDoList,
Linkify will turn it into an active link with the content URI:
content://com.google.android.wikinotes.db.wikinotes/wikinotes/MyToDoList
and if you click on it, Android will fire the default intent for that
content URI.
For this to all work, you will need a ContentProvider that understands
that Content URI, and you will need a default activity capable of
doing something with the resulting data. I plan to cover these in
future blog entries (and soon). In fact, the whole Wiki Note Pad
application is currently undergoing some clean up and review, and will
then hopefully be released as a sample application.

my jsoup can not selcet elements by some classes

I try to parse the source file of a website called dgtle.com
In order to get the top news, I coded as :
Document doc = Jsoup.connect("http://www.dgtle.com").get();
Elements blocks = doc.getElementsByClass("listcs1");
I got nothing but NullPointerExecption while doing this. But the Div with the class of "listcs1" really exists. This troubles me and I'm wondering whether anybody can help me deal with this.
Use a doc.select instead of getElementsByClass..
ex.
Elements blocks = doc.select("div.listcs1");
this will get all divs with the class of "listcs1"
Jsoup lists all of the selection combinations here: http://jsoup.org/cookbook/extracting-data/selector-syntax

how to get html source code in android?

What would the best approach to :
get html codes by calling url and displaying it in some views
if i would be editing the values against title tag, it must be automatically updated in the url link?
How to do this?
Check out the JSoup cookbook.
http://jsoup.org/cookbook/
This has a lot of good information in it and should be able to answer most of your questions.
You are probably looking for something along the lines of this: http://jsoup.org/cookbook/input/parse-body-fragment

Categories

Resources