![]() |
|
|
|||||||
![]() |
Java - HTMLEditorKit is throwing exception |
|
|
Thread Tools | Search this Thread |
|
|
#1 |
|
Hi!
What is wrong with this code: HTMLDocument html = new HTMLDocument(); try { HTMLEditorKit kit = new HTMLEditorKit(); URL url = new URL("http://www.google.com/"); kit.read(new BufferedReader(new InputStreamReader(url.openStream())), html, 0); } catch (Exception e) { System.err.println("Error"); } Exception is thrown every time. (Yes, I am connected to the Internet and I can read from reader - I have tryed reading from new BufferedReader(new InputStreamReader(url.openStream())) Reader and it works.) Mike Mike Mimic |
|
|
|
|
#2 |
|
Posts: n/a
|
On Tue, 20 Apr 2004 18:37:49 +0200, Mike Mimic <>
wrote: >Hi! > >What is wrong with this code: > >HTMLDocument html = new HTMLDocument(); >try { > HTMLEditorKit kit = new HTMLEditorKit(); > URL url = new URL("http://www.google.com/"); > kit.read(new BufferedReader(new > InputStreamReader(url.openStream())), html, 0); >} >catch (Exception e) { > System.err.println("Error"); >} > >Exception is thrown every time. (Yes, I am connected to the Internet >and I can read from reader - I have tryed reading from new >BufferedReader(new InputStreamReader(url.openStream())) Reader and it >works.) What's the Exception? Just printing "error" doesn't tell you much. Try e.printStackTrace(); -- now with more cowbell Bryce (Work) |
|
|
|
#3 |
|
Posts: n/a
|
Hi!
Bryce (Work) wrote: > What's the Exception? Just printing "error" doesn't tell you much. Try > e.printStackTrace(); Good idea. I have been reading message but it was null. Here it is: javax.swing.text.ChangedCharSetException at javax.swing.text.html.parser.DocumentParser.handle EmptyTag(Unknown Source) at javax.swing.text.html.parser.Parser.startTag(Unkno wn Source) at javax.swing.text.html.parser.Parser.parseTag(Unkno wn Source) at javax.swing.text.html.parser.Parser.parseContent(U nknown Source) at javax.swing.text.html.parser.Parser.parse(Unknown Source) at javax.swing.text.html.parser.DocumentParser.parse( Unknown Source) at javax.swing.text.html.parser.ParserDelegator.parse (Unknown Source) at javax.swing.text.html.HTMLEditorKit.read(Unknown Source) at test.main(test.java:1 Does not tell me much. Empty tag? Mike Mike Mimic |
|
|
|
#4 |
|
Posts: n/a
|
Mike Mimic wrote:
> Here it is: > > javax.swing.text.ChangedCharSetException Mike, When you provide a Reader to HTMLEditorKit to retrieve an HTML page, you're giving it a character stream that isn't encoded at all. If it encounters a meta tag in the HTML source that tries to change the encoding, it will therefore throw an exception, because it has no way to honor that request. You can set a property on the document to tell it to ignore this. The code looks like this: doc.putProperty("IgnoreCharsetDirective", new Boolean(true)); -- www.designacourse.com The Easiest Way to Train Anyone... Anywhere. Chris Smith - Lead Software Developer/Technical Trainer MindIQ Corporation Chris Smith |
|
|
|
#5 |
|
Posts: n/a
|
Hi!
Chris Smith wrote: > When you provide a Reader to HTMLEditorKit to retrieve an HTML page, > you're giving it a character stream that isn't encoded at all. Encoded to what? How can I then encode stream so that HTMLEditorKit will be able to change the encoding? Mike Mike Mimic |
|
|
|
#6 |
|
Posts: n/a
|
Mike Mimic wrote:
> Encoded to what? > > How can I then encode stream so that HTMLEditorKit will be able to > change the encoding? Though I haven't tried it, I believe you may have better luck if you used InputStream instead of Reader. Alternatively, just set that property. -- www.designacourse.com The Easiest Way to Train Anyone... Anywhere. Chris Smith - Lead Software Developer/Technical Trainer MindIQ Corporation Chris Smith |
|
|
|
#7 |
|
Posts: n/a
|
Hi!
Chris Smith wrote: > Though I haven't tried it, I believe you may have better luck if you > used InputStream instead of Reader. Alternatively, just set that > property. I had only BufferStream (chained to InputStream) but it was the same. Mike Mike Mimic |
|
|
|
#8 |
|
Posts: n/a
|
Mike Mimic wrote:
> Chris Smith wrote: > > Though I haven't tried it, I believe you may have better luck if you > > used InputStream instead of Reader. Alternatively, just set that > > property. > > I had only BufferStream (chained to InputStream) but it was the same. Yep, after looking into it, turns out that the default parser just gives up anyway, rather than switching encodings. Set that property and avoid the whole mess. -- www.designacourse.com The Easiest Way to Train Anyone... Anywhere. Chris Smith - Lead Software Developer/Technical Trainer MindIQ Corporation Chris Smith |
|
|
|
#9 |
|
Posts: n/a
|
Hi!
Chris Smith wrote: > Yep, after looking into it, turns out that the default parser just gives > up anyway, rather than switching encodings. Set that property and avoid > the whole mess. I have. It works now. Thanks. Mike Mike Mimic |
|
![]() |
| Thread Tools | Search this Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| java.net.SocketException: Broken pipe exception at HttpServletRequest | sadasivar | Software | 0 | 03-11-2008 04:40 AM |
| exception unknown software exception error - Excel.exe | mfinamore | Software | 0 | 11-15-2007 04:54 PM |
| Exception in thread "main" java.lang.NoClassDefFoundError: | prakash.nmsp@gmail.com | Software | 0 | 06-29-2007 10:03 AM |
| Help on PrincipalPermission Demand Throwing Exception | Abba Biya | MCTS | 1 | 02-07-2007 10:05 PM |
| how to overcome the nullpointer exception | arputharaj | Software | 0 | 11-11-2006 07:05 AM |