Android:URL连接访问网站 [英] Android: URL Connection accessing to website

查看:50
本文介绍了Android:URL连接访问网站的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码从网站中提取详细信息.

I am extracting details from a website using the below 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();
             ......
         }
     }

问题:

之前我使用了 Method1 中的 URL,并成功访问和提取了网站详细信息.然而,在这些日子里它不起作用.我现在尝试了 Method2,现在可以使用了.

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.

我想问一下

  1. 如果 Method1 中的随机数对于在访问网站的用户数量巨大时防止缓存很重要,并且

  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 中的直接 URL 有什么缺点?谢谢.

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

在 URLConnection 下添加 con.addHeader("Cache-Control", "no-cache"); 能解决问题吗?

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

推荐答案

尝试使用retrofit2.0 或快速的android 网络库来连接外部Web API.它比使用 AsycTask 更具可读性和易用性这里有一些有用的链接,可以教您如何使用它们

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所有外部连接的惊人库

此外,使用标准库进行解析,如 gson

Also, use standard lib for parsing like gson

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

这篇关于Android:URL连接访问网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆