I'm not exactly sure how to state this but here's the basic idea of what I'm trying to do:
I'm making a radio player application in Android Java. The function I'm looking at including is a dynamic TextView that get's the title of the song that's currently playing from either the website or ShoutCast.
My thoughts as they stand now is that my XML can stay as it is (a "content_wrap"-ed #string value). I just have no idea if there's a way to change that XML from Java and how to get the HTML (I'm not even sure if I need to use HTML) from the website.
Thank you in advance. You are all great people for even reading this :)
To answer my own question (regarding what I was looking for). I believe what I was starting with was javascript (something like a webplayer that constantly updated, so the title wasn't actually in the code nor the redirection).
What I did was from a permanent list (a played list in my case) and connect to that URL with a URLconnection (java.net.URL & java.net.URLConnection) then use a BufferedReader (java.io.BufferedReader). This part can probably be optomized more using the BufferedReader's mark and restart methods, but for this one I just kept circling though until I found a permanent mark then trimmed it till I found what I needed.
e.g.
while (!br.contains("PERMANANT MARK BEFORE MY DESIRED INFO"))
br.readLine(); //Which advances the line
String s = br.readLine(); //Repeat this till you get your info into s
//Trim functions (I used the </td> to my advantage)
s = s.split("THING NEXT TO INFO")[0[;
display.setText(s);
Thank you to everyone who read this. Good luck on your endeavors!
Related
I am an android developer and I knew about GeckoView recently.
I can get source code of a web page by android WebView (java code).
However with my new website, android Webview can't load the webpage but GeckoView can.
Now I want to get source code of a web page by GeckoView.
Is there any body know the solution to resolve this problem ?
Thank in advance
As far as I know, you cannot use GeckoView to get the source code, but the geckoview library does have GeckoWebExecutor. Take a look at the fetch method and the WebResponse it returns.
By converting the WebResponse.body InputStream to a String you can get the source code.
One option for doing this could follow these steps:
detect which page my geckoview loaded;
save the loaded url into a sharedpref;
if user wants to see the source of the page, load the url saved within that sharedpref.
So...
(1-2) as you already know, geckoview doesn't have that handy shouldOverrideUrlLoading() method, so you will have to deal with the NavigationDelegate class, which has the onLocationChange(), where i put this line, which does nothing more than save the current url as a string into a sharedpref string named "geckoViewUrl":
sharedprefs.edit().putString("geckoViewUrl", url).apply();
having all setup before (sharedprefs etc). Detected the loaded page and saved the url into a sharedpref, let's go to the final step 3.
(3) for my use, wishing only to allow the user to see (and copy) the source, for the law of the minimum effort i used another activity with a plain and old webview to display it. This is very easy to implement and rises no confusion to the user. He/She wants to see the code, i show it in another activity. When it's done, he/she closes the new activity and life continues.
So, user wants source? Load another activity with a webview and make it load that saved sharedpref string:
addr2open = pref_out.getString("geckoViewUrl", "");
Doing this (for example), you get the url user wants to see source assigned to a string var. To finish, all you have to do is to make the webview load this string preceded by the precious word view-source:, this way:
webView.loadUrl("view-source:" + addr2open);
That's it. Of course you could implement a solution relying on the same activity, or using geckoView, showing multiple options, menus etc. I only wanted to show you a way to solve your problem in a nice 'n' easy way. You asked for "How to get a web page source code with GeckoView". Here is the answer. Works perfectly. If this is good for you, please, accept this as the correct answer. Thank you. Happy coding.
One example on the use of "view-source", from the creators of GeckoView: https://firefox-source-docs.mozilla.org/devtools-user/view_source/index.html (see item "Link to a line number").
Click here to see a screenshot of the assignment
Here is how the Navigation.csv looks like, where I take data from
https://pastebin.com/JXnaRTzi <-- Click on the link for code - this is my code so far, I am reading the file and making objects from each entry
Guys I need help with this assignment. I chose to do it in Java, but really it doesn't matter. I need advise and help for making the right approach on this kind of problem so it can work on a larger file. What kind of data structures should I use and maybe if it's no bother, give me a solution. I am a new developer and I'm trying to get into backend and frankly I'm a bit lost.
Click on links above to see the task details.
I'm working with HTML tags, and I need to interpret HTML documents. Here's what I need to achieve:
I have to recognize and remove HTML tags without removing the
original content.
I have to store the index of the previously existing markups.
So here's a example. Imagine that I have the following markup:
This <strong>is a</strong> message.
In this example, we have a String sequence with 35 characters, and markedup with strong tag. As we know, an HTML markup has a start and an end, and if we interpret the start and end markup as a sequence of characters, each also has a start and an end (a character index).
Again, in the previous example, the beggining index of the open/start tag is 5 (starts at index 0), and the end index is 13. The same logic goes to the close tag.
Now, once we remove the markup, we end up with the following:
This is a message.
The question:
How can I remember with this sequence the places where I could enter the markup again?
For example, once the markup has been removed, how do I know that I have to insert the opening tag in the X position/index, and the closing tag in the Y position/index... Like so:
This is a message.
5 9
index 5 = <strong>
index 9 = </strong>
I must remember that it is possible to find the following situation:
<a>T<b attribute="value">h<c>i<d>s</a> <g>i<h>s</h></g> </b>a</c> <e>t</e>e<f>s</d>t</f>.
I need to implement this in Java. I've figured out how to get the start and end index of each tag in a document. For this, I'm using regular expressions (Pattern and Matcher), but I still do not know how to insert the tags again properly (as described). I would like a working example (if possible). It does not have to be the best example (the best solution) in the world, but only that it works the right way for any kind of situation.
If anyone has not understood my question, please comment that I will do it better.
Thanks in advance.
EDIT
People in the comments are saying that I should not use regular expressions to work with HTML. I do not care to use or not regular expressions to solve this problem, I just want to solve it, no matter how (But of course, in the most appropriate way).
I mentioned that I'm using regular expressions, but I do not mind using another approach that presents the same solution. I read that a XML parser could be the solution. Is that correct? Is there an XML parser capable of doing all this what I need?
Again, Thanks in advance.
EDIT 2
I'm doing this edition now to explain the applicability of my problem (as asked). Well, before I start, I want to say that what I'm trying to do is something I've never done before, it's not something on my area, so it may not be the most appropriate way to do it. Anyway...
I'm developing a site where users are allowed to read content but can not edit it (edit or remove text). However, users can still mark/highlight excerpts (ranges) of the content present (with some stylization). This is the big summary.
Now the problem is how to do this (in Java). On the client side, for now, I was thinking of using TinyMCE to enable styling of content without text editing. I could save stylized text to a database, but this would take up a lot of space, since every client is allowed to do this, given that they are many clients. So if a client marks snippets of a paragraph, saving the paragraph back in the database for each client in the system is somewhat costly in terms of memory.
So I thought of just saving the range (indexes) of the markups made by users in a database. It is much easier to save just a few numbers than all the text with the styling required. In the case, for example, I could save a line / record in a table that says:
In X paragraph, from Y to Z index, the user P defined a ABC
stylization.
This would require a translation / conversion, from database to HTML, and HTML to database. Setting a converter can be easy (I guess), but I do not know how to get the indexes (following this logic). And then we stop again at the beginning of my question.
Just to make it clear:
If someone offers a solution that will cost money, such as a paid API, tool, or something similar, unfortunately this option is not feasible for me. I'm sorry :/
In a similar way, I know it would be ideal to do this processing with JavaScript (client-side). It turns out that I do not have a specialized JavaScript team, so this needs to be done on the server side (unfortunately), which is written in Java. I can only use a JavaScript solution if it is already ready, easy and quick to use. Would you know of any ready-made, easy-to-use library that can do it in a simple way? Does it exist?
You can't use a regular expression to parse HTML. See this question (which includes this rather epic answer as well as several other interesting answers) for more information, but HTML isn't a regular language because it has a recursive structure.
Any language that allows recursion isn't regular by definition, so you can't parse it with a regex.
Keep in mind that HTML is a context-free languages (or, at least, pretty close to context-free). See also the Chomsky hierarchy.
ok so heres what i want to achieve:
i want to submit to pastebin or pastee a string(big string) with a custom title and recieve back the url to it as a string (this needs to be done as a guest)
before i continue, i have searched and there is lots of different api's some java i have tried them all and none have worked for me, theres a fair few posts but none with a definative answer.
ok so lets go with what i know (or think i know)
i need atleast : (according to http://pastebin.com/api)
api_dev_key (i have my api key)
api_option=paste
api_paste_code (the code we want to paste)
the title is optional but is :
api_paste_name=
the url for submission is :
*pastebin web page /api/api_post.php
so in theory
*pastebin web page /api/api_post.php/api_dev_key=myprivateapikey&api_option=paste&api_paste_name=testpaste&api_paste_code=hello%20world
should create a pastebin titled testpaste with the content hello world
instead it creates "This paste has been removed!"
so thats the first hurdle (yes i have double checked my api key)
then comes im not really sure how to get the address back after the key is submitted.
all in all im totaly confused (it doesnt help that i have been reading about a thousand and one api java's and guides and none seem to work.
the code i had cobbled together at one point is :
http://pastebin.com/4PVFH8tR
the alternative is pastee but its api is very very undocumented
*it counted them as links so had to make them non url so i could ask the question
Im new to java and working on a simple application that monitor an url and notify me when a table is updated whit new items. Looking at the entire page will not work as there are commercials that change all the time and they would give false positives.
My thought was to fetch the url line by line looking for the elements. For each element I will check to see if the element is already in an arraylist. If not the element is added to the arraylist and a notification is send.
What I need support with is not the exact code but advice if this would be a good approach and if I should store the elements in an array list or if I should use a file instead as there are 2 lines of text in each element.
Also It would be good to get recomandation on what methods and libs there would be good to look at.
Thanks in advance
Sebastian
To check the site it'd probably be more stable to parse the HTML and work with an object representation of the DOM. I've never had to do this but in a question regarding how to do this another user suggested using JTidy, maybe you could have a look at that.
As for storing the information (what you currently do in your ArrayList): this really depends on what you use your application for. If you only want to be notified of changes that occur during the runtime of your program this is perfectly fine. If you want to have the information persist you should find a way to store the information in the file system or database.