我正在尝试获取网站的标题,代码检查字符串是否包含标题不是' null'。如果它不为null,它将执行其余的代码。
try {
Document htmlDocument = Jsoup.connect("http://afterlifesolutions.com/theaterstore/app-featured.php").userAgent("Mozilla").get();
htmlContentInString = htmlDocument.title();
if(htmlContentInString != null) {
isNull = false;
}
} catch (IOException e) {
e.printStackTrace();
}
问题是
htmlContentInString = htmlDocument.title();
if(htmlContentInString != null)
该应用程序会跳过“如果声明'因为它没有收到标题。但是代码在其他网站上运行得很好。
答案 0 :(得分:0)
我不知道这是你想要的东西,但我认为这可能对你有帮助。我有一些jsoup项目与自定义适配器和listview所以我很少根据你的需要调整它,我得到了这个。基本上我想告诉你如何用jsoup解析你的html。我使用AsyncTask从url获取数据,您应该使用:
try {
document = Jsoup.connect("http://afterlifesolutions.com/theaterstore/app-featured.php").get();
Elements links = document.getElementsByClass("single-product");
for (Element element : links) {
Data data = new Data(); //class where I set and get the data, of course you don't have to follow this patern
Elements getLink = element.select("a.product_link[href]");
String link = getLink.attr("abs:href");
Elements getImage = element.select("img.product_poster[src]");
String image = getImage.attr("abs:src");
String title = element.select("p.product_name").text();
String price = element.select("p.product_price").text();
Log.d(image, title);
data.setUrl(link);
data.setTopic(title);
data.setBitmap(getBitmap(image));
datas.add(data);
}
} catch (IOException e) {
e.printStackTrace();
}
}
如果需要,可以下载图像的方法:
public Bitmap getBitmap(String src)
{
try {
URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream inputStream = connection.getInputStream();
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2;
Bitmap myBitmap = BitmapFactory.decodeStream(inputStream, null, options);
return myBitmap;
}
catch (IOException e)
{
e.printStackTrace();
return null;
}
}
这就是结果: