我有一個ListView,點擊一個項目後,一個新的活動通過交付意向來調用。所有活動都有一個webview,並在同一個url中加載不同的url或不同的部分(同一個url的不同div元素)。Android WebView無法在一個活動中加載網址,但在另一個活動中加載
我面臨的問題是隻有一個Activity在web視圖中加載url,剩下的顯示一個空白的webview。這可能是Jsoup的問題嗎?
另請注意,當我只運行Jsoup代碼來獲取數據並輸出到控制檯時,它可以工作;但不是當我在活動中使用它時。
以下是兩項活動的代碼(它們僅在doc.select()
的單行中有所不同)。活動1工作得很好,並在web視圖中顯示html,而活動2顯示一個空白屏幕。
活動1:
public class Article extends Activity {
ProgressDialog mProgressDialog;
WebView wv;
private static final int SUCCESS = 1;
private static final int NETWORK_ERROR = 2;
private static final int ERROR = 3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.article);
wv = (WebView) findViewById(R.id.webview1);
new getHtml().execute();
}
private class getHtml extends AsyncTask<String, Void, Integer> {
Elements tfa;
@Override
protected void onPreExecute() {
mProgressDialog = new ProgressDialog(Article.this);
mProgressDialog.setTitle("Wikipedia");
mProgressDialog.setMessage("Loading today's featured article...");
mProgressDialog.setIndeterminate(false);
mProgressDialog.show();
}
@Override
protected Integer doInBackground(String... params) {
try {
// Connect to the web site
Document doc = Jsoup.connect(MainActivity.url).get();
tfa = doc.select("div#mp-tfa");
return SUCCESS;
} catch (UnknownHostException e) {
Log.e("Unknown Host Exception", "Network error", e);
return NETWORK_ERROR;
} catch (IOException e) {
Log.e("IO Exception", "Failed to load HTML", e);
return ERROR;
} catch (Exception e) {
Log.e("Exception", "An exception occured", e);
return ERROR;
}
}
@Override
protected void onPostExecute(Integer result) {
if (result == 2) {
Toast.makeText(
getApplicationContext(),
"Network connection error. Check your internet connection and try again.",
Toast.LENGTH_LONG).show();
} else if (result == 3) {
Toast.makeText(getApplicationContext(),
"Unknown error. Failed to load wikipedia.",
Toast.LENGTH_LONG).show();
} else if (result == 1) {
wv.setWebViewClient(new WebViewClient());
String tfa_html = tfa.outerHtml();
tfa_html = tfa_html.replaceAll("a href=\"/wiki/",
"a href=\"http://en.wikipedia.org/wiki/");
tfa_html = tfa_html.replaceFirst(
"src=\"//upload.wikimedia.org/",
"src=\"http://upload.wikimedia.org/");
wv.loadData(tfa_html, "text/html", null);
}
mProgressDialog.dismiss();
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// Check if the key event was the Back button and if there's history
if ((keyCode == KeyEvent.KEYCODE_BACK) && wv.canGoBack()) {
wv.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
活動2:
public class Article extends Activity {
ProgressDialog mProgressDialog;
WebView wv;
private static final int SUCCESS = 1;
private static final int NETWORK_ERROR = 2;
private static final int ERROR = 3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.article);
wv = (WebView) findViewById(R.id.webview1);
new getHtml().execute();
}
private class getHtml extends AsyncTask<String, Void, Integer> {
Elements tfa;
@Override
protected void onPreExecute() {
mProgressDialog = new ProgressDialog(Article.this);
mProgressDialog.setTitle("Wikipedia");
mProgressDialog.setMessage("Loading today's featured article...");
mProgressDialog.setIndeterminate(false);
mProgressDialog.show();
}
@Override
protected Integer doInBackground(String... params) {
try {
// Connect to the web site
Document doc = Jsoup.connect(MainActivity.url).get();
tfa = doc.select("div#mp-tfp");
return SUCCESS;
} catch (UnknownHostException e) {
Log.e("Unknown Host Exception", "Network error", e);
return NETWORK_ERROR;
} catch (IOException e) {
Log.e("IO Exception", "Failed to load HTML", e);
return ERROR;
} catch (Exception e) {
Log.e("Exception", "An exception occured", e);
return ERROR;
}
}
@Override
protected void onPostExecute(Integer result) {
if (result == 2) {
Toast.makeText(
getApplicationContext(),
"Network connection error. Check your internet connection and try again.",
Toast.LENGTH_LONG).show();
} else if (result == 3) {
Toast.makeText(getApplicationContext(),
"Unknown error. Failed to load wikipedia.",
Toast.LENGTH_LONG).show();
} else if (result == 1) {
wv.setWebViewClient(new WebViewClient());
String tfa_html = tfa.outerHtml();
tfa_html = tfa_html.replaceAll("a href=\"/wiki/",
"a href=\"http://en.wikipedia.org/wiki/");
tfa_html = tfa_html.replaceFirst(
"src=\"//upload.wikimedia.org/",
"src=\"http://upload.wikimedia.org/");
wv.loadData(tfa_html, "text/html", null);
}
mProgressDialog.dismiss();
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// Check if the key event was the Back button and if there's history
if ((keyCode == KeyEvent.KEYCODE_BACK) && wv.canGoBack()) {
wv.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
我曾經提到過,當我在一個單獨的java程序中運行它時,jsoup將該div的輸出打印到控制檯。該節點存在。 – gopi1410
Log.i(「ARTICLE DATA FETCH」,tfa_html.substring(0,30)); – davisli
tfa_html的len(「div#mp-tfp」的節點)> = 30? – davisli