我正在创建一个运行asynctask的Web应用程序。使用JSoup,每次代码循环遍历div容器中的图像时,我都希望代码创建一个ImageView对象,在其中将某些图像放入OnPostExecute方法中。
例如,我有此页面:https://www.flagrantdelit.ca/lever-le-voile/
我能够从div class =“ entry-content”中获取所有文本,但是每次我的代码循环遍历包含Image的p标签时,我想创建一个ImageView来包含相同的图像。我需要保持与网站相同的格式。
我尝试检查p标记内的空文本,然后创建ImageView,但是它给了我这个错误:
影像检视
(android.content.Context)
在ImageView中无法应用
至
(com.example.leflagrantdlit.ArticlesFocused.DownloadData)
@Override
protected void onPostExecute(String s)
{
super.onPostExecute(s);
Log.d(TAG, "onPostExecute: Ended downloading AsyncTask");
tvFocused = findViewById(R.id.tvFocused);
tvFocused.setText(stringBuilder);
}
private String downloadHtml(String urlPath)
{
try {
Document doc = Jsoup.connect(urlPath).get();
String title = doc.getElementsByClass("entry-title").get(0).text();
Elements pContent = doc.select("div.entry-content > p");
Elements imgContent = doc.select("div.entry-content > p > img");
for (Element textContent : pContent)
{
if(textContent == null){
ImageView imageView = new ImageView(this);
}
stringBuilder.append(textContent.text());
Log.d(TAG, "downloadHtml: " + textContent.text());
stringBuilder.append("\n");
}
//String title = doc.getElementsByClass("entry-title").get(0).text();
Log.d(TAG, "downloadHtml: " + urlPath + title + stringBuilder);
return null;
} catch (MalformedURLException e) {
Log.e(TAG, "downloadHTML: Invalid Url " + e.getMessage());
} catch (IOException e) {
Log.e(TAG, "downloadHTML: IO Exception reading data: " + e.getMessage());
} catch (SecurityException e) {
Log.e(TAG, "downloadHTML: Security Exception. Need permission? " + e.getMessage());
}
return null;
}
例如,我要将这张图片放在段落的这一部分。