Loading html page problem
Hello,
In my application I load help sites from my website. I use JTextPane setPage function to load a html page into my application. I have a HtmlLoader class - what I use in a thread - which loads the url. See codes below. In my main application I call a function getMyHtml which starts the thread and checks whether the page load was finished.
I have 2 problems with this sollution:
1. Every call consumes some memory and it seems never release it. It means that after the user opens the help pages 100 times the application needs 20-30MB more memory. At the end I get a "Not enough heap memory" error or like this.
2. At the moment I put a html comment at the end of my help pages which I use as endTag in the getMyHtml function to determine whether the page was loaded. It is a quite bad sollution I think. Does anybody know a better sollution?
Thanks!
import java.net.URL;
import javax.swing.JTextPane;
publicclass HtmlLoaderimplements Runnable{
private JTextPane browser =null;
private String url;
public HtmlLoader(JTextPane browser, String url){
this.browser = browser;
this.url= url;
}
publicvoid run(){
try{
browser.setPage(new URL(url));
}
catch (Exception e){
e.printStackTrace();
}
}
}
And the getMyHtml function is :
publicstatic String getMyHtml(String myurl,
JTextPane browser,
String endTag,
int maxWait,
int maxRepeat){
try{
int time = 0;
Runnable r =new HtmlLoader(browser,myurl);
Thread t =new Thread(r);
t.start();
for (int i=0;i<maxRepeat;i++){
while (t.isAlive() && time >< maxWait){
Thread.sleep(50);
time+=50;
}
if (t.isAlive()) t.stop();
elsebreak;
}
time = 0;
while ((browser.getText().indexOf(endTag) == -1) && (time < maxWait)){
Thread.sleep(50);
time += 50;
}
return browser.getText();
}catch (Exception e){
e.printStackTrace();
}
return"";
}

