我正在尝试在我的库中创建一个用于 Volley 用法的示例,但字符串请求部分似乎无法正常工作。代码很简单,但我找不到解决方案来解释为什么当我使用 URL 在线请求字符串时,它还包含来自其来源网站的 HTML 标记。
如何只提取文本而不提取 HTML?
// <<<<<<<<<<<STRING REQUEST>>>>>>>>
// #1: A string from a URL.
String url ="http://httpbin.org/html";
final TextView mTextView = (TextView) findViewById(R.id.stringView);
// #2: Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
// Display the first 300 characters of the response string.
mTextView.setText(response.substring(0,300));
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
mTextView.setText("That didn't work!");
}
});
// #3: Add the request to the RequestQueue.
VolleySingleton.getInstance(this).addToRequestQueue(stringRequest);
我在 TextView
中显示字符串,它应该如下所示:
Herman Melville - Moby-Dick
Availing himself of the mild, summer-cool weather that now reigned in these latitudes, and in preparation for the peculiarly active pursuits shortly to be anticipated, Perth, the begrimed, blistered old blacksmith, had not removed his portable forge to the hold again, after concluding his contributory work for Ahab's leg, but still retained it on deck, fast lashed to ringbolts by the foremast; being now almost incessantly invoked by the headsmen, and harpooneers, and bowsmen to do some little job for them; altering, or repairing, or new shaping their various weapons and boat furniture....
相反,它看起来像这样:
<!DOCTYPE html> <html> <head> </head> <body>
<h1>Herman Melville - Moby-Dick</h1>
<div>
<p>
Availing himself of the mild, summer-cool weather that now reigned in these latitudes, and in preparation for the peculiarly active pursuits shortly to be anticipated, Perth, the begrimed, blistered old blacksmith, had not removed his portable forge to the hold again, after concluding his contributory work for Ahab's leg, but still retained it on deck, fast lashed to ringbolts by the foremast; being now almost incessantly invoked by the headsmen, and harpooneers, and bowsmen to do some little job for them; altering, or repairing, or new shaping their various weapons and boat furniture....
</p>
</div>
</body>
</html>
最佳答案
看起来 Volley 并没有完全将 StringRequest 转换成您可能认为的字符串格式。所以我们必须自己解析HTML。这可以使用 Jsoup 来完成,代码很少。
要将其添加到 Android Studio 中,请打开模块设置,然后选择左上角的“+”号,然后选择“导入 .JAR/.AAR 包”,然后从文件系统中选择 jar 下载。
请务必将 compile 'org.jsoup:jsoup:1.8.3'
添加到您的 build.gradle 文件中。
修改后的代码如下:
// <<<<<<<<<<<STRING REQUEST>>>>>>>>
// #1: A string from a URL.
String url ="http://httpbin.org/html";
final TextView mTextView = (TextView) findViewById(R.id.stringView);
// #2: Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
// Use Jsoup to parse HTML
Document doc = Jsoup.parse(response);
String parsedText = doc.body().text();
// Display the first 300 characters of the response string.
mTextView.setText(parsedText.substring(0,300));
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
mTextView.setText("That didn't work!");
}
});
关于android - 使用 Volley 的字符串请求带有不需要的 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34845475/