【问题标题】:Android app to push updating data用于推送更新数据的 Android 应用
【发布时间】:2011-09-07 00:39:06
【问题描述】:

我是在 android 中开发的新手,但我不在 java 中。所以我想开发一个应用程序来检查足球比赛比分,一旦有新数据可用,android 应用程序必须以通知方式将该数据推送给用户。

我的问题如下:

我可以使用不是我的网站服务器来获取数据,因为我没有服务器可以使用。因此我不能使用 C2DM

如果不是,解决方案是什么:TCP/IP 连接,或者我可以根据自己的喜好自定义 web 视图吗?

提前致谢, 罗伊

【问题讨论】:

    标签: java android eclipse notifications push


    【解决方案1】:

    我使用互联网数据的经验,但这可能会帮助您开始。

    这是我用来下载网页并将它们作为字符串返回的类,应该可以解析页面数据以提取您想要的数据。您应该注意的一点是,网页更改其格式是很常见的,这可能会破坏您的解析功能,甚至可能您都没有意识到。

    查看Java HTML parsers

    package AppZappy.NIRailAndBus;
    
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    
    
    /**
     * Simplifies downloading of files from the internet
     */
    public class FileDownloading
    {
        /**
         * Download a text file from the Internet
         * @param targetUrl The URL of the file to download
         * @return The string contents of the files OR NULL if error occurred
         */
        public static String downloadFile(String targetUrl)
        {
            BufferedReader in = null;
            try
            {
                // Create a URL for the desired page
                URL url = new URL(targetUrl);
    
                // Read all the text returned by the server
                in = new BufferedReader(new InputStreamReader(url.openStream()));
    
                StringBuilder sb = new StringBuilder(16384); // 16kb
                String str = in.readLine();
                if (str != null)
                {
                    sb.append(str);
                    str = in.readLine();
                }
                while (str != null)
                {
                    // str is one line of text; readLine() strips the newline
                    // character(s)
                    sb.append(C.new_line());
                    sb.append(str);
                    str = in.readLine();
                }
    
                String output = sb.toString();
                return output;
            }
            catch (MalformedURLException e)
            {}
            catch (IOException e)
            {}
            finally
            {
                try
                {
                    if (in != null) in.close();
                }
                catch (IOException e)
                {
    
                }
            }
            return null;
        }
    
        private FileDownloading()
        {}
    
    }
    

    【讨论】:

    • 感谢 kurru 的帮助,但这是否意味着轮询消耗资源和电池寿命的数据?我可以让应用程序检查新数据(推送)吗?当更改退出时,它会开始下载文件,如果可以,我如何跟踪更改我不想为文本文件创建 sql 数据库,这将是低效的。跨度>
    • 您需要自己的服务器应用程序来维护会提醒您注意新数据的下游。至于存储文件,你只需要最近的一个,SQLite 存储大量文本没有问题,所以这不应该被认为是一个问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-29
    • 2021-11-17
    • 2014-06-08
    • 1970-01-01
    相关资源
    最近更新 更多