0

Ok, so I have this method, by which i grap html code from any input url:

private String GetURL(String f)throws Exception{ //return html code for input url f
                String k="";        String l="";
                URL url = new URL(f);

        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setConnectTimeout(5000);    // 5 seconds
        conn.setRequestMethod("GET");       
        conn.connect();
        BufferedReader rd  = new BufferedReader(new InputStreamReader(  
                                                    conn.getInputStream()));


        while ((k = rd.readLine()) != null) {
            l = l + k;
        }
        conn.disconnect(); 
        return l; 
       }

It works fine for me. I am using it in an android application on its Main UI. So that when user launches the application, it fetches data from internet using above method and then displays it.

I want to know, is there any way that Java caches this information so that if it is retrieved too soon, it can return result from cache then going online again.

Because the problem is, whenever the user changes the layout, vertical to horizontal; the main display is reset and again user gets the progress bar prompt (which I've designed till the data is loaded). I want to avoid this without using any database complexity.

So want to know if Java offers any cache option automatically or not?

Cristian
  • 198,401
  • 62
  • 356
  • 264
JOhny
  • 1
  • 1. Save the last HTML, and time recieved in another buffer 2. On each call to getUrl, check if the last time you retrieved a webpage was within a short amount of time (200ms). If so, return the HTML in the buffer – Nick Feb 13 '11 at 17:43

1 Answers1

0

So want to know if Java offers any cache option automatically or not?

No. You will have to do it your self.

Cristian
  • 198,401
  • 62
  • 356
  • 264