我正在为我的博客构建一个应用程序,我正在使用Volley加载URL和Jsoup来解析它。
示例Html
<body class="sin">
<div class="ks">
<div class="wrap">
<div class="content-right-sidebar-wrap">
<main class="content">
//A lot of unneeded tags
<article class="post-1989009 post type-post post" itemscope="" itemtype="http://schema.org/CreativeWork">
<header class="post-header">
<h1 class="post-title" itemprop="headline">Yet Another 6GB RAM Phone: LeEco Le Max 2 Unveiled</h1>
</header>
//A lot of unneeded tags
<div class="post-content" itemprop="text">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam nec nisi lectus. In consectetur nunc accumsan dui molestie, ut ultricies elit lobortis.
<a href="https://website.com/2002/03/odales-cursus-sed-eget-dolor.html">odales cursus sed eget dolor</a> Etiam arcu risus, aliquet porta pharetra non, pharetra in dui..
</p>
<p>
<img class="aligncenter size-full wp-image-19289" src="https://website.com/wp-content/uploads/2002/04/image-39.jpeg" alt="LeEco Le Max 2" width="800" height="450" srcset="https://website.com/wp-content/uploads/2002/09/gutter-bkan.jpeg 800w, https://website.com/wp-content/uploads/2002/09/gutter-bkan-300x169.jpeg 300w, https://website.com/wp-content/uploads/2002/09/gutter-bkan-768x432.jpeg 768w, https://website.com/wp-content/uploads/2002/09/gutter-bkan-265x150.jpeg 265w, https://website.com/wp-content/uploads/2002/09/gutter-bkan-320x180.jpeg 320w" sizes="(max-width: 800px) 100vw, 800px">
</p>
<p>Sed porta aliquet sollicitudin. Vivamus commodo placerat sapien vitae interdum. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus</p>
<p> eu massa volutpat, volutpat ipsum id, maximus risus. Etiam maximus lobortis enim sed eleifend. Integer imperdiet, augue accumsan ultricies faucibus, orci orci porttitor velit, semper fringilla</p>
<img class="aligncenter size-full wp-image-19290" src="https://website.com/wp-content/uploads/2002/07/guter-lop.jpeg" alt="LeEco Le Max 2" width="728" height="324" srcset="https://website.com/wp-content/uploads/2002/07/guter-lop.jpeg 728w, https://website.com/wp-content/uploads/2002/07/guter-lop-300x134.jpeg 300w" sizes="(max-width: 728px) 100vw, 728px">
</p>
<p>Sed nec nunc nec eros vulputate vehicula. Duis laoreet ex vel auctor finibus. Sed semper blandit massa, at molestie ligula vestibulum in. Nulla vestibulum viverra risus vitae fringilla</p>
<h2>Luccuii</h2>
<p>Leuismod ultrices libero at consequat. Quisque vestibulum vulputate vehicula. Vivamus posuere nibh tincidunt tristique faucibus. Integer sed vulputate dui, a luctus sem. Suspendisse potenti.</p>
</div>
//Skipped the closing tags
我的活动
public class PostDetails extends AppCompatActivity{
private final String TAG = "PostDetails";
private AlertDialog internetDialog;
private String postData;
protected com.nostra13.universalimageloader.core.ImageLoader mImageLoader;
TextView postTitle, postContent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_post_details);
showDialog();
postTitle = (TextView) findViewById(R.id.dpost_title);
postContent = (TextView) findViewById(R.id.dpost_content);
postContent.setMovementMethod(LinkMovementMethod.getInstance());
DisplayImageOptions defaultoptions = new DisplayImageOptions.Builder()
.cacheInMemory(true)
.cacheOnDisk(true)
.build();
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
.defaultDisplayImageOptions(defaultoptions)
.writeDebugLogs()
.build();
mImageLoader = com.nostra13.universalimageloader.core.ImageLoader.getInstance();
mImageLoader.init(config);
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
}
if (savedInstanceState != null) {
postData = savedInstanceState.getString("postData");
if (postData != null) {
parseHtml(postData);
} else {
loadPost();
}
} else {
if (NetworkCheck.isAvailableAndConnected(this)) {
//Calling method to load posts
loadPost();
} else {
internetDialog.show();
}
}
}
private void showDialog() {
internetDialog = new AlertDialog.Builder(PostDetails.this)
.setCancelable(false)
.setMessage(R.string.alert_mess_det)
.setPositiveButton(R.string.alert_retry, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (!NetworkCheck.isAvailableAndConnected(PostDetails.this)) {
if (internetDialog != null && internetDialog.isShowing()) {
internetDialog.dismiss();
internetDialog = null;
showDialog();
internetDialog.show();
}
} else {
loadPost();
}
}
})
.setNegativeButton(R.string.alert_cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
.create();
}
private void loadPost() {
Log.d(TAG, "loadPost called");
final ProgressBar progressBar;
progressBar = (ProgressBar) findViewById(R.id.progress_circle);
progressBar.setVisibility(View.VISIBLE);
String news_id = getIntent().getStringExtra("PostId");
Log.d(TAG, "You clicked post id " + news_id);
StringRequest stringRequest = new StringRequest(news_id,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
//Log.d("Debug", response.toString());
if (progressBar != null) {
progressBar.setVisibility(View.GONE);
}
parseHtml(response);
postData = response;
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d("", "Error: " + error.getMessage());
if (progressBar != null) {
progressBar.setVisibility(View.GONE);
}
final AlertDialog.Builder sthWrongAlert = new AlertDialog.Builder(PostDetails.this);
sthWrongAlert.setCancelable(false);
sthWrongAlert.setMessage(R.string.sth_wrongme_det);
sthWrongAlert.setPositiveButton(R.string.alert_retry, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (!NetworkCheck.isAvailableAndConnected(PostDetails.this)) {
internetDialog.show();
} else {
loadPost();
}
}
});
sthWrongAlert.setNegativeButton(R.string.alert_cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
sthWrongAlert.show();
}
});
//Creating requestqueue
RequestQueue requestQueue = Volley.newRequestQueue(this);
//Adding request queue
requestQueue.add(stringRequest);
}
private void parseHtml(String response) {
Log.d(TAG, "parsinghtml");
Document document = Jsoup.parse(response);
String post_title = document.select("h1.post-title").first().text();
String post_content = document.select("div.post-content").first().html();
postTitle.setText(post_title);
Spanned spanned = Html.fromHtml(post_content, new UILImageGetter(postContent, this), null );
postContent.setText(spanned);
//Unhiding views
postTitle.setVisibility(View.VISIBLE);
postContent.setVisibility(View.VISIBLE);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString("postData", postData);
}
@Override
public boolean onSupportNavigateUp() {
onBackPressed();
return true;
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy called");
mImageLoader.destroy();
}
}
postContent
中显示的文字有链接,链接指向其他帖子。
所以我想要的是,当链接(让我们称之为链接A )被点击时,同样的活动( PostDetail )会获得该网址的网址。点击链接并解析它。
当链接A 的内容已加载并解析时,它的postContent
也包含指向其他帖子的链接。点击这些链接时,同样的活动( PostDetail )也应加载并解析它。
注1:帖子页面的html格式相同,因此上面的代码可以成功解析它。
注意2:说实话,除postContent
中存在的帖子链接之外还有其他链接(例如指向其他网站的链接),但导致我博客中帖子的链接开始了用&#34; https://website.com/20&#34; (例如&#34; https://website.com/2013/12/1/post-title&#34;,&#34; https://website.com/2007/11/21/another-post-title&#34;)。
所以我正在考虑检查链接是否以&#34; https://website.com/20&#34;开头,如果是,则将其加载到此活动中;如果不是,则implicint意图用户选择浏览器。