i am implementing an android application and i'd like to implement an url link preview in it . As a first try , i have used the jsoup library but i am encountering some issues .Not all the links have metadata or some information are missing like the image , the data or the description .Also , some links are causing some errors like :
java.lang.RuntimeException: javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
Is there another alternative to implement an url preview in android ??
Here is what i have already done :
Utils.getJsoupData(link)
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe({result: Document ->
// Log the entire HTML content
Log.d("TAG", "HTML Content: $result")
val metaTags = result.getElementsByTag("meta")
for(element in metaTags)
{
when{
element.attr("property").equals("og:image")
->{
if(element.attr("content")!=null){
Picasso.get()
.load(element.attr("content"))
.into(binding.image)
}
else{
binding.imageTitleDescriptionLayout.visibility= View.INVISIBLE
}
}
element.attr("property").equals("og:url")
->{
val browserUri = element.attr("content")
val i = Intent(Intent.ACTION_VIEW)
i.data= Uri.parse(browserUri)
context.startActivity(i)
}
element.attr("property").equals("og:title")
-> binding.title.text=element.attr("content")
element.attr("name").equals("description")
->{
if(element.attr("content")!=null){
binding.description.text=element.attr("content")
Log.d("TAG", "bind: The description text is ...${element.attr("content")} ")
}else{
binding.imageTitleDescriptionLayout.visibility= View.INVISIBLE
}
}
element.attr("property").equals("og:url")
-> {
binding.address.text=element.attr("content")
Log.d("TAG", "bind: The url text is ...${element.attr("content")} ")
}
}
}
},
{ error -> Toast.makeText(context,error.message+"there is an error ",Toast.LENGTH_SHORT).show()
Log.e("TAG", "Error fetching data", error)
})
and here is the getJsoupData function :
object Utils {
@JvmStatic
fun getJsoupData(url:String):Observable<Document>{
return Observable.fromCallable {
try {
return@fromCallable Jsoup.connect(url)
.timeout(0)
.get()
}catch(e:IOException)
{
throw RuntimeException(e)
}
}
}
}