Java j集团赢得';我不能解析我的网站
我正在尝试获取站点的标题,代码将检查包含标题的字符串是否为“null”。如果它不是null,它将执行代码的其余部分Java j集团赢得';我不能解析我的网站,java,android,jsoup,Java,Android,Jsoup,我正在尝试获取站点的标题,代码将检查包含标题的字符串是否为“null”。如果它不是null,它将执行代码的其余部分 try { Document htmlDocument = Jsoup.connect("http://afterlifesolutions.com/theaterstore/app-featured.php").userAgent("Mozilla").get(); htmlContentInString = htmlDocument.title(); i
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)
应用程序跳过该“if语句”,因为它没有收到标题。但是该代码在其他网站上运行得非常好。我不知道这是否是您想要的,但我认为这可能会对您有所帮助。我有一些带有自定义适配器和listview的jsoup项目,所以我根据您的需要对它进行了一些调整,我得到了这个。基本上,我想向您展示如何使用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;
}
}
这就是结果:
html是否有标题?您是否记录了htmlDocument,也许您没有收到任何东西?@efekActive这是我试图从中获取数据的网站,是的,它有标题tag@Abdenaceur这就是问题所在,该代码在除我之外的所有网站上都运行良好。您的代码对我来说运行良好,并将
HTMLContentString
设置为网站标题。使用JSOUP10.0.1进行测试。这段代码做得非常好,我必须在循环中添加一个'isNull=false'来检查它是否包含数据。谢谢