开发者

Android app to push updating data

I new to developing in android but I'm not in java. So I want to develop an app that checks for football match scores and as soon as new data is available 开发者_运维百科the android app must push that data to the user in a notification fashion.

My question is the following:

Can I use a website's server that is not mine to get data from since I dont have a server to use. Therefore I cant use C2DM

If not what is the solution to this: TCP/IP connection or can I customize a webview to my liking?

Thanks in advance, Roy


My experience with using internet data, however this may help you start.

This is the class that I use to download webpages and return them as a string, it should be possible to parse the page data to extract the data that you want. Something you should be careful is that it can be common for webpages to change their format which could break your parsing functionality, perhaps without you even realising.

Check this out for 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()
    {}

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜