java程序在连接谷歌电子表格时有时会暂停?

时间:2014-12-08 12:21:21

标签: java google-sheets

如果我的程序运行了很长时间,我的程序在连接到谷歌电子表格时会暂停吗?谁能为我提供解决方案...... 在此先感谢...............

public class Index {
    static final SpreadsheetService service = new SpreadsheetService(
            "Print Google Spreadsheet Demo");
    static URL metafeedUrl;
    static SpreadsheetEntry spreadsheet;
    static URL listFeedUrl;
    static ListFeed listFeed;
    static List<String> allBitCoinAddresses = new ArrayList<String>();
    static int counter = 0;
    static int value = 0;
    static FileReader fr;
    static BufferedReader br;
    static String username;
    static String password;
    static String spreadsheet_url;

    public static void main(String[] args) throws IOException,
            ServiceException, URISyntaxException {

        System.out.println("program started....................");
        int reconnect = 0;
        fr = new FileReader(new File("userNpwd.txt"));
        br = new BufferedReader(fr);
        String read;
        int i = 0;
        while ((read = br.readLine()) != null) {
            if (i == 0) {
                username = read;
                i++;
            } else if (i == 1) {
                password = read;
                i++;
            } else {
                spreadsheet_url = read;
                spreadsheet_url = spreadsheet_url.replace("/edit#gid=0", "");
                spreadsheet_url = spreadsheet_url.substring(spreadsheet_url
                        .lastIndexOf('/'));
                spreadsheet_url = "https://spreadsheets.google.com/feeds/spreadsheets"
                        + spreadsheet_url;
            }
        }
        while (reconnect == 0)
            try {

                // Login and prompt the user to pick a sheet to use.
                service.setUserCredentials(username, password);

                // Load sheet
                metafeedUrl = new URL(spreadsheet_url);
                spreadsheet = service.getEntry(metafeedUrl,
                        SpreadsheetEntry.class);
                listFeedUrl = ((WorksheetEntry) spreadsheet.getWorksheets()
                        .get(0)).getListFeedUrl();

                listFeed = service.getFeed(listFeedUrl, ListFeed.class);
                reconnect = 1;
            } catch (Exception e) {
                reconnect = 0;
            }
        while (true) {
            checkForAddressChange();
        }
    }

    private static void checkForAddressChange() throws IOException,
            ServiceException, URISyntaxException {

        ListFeed feed;

        try {
            feed = service.getFeed(listFeedUrl, ListFeed.class);
        } catch (IOException e) {
            return;
        } catch (ServiceException e) {
            return;
        }

        int i = 0;

        // ...............
        try {
            feed = service.getFeed(listFeedUrl, ListFeed.class);
        } catch (IOException e) {
            return;
        } catch (ServiceException e) {
            return;
        }

        System.out.println("checking.................................");

        for (ListEntry entry : feed.getEntries()) {

            try {
                if (!entry.getCustomElements().getValue("BitCoinAddress")
                        .equals(null)
                        & !entry.getCustomElements().getValue("SpreadsheetURL")
                                .equals(null)) {
                    if (counter == 0) {
                        if (value < feed.getEntries().size()) {
                            allBitCoinAddresses.add(entry.getCustomElements()
                                    .getValue("BitCoinAddress"));
                            value += 1;
                            continue;
                        }
                        counter += 1;
                        break;
                    }

                    if (allBitCoinAddresses.get(i).equals(
                            entry.getCustomElements()
                                    .getValue("BitCoinAddress"))) {
                        i += 1;
                        continue;
                    } else {

                        System.out
                                .println("value changed...................................");
                        String url = entry.getCustomElements().getValue(
                                "SpreadsheetURL");
                        url = url.replace("/edit#gid=0", "");
                        url = url.substring(url.lastIndexOf('/'));
                        url = "https://spreadsheets.google.com/feeds/spreadsheets"
                                + url;
                        System.out.println("url is : " + url);
                        Scrapping scrapping = new Scrapping(url);
                        scrapping.deleteEnteries();
                        System.out.println("url is: "
                                + entry.getCustomElements().getValue(
                                        "SpreadsheetURL"));
                        scrapping
                                .insertIntoSpreadsheet(entry
                                        .getCustomElements().getValue(
                                                "BitCoinAddress"));
                        allBitCoinAddresses.set(i, entry.getCustomElements()
                                .getValue("BitCoinAddress"));
                        System.out.println("spreadsheet updated!!!!!!!");
                        i += 1;
                        System.out.println("SIZE OF : "
                                + allBitCoinAddresses.size());

                    }
                }
            } catch (NullPointerException npe) {
                System.out.println("nullllllll");
                continue;
            } catch (IndexOutOfBoundsException iobe) {

                System.out.println("exception in index out of bounds");
                allBitCoinAddresses.add(entry.getCustomElements().getValue(
                        "BitCoinAddress"));

                // change the spreadsheet.........
                String url = entry.getCustomElements().getValue(
                        "SpreadsheetURL");
                url = url.replace("/edit#gid=0", "");
                url = url.substring(url.lastIndexOf('/'));
                url = "https://spreadsheets.google.com/feeds/spreadsheets"
                        + url;

                Scrapping scrapping = new Scrapping(url);
                scrapping.deleteEnteries();
                System.out.println("url is: "
                        + entry.getCustomElements().getValue("SpreadsheetURL"));
                scrapping.insertIntoSpreadsheet(entry.getCustomElements()
                        .getValue("BitCoinAddress"));

                continue;
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我建议使用一个线程来连接和阅读部分。 即使由于某种原因,您的应用程序需要时间连接(可能是网络问题,慢速计算机或不是什么),该过程将在后台运行,因此不会暂停整个应用程序。