Getting audio URL from jsoup android - android

Below is the HTML tag for the audio link url,
And below is the logic I used to get the URL,
String url = "https://www.dictionary.com/browse/happy?s=t";
Document doc = Jsoup.connect(url).get();
Elements wordBlock = doc.getElementsByClass("e16867sm0");
Element e = wordBlock.get(0);
Elements audioSection = e.getElementsByClass("e1rg2mtf7");
String audioUrl = audioSection.get(0).attr("audio");
But still I am unable to get the URL,
How can we get the URL of audio by using class id.

You can use either doc.select("source[type=audio/ogg]" for the first file or doc.select("source[type=audio/mpeg]" for the second one.
You can also use source[type^=audio] - it will give you both.

Related

Get video information from a web link

I have been trying to implement a video download feature into an application but I cannot get it right. Given a video link, how do I get the video info like video size, video title e.t.c. My code for web scraping this information hangs at parsing the url till application runs out of memory.
String url; //e.g https://www.example.com/video-480p.mp4
Map<String, dynamic>? videoInfo = {};
String? videoTitle;
String? videoImageLink;
var linkInfo = await http.get(Uri.parse((url)));//hangs here
if(linkInfo.statusCode!=200){
customToastWidget('Cannot Parse this link');//Toast for showing messages
return null;
}
final document = parse(linkInfo.body);//parse is a method from html package to get the html body of web page
videoInfo["videoSize"] = linkInfo.contentLength;
videoInfo["videoTitle"]=document.getElementsByTagName("title")[0].text;

How to save all web page including .css .js?

I wanna save all web page including .css .js on android by programmatically.
So far I tried html get method and jsoup , webview content but all of them I could not save all page with css and js. These methods just save html parts of WEB Page. When I save the all page ,I want to open it offline.
Thanks in advance
You have to take the html, parse it and get the urls of the resources and then make requests for those urls too.
public class Stack {
private static final String USER_AGENT = "";
private static final String INITIAL_URL = "";
public static void main(String args[]) throws Exception {
Document doc = Jsoup
.connect(INITIAL_URL)
.userAgent(USER_AGENT)
.get();
Elements scripts = doc.getElementsByTag("script");
Elements css = doc.getElementsByTag("link");
for(Element s : scripts) {
String url = s.absUrl("src");
if(!url.isEmpty()) {
System.out.println(url);
Document docScript = Jsoup
.connect(url)
.userAgent(USER_AGENT)
.ignoreContentType(true)
.get();
System.out.println(docScript);
System.out.println("--------------------------------------------");
}
}
for(Element c : css) {
String url = c.absUrl("href");
String rel = c.attr("rel") == null ? "" : c.attr("rel");
if(!url.isEmpty() && rel.equals("stylesheet")) {
System.out.println(url);
Document docScript = Jsoup
.connect(url)
.userAgent(USER_AGENT)
.ignoreContentType(true)
.get();
System.out.println(docScript);
System.out.println("--------------------------------------------");
}
}
}
}
I have similar problem...
Using this code we can get images,.css,.js. However some html contents are still missing.
For instance when we save a web page via chrome,there are 2 options.
Complete html
html only
Out of .css,.js,.php..."Complete html" consists of more elements than "only html". The requirement is to download the html as complete like chrome does in the first option.

Unable to display captcha while using jsoup to extract a division in android webview

I am using the following code to fetch a division of a webpage using htmlunit.
String url="url";
String divisonname="dv";
WebView w=(WebView)v.findViewById(R.id.webView1);
WebClient webClient = new WebClient();
HtmlPage currentPage = webClient.getPage(url);
HtmlElement imgElement = currentPage.getHtmlElementById(divisionname);
Now, i don't know how to load this fetched data into webview.I just know this command w.loadurl(url);

url JSON in android

why if i run this source , error FAILED EXCEPTION Asynctask #2 Illegal character in url
this is source
Intent in = getIntent();
get_nama = in.getStringExtra(tag_nama);
get_subkategoris = in.getStringExtra(tag_subkategoris);
"http://tribinacita.esy.es/tribinacita/index.php/sikomando/tampil_lokasi/?";
url += "nama_lok="+get_nama+"&subkategoris="+get_subkategoris+"&is_android=1";
"http://tribinacita.esy.es/tribinacita/index.php/sikomando/tampil_lokasi/?";
This line of code is just a string literal that isn't assigned to a variable. If this is identical to the code you are actually running, then you need to add an assignment:
url = "http://tribinacita.esy.es/tribinacita/index.php/sikomando/tampil_lokasi/?";

How can I load an image from a url into an android remote view for a widget

I have an image url I parse form json that I want to load into an android widget onto the homescreen. Right now I am trying to do it this way but its wrong:
ImageDownloadTask imageD = new ImageDownloadTask(image);
views.setImageViewBitmap(R.id.image, imageD.execute(image));
image is a string holding a url to an image that needs to be downloaded and I am trying to set it to R.id.image
I found another stack question and tried this as a result:
views.setBitmap(R.id.image, "setImageBitmap",BitmapFactory.decodeStream(new URL(image).openStream()));
And when I use that nothing in the app loads at all, none of the text views get set.
My third try was this:
//get beer data
JSONObject o = new JSONObject(result);
String name = getName(o);
String image = getImage(o);
String abv = getABV(o);
String ibu = getIBU(o);
String glass = getGlass(o);
String beerBreweryName = getBreweryName(o);
String beerBreweryStyle = getBreweryStyle(o);
String beerDescription = getDescription(o);
InputStream in = new java.net.URL(image).openStream();
Bitmap bitmap = BitmapFactory.decodeStream(in);
views.setTextViewText(R.id.beerTitle, name);
views.setTextViewText(R.id.beerBreweryName, beerBreweryName);
views.setTextViewText(R.id.beerStyleName, beerBreweryStyle);
views.setImageViewBitmap(R.id.image, bitmap);
This gave the same result as the last attempt, it would not even set any text views....
Just tried another attempt after one of the answers posted below:
RemoteViews views = new RemoteViews(c.getPackageName(), R.layout.widget_test);
//get beer data
JSONObject o = new JSONObject(result);
String name = getName(o);
String imageURL = getImage(o);
String abv = getABV(o);
String ibu = getIBU(o);
String glass = getGlass(o);
String beerBreweryName = getBreweryName(o);
String beerBreweryStyle = getBreweryStyle(o);
String beerDescription = getDescription(o);
Log.d("widgetImage" , imageURL);
views.setImageViewUri(R.id.image, Uri.parse(imageURL));
views.setTextViewText(R.id.beerTitle, name);
views.setTextViewText(R.id.beerBreweryName, beerBreweryName);
views.setTextViewText(R.id.beerStyleName, beerBreweryStyle);
mgr.updateAppWidget(appWidgetIds, views);
This attempt lets all the text views load, but no image ever shows up.
The way to do this reliably is to use setImageViewURI on the remote ImageView. The trick is that the URI you give it is a content:// URI which then points back to a content provider that you export from your application. In your content provider you can do anything you need to do to supply the image bytes.
For example, in your manifest:
<provider android:name=".ImageContentProvider" android:authorities="com.example.test" android:exported="true" />
And your provider:
public class ImageContentProvider extends ContentProvider {
// (Removed overrides that do nothing)
#Override
public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
List<String> segs = uri.getPathSegments();
// Download the image content here, get the info you need from segs
return ParcelFileDescriptor.open(new File(path), ParcelFileDescriptor.MODE_READ_ONLY);
}
}
And then your URL is something like:
content://com.example.test/something-you-can-define/here
This is necessary because your remote image view is not running in your process. You are much more limited in what you can do because everything must be serialized across the process boundary. The URI can serialize just fine but if you try to send a megabyte of image data with setImageViewBitmap, it's probably going to fail (depending on available device memory).
Got a lot of help from multiple sources for this question. The big problem for me why a bunch of the attempts I tried listed above seemed to lock the widget app and not load anything is because I can not download the image and set it in a UI thread.
To accomplish this I had to move everything to the do in background of my async task and not in the onPostExecute.

Categories

Resources