I want to load a HTML form into webview however it is not working for me and Im wondering if its even possible? This is the code I have. Thanks
package com.timetable;
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.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class Timetable extends Activity {
WebView mWebView;
String html = "<html><body>";
Document docs;
public void main(String... args) {
try {
docs = Jsoup.connect("http://www.dcu.ie/timetables/search.shtml").get();
} catch (IOException e) {
e.printStackTrace();
}
Element table = docs.tagName("header-search");
Elements tables = table.select("form");
for (int i = 1; i < tables.size(); i ++)
{
html += tables.get(i).toString() ;
/*while (tables.get(i).text() == "")
i++;
html += "<tr>" + tables.get(i).toString();
while (tables.get(i+1).text() == "")
i++;
html +=tables.get(i+1).toString() + "</tr>";
i++;*/
}
html += "</html></body>";
}
public void onCreate(Bundle savedInstanceState) {
main();
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getWindow().setFeatureInt( Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON);
mWebView = (WebView) findViewById(R.id.webview);
mWebView.setWebViewClient(new TimeClient());
mWebView.setInitialScale(1);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setDomStorageEnabled(true);
mWebView.loadData(html, "text/html", "utf-8");
final Activity MyActivity = this;
mWebView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress)
{
//Make the bar disappear after URL is loaded, and changes string to Loading...
MyActivity.setTitle("Loading...");
MyActivity.setProgress(progress * 100); //Make the bar disappear after URL is loaded
// Return the app name after finish loading
if(progress == 100)
MyActivity.setTitl开发者_JAVA技巧e(R.string.app_name);
}
});
}
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 TimeClient extends WebViewClient {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
}
also don't do this : final Activity MyActivity = this; instead use "MyActivity.this" inside WebChromeClient.onProgressChanged
also there is a gotcha with webView you cant do "loadData(data,mime,enc)" for some reason you need to use : web.loadDataWithBaseURL("fake://host/path",htmldata.toString(), mimetype, encoding, ""); "fake://host/path" is a fake url but you could also use your homepage or something.
so something like :
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getWindow().setFeatureInt( Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON);
mWebView = (WebView) findViewById(R.id.webview);
mWebView.setWebViewClient(new TimeClient());
mWebView.setInitialScale(1);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setDomStorageEnabled(true);
//mWebView.loadData(html, "text/html", "utf-8");
//final Activity MyActivity = this;
mWebView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress)
{
//Make the bar disappear after URL is loaded, and changes string to Loading...
MyActivity.setTitle("Loading...");
MyActivity.setProgress(progress * 100); //Make the bar disappear after URL is loaded
// Return the app name after finish loading
if(progress == 100)
MyActivity.this.setTitle(R.string.app_name);
}
});
}
public void onStart() {
main();
mWebView.loadData("fake://host/path",html, "text/html", "utf-8");
}
public void onStop() {
// some JSoup disconnect code
}
then the webView will load every time the app is launched.
move view.loadUrl(url); to your onStart method.
if you was a loading icon, override onPageStarted & onPageFinished in WebViewClient. then you can use WebChromeClient.onProgressChanged to update your progress bar.
精彩评论