Android: URL Connection accessing to website

2019-08-17 07:59发布

问题:

I am extracting details from a website using the below code.

Code:

private class FetchAllData extends AsyncTask<Void, Void, Void> 
    {
         @Override
         protected void onPreExecute() 
         {
             super.onPreExecute();              
             Utilities.custom_toast(CurrentResult.this, "Refreshing", "gone!", "short", "vertical");
         }

         @Override
         protected Void doInBackground(Void... params) 
         {
            try 
            {
                //String urlX = URL1 + "?x=" + new Random().nextInt(100000); //Method1
                String urlX = URL1 //Method2;
                URL url = new URL(""+ urlX);
                URLConnection con = url.openConnection();
                con.setUseCaches(false); //This will stop caching!
                // BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
                BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
                String inputLine;
                PageCode = "";
                OriginalPageCode = "";
                while ((inputLine = in.readLine()) != null) 
                {
                    PageCode += inputLine;
                }                   
                OriginalPageCode = PageCode;
                toast_IshtmlObtained = urlX+ "\nHTML success obtained as follows:\n\n";
                try
                {
                    extract_website_and_save();
                    toast_IsInfoExtracted = "success extracting website";
                }
                catch (Exception e1)
                {
                    toast_IsInfoExtracted = "error extracting website";
                }

                in.close();
            } 
            catch (Exception e) 
            {
                PageCode = "ERROR: " + e;
                toast_IshtmlObtained = "HTML not obtained:\nHTML retrieved as follows:" + PageCode;
            }
            return null;
         }

         @Override
         protected void onPostExecute(Void result) 
         { 
             Utilities.custom_toast(CurrentResult.this, "Done", "gone!", "short", "vertical");

             setText();
             ......
         }
     }

Question:

Beforehand I was using the URL in Method1 and was successful to access and extract the website details. However, in these days it does not work. I now tried Method2 and it works now.

I would like to ask

  1. if the random number in Method1 is important for preventing caches if the number of users accessing to the website is enormous, and

  2. what is the drawback if using the direct URL as in Method2? Thanks.

  3. Would adding under URLConnection con.addHeader("Cache-Control", "no-cache"); solve the problem ?

回答1:

Try to use retrofit2.0 or fast android networking library to connect to external web Apis. it's more readable and easy to use than using AsycTask here are helpful links that will teach you how to use them

https://github.com/amitshekhariitbhu/Fast-Android-Networking its amazing lib for all external connections

Also, use standard lib for parsing like gson

http://www.vogella.com/tutorials/JavaLibrary-Gson/article.html