我在我的android程序中显示奇怪的文本。每隔一段时间我就会得到一个奇怪的符号,即首都A有一个^顶部......任何想法?
这是我的代码
package com.news;
import java.io.IOException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class NewsActivity extends Activity {
WebView mWebView;
String test2 = "<html><body><table border=0 width=10 height=10>";
Document docs;
Document writing;
String text(String link)
{
String full ="<html><body><table border=0 cellpadding=2 cellspacing=2 height = 10><tr><td>";;
try {
writing = Jsoup.connect(link).get();
} catch (IOException e) {
e.printStackTrace();
}
Element heading = writing.select("h2").first();
Elements classname = writing.getElementsByClass("news");
Elements items = classname.select("p");
full = full + heading.toString() + "<br>" + items.get(0).toString() + "</td></tr>";
Element imgs2 = writing.select("div.News img").first();
//Elements imgs2 = writing.select("img[src$=.jpg]");
String picture = imgs2.absUrl("src");
String newImg = "<img src=\"" + picture + "\"/ width =100 >";
full = full + "<tr><td>" + newImg + "</td></tr>";
full = full + "<tr><td>";
for (int i = 1; i< items.size(); i++)
{
full = full + items.get(i).toString();
}
full = full + "</td></tr></table></body></html>";
return full;
}
public void main(String... args)
{
try
{
docs = Jsoup.connect("http://www.dcu.ie/news/index.shtml").get();
}
catch (IOException e)
{
e.printStackTrace();
}
//Elements imgs = docs.select("img");
Elements imgs = docs.select("div.news-feature img");
//Elements imgs = docs.select("img[src$=.jpg]");
Elements txt = docs.select("h2");
Elements article = docs.getElementsByClass("date");
Elements links = article.select("a[href]");
for (int i = 0; i < imgs.size(); i++){
String url = imgs.get(i).absUrl("src");
String temp = links.get(i).absUrl("href");
String temp2 = "<a href=\"" + temp + "\"/>";
String newImg = temp2 + "<img src=\"" + url + "\"/ width =100 >";
test2 = test2 + "<tr>";
test2 = test2 + "<td>";
test2 = test2 + " " + newImg + " ";
test2 = test2 + "</td>";
test2 = test2 + "<td><h6>";
test2 = test2 + txt.get(i).toString();
test2 = test2 + "</h6></td>";
test2 = test2 + "</tr>";
}
test2 = test2 + "</table>";
test2 = test2 + "</html></body>";
}
public void onCreate(Bundle savedInstanceState) {
main();
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mWebView = (WebView) findViewById(R.id.webview);
mWebView.setWebViewClient(new NewsClient());
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setDomStorageEnabled(true);
mWebView.loadData(test2, "text/html", "utf-8");
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
mWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
private class NewsClient extends WebViewClient {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
String newUrl = text(url);
view.loadData(newUrl, "text/html", "utf-8");
return true;
}
}
}
答案 0 :(得分:1)
不是真正的答案,但你应该有一个有效的HTML。
String full ="<html><body><table border='0' cellpadding='2' cellspacing='2' height='10'><tr><td>";
将参数内容放在'或'。
之间其次,为什么要使用'text / html'和'utf-8'调用loadData。虽然此url的内容不是utf-8而是iso-8859-1。那说你加载了错误的编码,因此显示错误。