Java 在try-catch块中使用if语句不适用于JSoup
我正在尝试让我的Android应用程序连接到互联网,然后使用JSoup从Amazon上删除一些HTML。唯一的问题是,并非所有的亚马逊页面都是相同的,因此我需要创建几个if-else语句来检查我要查找的数据(如标题、图像、价格等)可能位于的不同位置。 唯一的问题是,由于Android要求所有连接到互联网的代码都在try-catch语句中,一个不幸的副作用是一些if语句不能正常工作。如果我尝试将字符串设置为某个值,但它没有找到任何内容,并且将自身设置为null,那么它会立即从try-catch语句中退出,这意味着其余代码不会运行 有没有办法在不让catch语句停止程序的情况下保持if语句的运行? 我知道这是非常令人困惑的,所以代码如下,如果您有任何问题,我将尝试以我能最好的方式回答他们。 谢谢你的时间Java 在try-catch块中使用if语句不适用于JSoup,java,android,jsoup,Java,Android,Jsoup,我正在尝试让我的Android应用程序连接到互联网,然后使用JSoup从Amazon上删除一些HTML。唯一的问题是,并非所有的亚马逊页面都是相同的,因此我需要创建几个if-else语句来检查我要查找的数据(如标题、图像、价格等)可能位于的不同位置。 唯一的问题是,由于Android要求所有连接到互联网的代码都在try-catch语句中,一个不幸的副作用是一些if语句不能正常工作。如果我尝试将字符串设置为某个值,但它没有找到任何内容,并且将自身设置为null,那么它会立即从try-catch语句
public class getProductAttributes extends AsyncTask<Void,Void,Void>{
String url;
String price;
String title;
String imageSRC;
ImageView productView;
int result;
public getProductAttributes(String url){
this.url = url;
}
protected Void doInBackground (Void... voids) {
try{
//Create JSoup connection
Document doc = Jsoup.connect(url).get();
//Checks the HTML code of Amazon for the title and image, the elements are nested HTML tags that lead me to the <img> tag that I need to retrieve my data.
Elements link= doc.select("ul.a-unordered-list.a-nostyle.a-horizontal.list.maintain-height")
.select("li.image.item.itemNo0.selected.maintain-height")
.select("span.a-list-item")
.select("span.a-declarative")
.select("div.imgTagWrapper");
//If an image is vertical (contains a-stretch-vertical,) this code works fine because it never checks a-stretch-horizontal. If the image contains a-stretch-horizontal however, the rest of the code isn't read and the else statement doesn't do its job
if(link.select("img.a-dynamic-image.a-stretch-vertical").equals(null)){
link = link.select("img.a-dynamic-image.a-stetch-horizontal");
}else{ link= link.select("img.a-dynamic-image.a-stretch-vertical");}
//Gets Title and Image Source information from the attributes inside <img>
title= link.attr("alt");
imageSRC= link.attr("data-old-hires");
}catch (Exception e){e.printStackTrace();}
公共类getProductAttributes扩展异步任务{
字符串url;
串价;
字符串标题;
字符串imageSRC;
ImageView产品视图;
int结果;
公共getProductAttributes(字符串url){
this.url=url;
}
受保护的空位背景(空位…空位){
试一试{
//创建JSoup连接
Document doc=Jsoup.connect(url.get();
//检查Amazon的HTML代码中的标题和图像,这些元素是嵌套的HTML标记,可以引导我找到代码
if(link.select("img.a-dynamic-image.a-stretch-vertical").equals(null)){
没有意义。返回一个集合,它永远不会是.equals(null)
。如果要检查选择是否返回任何内容,请使用或查看返回集合的名称:
if(link.select("img.a-dynamic-image.a-stretch-vertical").isEmpty()){
// It's empty
请注意,try/catch
的存在对其中的代码没有任何影响。它唯一改变的是该代码抛出的错误发生了什么。代码
if(link.select("img.a-dynamic-image.a-stretch-vertical").equals(null)){
没有意义。返回一个集合,它永远不会是.equals(null)
。如果要检查选择是否返回任何内容,请使用或查看返回集合的名称:
if(link.select("img.a-dynamic-image.a-stretch-vertical").isEmpty()){
// It's empty
请注意,try/catch
的存在对其中的代码没有任何影响。它唯一改变的是该代码抛出的错误会发生什么情况。好的,这是有道理的。但是,我的代码仍然不能正常工作。如果图片位于img.a-dynamic-image.a-stetch-horizontal中,则代码将插入ide仍然没有执行if语句。@coolyfrost::-)不知道该告诉你什么,如果它在那里并且是由link
匹配的元素的后代,select
将找到它。好的,这是有意义的。但是,我的代码仍然不能正常工作。如果图片是img.a-dynamic-image.a-stetch-horizontal,那么代码在if语句内部仍然没有执行。@coolyfrost::-)不知道该告诉你什么,如果它在那里并且是由链接匹配的元素的后代,选择将找到它。如果是那一系列。选择(…)。选择(…)。选择(…)
,你可以查看。:-)如果是那一系列。选择(…)。选择(…)。选择(…)
,您可以查看。:-)