在我制作的基于java的web浏览器中使用cobra api呈现html页面
COBRA api要求输入InputStream和uri作为要解析的输入,但我无法在其parse()方法中进行解析。如果我们使用下面给出的方法,将引发连接超时异常。如何使用该api在我用java创建的web浏览器中成功呈现和显示网页。。?? 代码如下:在我制作的基于java的web浏览器中使用cobra api呈现html页面,java,browser,cobra,Java,Browser,Cobra,COBRA api要求输入InputStream和uri作为要解析的输入,但我无法在其parse()方法中进行解析。如果我们使用下面给出的方法,将引发连接超时异常。如何使用该api在我用java创建的web浏览器中成功呈现和显示网页。。?? 代码如下: private void createNewTab() throws Exception{ //JPanel panel=new JPanel(new BorderLayout()); HtmlPanel panel = ne
private void createNewTab() throws Exception{
//JPanel panel=new JPanel(new BorderLayout());
HtmlPanel panel = new HtmlPanel();
WebBrowserPane browserPane = new WebBrowserPane();
WebToolBar toolBar = new WebToolBar(browserPane);
//panel.add(toolBar, BorderLayout.NORTH);
//panel.add(new JScrollPane(browserPane), BorderLayout.CENTER);
UserAgentContext ucontext = new SimpleUserAgentContext();
SimpleHtmlRendererContext rcontext = new SimpleHtmlRendererContext(panel, ucontext);
// Note that document builder should receive both contexts.
DocumentBuilderImpl dbi = new DocumentBuilderImpl(ucontext, rcontext);
// A documentURI should be provided to resolve relative URIs.
// Open a connection on the URL we want to render first.
String uri = "http://lobobrowser.org/browser/home.jsp";
URL url = new URL(uri);
URLConnection connection = url.openConnection();
connection.connect();
InputStream in = connection.getInputStream();
// A Reader should be created with the correct charset,
// which may be obtained from the Content-Type header
// of an HTTP response.
Reader reader = new InputStreamReader(in);
Document document = dbi.parse(new InputSourceImpl(reader, uri));
// Now set document in panel. This is what causes the document to render.
panel.setDocument(document, rcontext);
tabbedPane.addTab("Browser " + tabbedPane.getTabCount(), panel);
}
private class NewTabAction extends AbstractAction {
public NewTabAction() {
putValue(Action.NAME, "New Browser Tab");
putValue(Action.SHORT_DESCRIPTION, "Create New Web Browser Tab");
putValue(Action.MNEMONIC_KEY, new Integer('N'));
}
public void actionPerformed(ActionEvent event) {
try{ createNewTab();} catch(Exception e){e.printStackTrace();}
}
}
private class ExitAction extends AbstractAction {
public ExitAction() {
putValue(Action.NAME, "Exit");
putValue(Action.SHORT_DESCRIPTION, "Exit Application");
putValue(Action.MNEMONIC_KEY, new Integer('x'));
}
public void actionPerformed(ActionEvent event) {
System.exit(0);
}
}
public static void main(String args[]) {
TabbedPaneWebBrowser browser = new TabbedPaneWebBrowser();
browser.setDefaultCloseOperation(EXIT_ON_CLOSE);
browser.setSize(640, 480);
browser.setVisible(true);
}
}
上面的代码片段是我的浏览器的createnewTab()方法,它需要在浏览器的新选项卡中打开网页。什么是COBRA API?