Parse all HTML code - android

I need to copy all HTML code on the page.
I do so:
URL url = new URL(testurl);
URLConnection connection = url.openConnection();
connection.connect();
Scanner in = new Scanner(connection.getInputStream());
while(in.hasNextLine())
{
htmlText=htmlText+in.nextLine();
}
in.close();
But if the page is large, it takes a lot of time.
Is there a faster method?

Have you tried a different method of reading the page? Like a buffered reader? Reading the content of web page or
Reading entire html file to String?
I'm just thinking Scanner may be a little slow.
Tim

Try to use use (http://jsoup.org "JSoup") to download and parse the HTML from URL
You can get the HTML as document and read the text on each elements
new AsyncTask<Void, Integer, String>(){
#Override
protected String doInBackground(Void... params) {
try {
final Document doc = Jsoup.connect("http://youturl.com").get();
final String content;
runOnUiThread(new Runnable() {
#Override
public void run() {
// get the required text
content = doc.body().getElementsByTag("bodyTag").text();
}
});
} catch (IOException e) {
e.printStackTrace();
}
return content;
}
}.execute();

Related

Why does using the 'search' function in the YouTube Data API use up so much of my quota?

I've been trying to develop an application that returns the first video result for a keyword (as well as getting its content details). This is done multiple times in a session (let's say, like 30 times). If all I'm doing is reading data from 1 result each time (from the "Calculating quota usage" section of their docs), then why is it that I use up all 10,000 of my daily units with a few test runs of my application?
Here's my specific code:
private class GetVideoDuration extends AsyncTask<String, Void, String> {
#Override
protected String doInBackground(String... params) {
String sURL = "https://www.googleapis.com/youtube/v3/videos?id=" + params[0] + "&part=contentDetails&key=" + API_KEY;
String var = null;
try {
// Connect to the URL using java's native library
URL url = new URL(sURL);
URLConnection request = url.openConnection();
request.connect();
// Convert to a JSON object to print data
JsonParser jp = new JsonParser(); //from gson
JsonElement root = jp.parse(new InputStreamReader((InputStream) request.getContent())); //Convert the input stream to a json element
Log.d("rootJSON", root.toString());
Log.d("urlJSON", url.toString());
JsonObject video = null;
try {
video = root.getAsJsonObject().get("items").getAsJsonArray().get(0).getAsJsonObject().get("contentDetails").getAsJsonObject();
} catch (IndexOutOfBoundsException e) {
e.printStackTrace();
Log.e("Chief", "It looks like we've exceeded our quota for the day :(");
}
var = video.get("duration").getAsString();
} catch (IOException e) {
e.printStackTrace();
// contDets = contentDetails
Log.d("Error parsing contDets", e.getMessage());
ERROR_STATE = true;
}
return var;
}
}
I'm aware of the option to apply for a higher quota, but it just seems like something's wrong. Any help would be greatly appreciated.

Send data from Android Studio to server using url

I have an application created in Android Studio with API 28. I also have a php file (http: //mydomain/receptor.php) that collects data from a url of the type (http: //mydomain/receptor.php? Userid = 23 & points = 123) and saves them in the database. What I want is to know how I can send those urls from my application. I have tried different things but I can not get the application to activate the url. I do not need a response from the server in the application, I just need to activate the url. What is the easiest way to do it? Thank you!!!
You should run the code in a separate thread, paste this in your Activity Class
public class HTTPget extends AsyncTask<String , Void ,String> {
#Override
protected String doInBackground(String... strings) {
URL url;
HttpURLConnection urlConnection = null;
try {
url = new URL(strings[0]);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setReadTimeout(150000); //milliseconds
urlConnection.setConnectTimeout(15000); // milliseconds
urlConnection.setRequestMethod("GET");
urlConnection.connect();
}catch (IOException e) {
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
}
}
And you execute the code by calling .execute() whenever you want to execute it
new HTTPget().execute("http://mydomain/receptor.php?Userid=23&points=123");

Get file name from headers with DownloadManager in Android

I'm using DownloadManager to download video files from a url.
The problem is if I use the default folder to download the file I can not see the video in the galery.
Also, If I try to use this method:
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, 'filename');
I need to know the file name before download which in this case, I don't.
And also, I don't have the name of the file in the url.
How can I do to get the file name from the headers and pass the name to the method setDestinationInExternalPublicDir ?
Other alternatives?
In case anyone want an implementation of doing a HEAD request to get the filename:
class GetFileName extends AsyncTask<String, Integer, String>
{
protected String doInBackground(String... urls)
{
URL url;
String filename = null;
try {
url = new URL(urls[0]);
String cookie = CookieManager.getInstance().getCookie(urls[0]);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestProperty("Cookie", cookie);
con.setRequestMethod("HEAD");
con.setInstanceFollowRedirects(false);
con.connect();
String content = con.getHeaderField("Content-Disposition");
String contentSplit[] = content.split("filename=");
filename = contentSplit[1].replace("filename=", "").replace("\"", "").trim();
} catch (MalformedURLException e1) {
e1.printStackTrace();
} catch (IOException e) {
}
return filename;
}
#Override
protected void onPreExecute() {
super.onPreExecute();
}
#Override
protected void onPostExecute(String result) {
}
}
Small tip, there is a nice helper method in Android
URLUtil.guessFileName(url, contentDisposition, contentType);
So after completing the call to the server, getting the contenttype and contentDisposition from the Headers this will try and find the filename from the information.
I got into the same problem.I used #rodeleon answer but there was no Content-Disposition in response header. Then I analyzed url header from Chrome dev tools and got 'Location' in response header which contained filename at the end, it was like "b/Ol/fire_mp3_24825.mp3". so instead of using
String content = con.getHeaderField("Content-Disposition")
I used
String content = con.getHeaderField("Location")
and at the end in onPostExecute
protected void onPostExecute(String result) {
super.onPostExecute(result);
String fileName = result.substring(result.lastIndexOf("/") + 1);
// use result as file name
Log.d("MainActivity", "onPostExecute: " + fileName);
}
Method URLUtil.guessFileName() (while not yet knowing about the Content-Disposition) and also the meanwhile deprecated class AsyncTask are both kinda problematic approaches these days. To properly enqueue the download with the DownloadManager:
One first has to disable "following redirects" in whatever HTTP client (that's the clue).
Then the first response will be HTTP 302 (with a Location header), instead of HTTP 200.
When fetching that, one will get HTTP 200 with a Content-Disposition header (filename).
Only then one can enqueue with DownloadManager (whilst knowing the filename already).
Here's an example of mine: RepositoryFragment (a GitHub Client).
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_MOVIES, uri.getLastPathSegment());

how do i display an image from a certain URL in my android app? [duplicate]

This question already has answers here:
How to load an ImageView by URL in Android?
(23 answers)
Closed 8 years ago.
I am making an app and I would like to keep the images updated when I change them. I am not sure how to display an image from a specific URL.
any help and suggestions is appreciated
thank you
Possibly a duplicate, as mentioned before, but still, I'd use an AsyncTask to load an image from an URL (which doesn't seems to be the case in the provided links).
Something along these lines:
new AsyncTask<String, Void, Drawable>() {
#Override
protected Drawable doInBackground(String... params) {
Drawable result = null;
try {
URL url = new URL(params[0]);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.connect();
InputStream input = connection.getInputStream();
result = Drawable.createFromStream(input, "src");
} catch (MalformedURLException muExp) {
Log.e(TAG, "Bad URL provided!", muExp);
} catch (IOException ioExp) {
Log.e(TAG, "Error loading content from URL!", ioExp);
}
return result;
}
#Override
protected void onPostExecute(Drawable result) {
// Do something with your Drawable, in UI, here...
}
}.execute("http://myimageurl.com/image.jpg");

android: parse html from page

i would like to parse out some text from a page.
Is there an easy way to save the product info in to a string for example? Example url: http://upcdata.info/upc/7310870008741
Thanks
Jsoup is excellent at parsing simple HTML from Android applications:
http://jsoup.org/
To get the page, just do this:
URL url = new URL("http://upcdata.info/upc/7310870008741");
Document document = Jsoup.parse(url, 5000);
Then you can parse out whatever you need from the Document. Check out this link for a brief description of how to extract parts of the page:
http://jsoup.org/cookbook/extracting-data/dom-navigation
If you want to read from a URL into a String:
StringBuffer myString = new StringBuffer();
try {
String thisLine;
URL u = new URL("http://www.google.com");
DataInputStream theHTML = new DataInputStream(u.openStream());
while ((thisLine = theHTML.readLine()) != null) {
myString.append(thisLine);
}
} catch (MalformedURLException e) {
} catch (IOException e) {
}
// call toString() on myString to get the contents of the file your URL is
// pointing to.
This will give you a plain old string, HTML markup and all.
String tmpHtml = "<html>a whole bunch of html stuff</html>";
String htmlTextStr = Html.fromHtml(tmpHtml).toString();

Categories

Resources