Android 使用 Jsoup 解析问题 HTML
Android parse trouble HTML using Jsoup
我们在 android studio.the 中使用 jsoup 实时解析 url 时遇到问题 studio.the 相同的代码将在 eclipse.we 中工作得到错误
Method threw 'java.lang.NullPointerException' exception.can not evaluate org.jsoup.parser.HtmlTreebuilder.tostring()
这是我的 activity 代码
String title=null;
Document document;
try {
document= Jsoup.connect("https://www.facebook.com/")
.userAgent("Mozilla/5.0 (Windows; U; WindowsNT 5.1; en-US; rv1.8.1.6) Gecko/20070725 Firefox/2.0.0.6")
.referrer("http://www.google.com")
.get();
title=document.title();
TextView text=(TextView)findViewById(R.id.textView);
text.setText(title);
} catch (Exception e) {
e.printStackTrace();
Log.d("tag","document");
}
如何解决这个问题并从直播中获取标题url?
你可以使用AsyncTask
class来避免NetworkOnMainThreadException
在这里你可以试试这个代码。
private class FetchWebsiteData extends AsyncTask<Void, Void, Void> {
String websiteTitle, websiteDescription;
@Override
protected void onPreExecute() {
//progress dialog
}
@Override
protected Void doInBackground(Void... params) {
try {
// Connect to website
Document document = Jsoup.connect(URL).get();
// Get the html document title
websiteTitle = document.title();
Elements description = document.select("meta[name=description]");
// Locate the content attribute
websiteDescription = description.attr("content");
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
// Set title into TextView
TextView txttitle = (TextView) findViewById(R.id.txtData);
txttitle.setText(websiteTitle + "\n" + websiteDescription);
mProgressDialog.dismiss();
}
}
}
并添加你的 mainActivity 这个
new FetchWebsiteData().execute();
我们在 android studio.the 中使用 jsoup 实时解析 url 时遇到问题 studio.the 相同的代码将在 eclipse.we 中工作得到错误
Method threw 'java.lang.NullPointerException' exception.can not evaluate org.jsoup.parser.HtmlTreebuilder.tostring()
这是我的 activity 代码
String title=null;
Document document;
try {
document= Jsoup.connect("https://www.facebook.com/")
.userAgent("Mozilla/5.0 (Windows; U; WindowsNT 5.1; en-US; rv1.8.1.6) Gecko/20070725 Firefox/2.0.0.6")
.referrer("http://www.google.com")
.get();
title=document.title();
TextView text=(TextView)findViewById(R.id.textView);
text.setText(title);
} catch (Exception e) {
e.printStackTrace();
Log.d("tag","document");
}
如何解决这个问题并从直播中获取标题url?
你可以使用AsyncTask
class来避免NetworkOnMainThreadException
在这里你可以试试这个代码。
private class FetchWebsiteData extends AsyncTask<Void, Void, Void> {
String websiteTitle, websiteDescription;
@Override
protected void onPreExecute() {
//progress dialog
}
@Override
protected Void doInBackground(Void... params) {
try {
// Connect to website
Document document = Jsoup.connect(URL).get();
// Get the html document title
websiteTitle = document.title();
Elements description = document.select("meta[name=description]");
// Locate the content attribute
websiteDescription = description.attr("content");
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
// Set title into TextView
TextView txttitle = (TextView) findViewById(R.id.txtData);
txttitle.setText(websiteTitle + "\n" + websiteDescription);
mProgressDialog.dismiss();
}
}
}
并添加你的 mainActivity 这个
new FetchWebsiteData().execute();