简体   繁体   中英

Java Question, fetching data from Internet

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?

You have to handle the Activity lifecycle yourself, so unfortunately it's not quite that simple. This answer goes through a good solution to the problem you're describing: Activity restart on rotation Android

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

No. You will have to do it your self.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM