【问题标题】:Get current date and time from Google in Android在 Android 中从 Google 获取当前日期和时间
【发布时间】:2017-04-13 11:18:51
【问题描述】:

我在 Android 应用程序开发方面有一些经验。现在我们开发了一个 Android 应用程序,我们需要从谷歌或互联网获取确切的日期和时间。我已经从 Stack Overflow 和其他一些站点测试了一些代码,但它不能正常工作。应用程序崩溃了。谁能帮帮我?

【问题讨论】:

  • 你的意思是得到格林威治标准时间?
  • 这样不行。您应该展示您尝试过的方法以及问题所在,我们会尽力帮助解决它

标签: android date time server ntp


【解决方案1】:

试试这个:

private long getTime() throws Exception {
    String url = "https://time.is/Unix_time_now";
    Document doc = Jsoup.parse(new URL(url).openStream(), "UTF-8", url);
    String[] tags = new String[] {
            "div[id=time_section]",
            "div[id=clock0_bg]"
    };
    Elements elements= doc.select(tags[0]);
    for (int i = 0; i <tags.length; i++) {
        elements = elements.select(tags[i]);
    }
    return Long.parseLong(elements.text() + "000");
}

分级:

compile 'org.jsoup:jsoup:1.10.2'

【讨论】:

  • 应用没有崩溃。但给出空值
  • 你添加&lt;uses-permission android:name="android.permission.INTERNET" /&gt;权限了吗?
  • Timeapi 好像下线了
  • 我的回答对您有帮助吗?如果对您有帮助,请点击绿色复选标记接受答案。
【解决方案2】:

这足以得到你想要的:

使用 HttpGet、Client 和 Response,我设法从响应 Date Header 中获取服务器的当前时间。我可以在任何时候都调用它,并且会得到自信的响应(Google 几乎 100% 可用,我可以相信得到正确的日期和时间)

try{
        HttpClient httpclient = new DefaultHttpClient();
        HttpResponse response = httpclient.execute(new HttpGet("https://google.com/"));
        StatusLine statusLine = response.getStatusLine();
        if(statusLine.getStatusCode() == HttpStatus.SC_OK){
            String dateStr = response.getFirstHeader("Date").getValue();
            //Here I do something with the Date String
            System.out.println(dateStr);

        } else{
            //Closes the connection.
            response.getEntity().getContent().close();
            throw new IOException(statusLine.getReasonPhrase());
        }
    }catch (ClientProtocolException e) {
        Log.d("Response", e.getMessage());
    }catch (IOException e) {
        Log.d("Response", e.getMessage());
    }

【讨论】:

  • 在 SDK 23 和 UP 中不再支持 HttpClient
【解决方案3】:

您可以使用以下程序从互联网时间服务器获取时间

import java.io.IOException;

import org.apache.commons.net.time.TimeTCPClient;

public final class GetTime {

public static final void main(String[] args) {
    try {
        TimeTCPClient client = new TimeTCPClient();
        try {
            // Set timeout of 60 seconds
            client.setDefaultTimeout(60000);
            // Connecting to time server
            // Other time servers can be found at : http://tf.nist.gov/tf-cgi/servers.cgi#
            // Make sure that your program NEVER queries a server more frequently than once every 4 seconds
            client.connect("nist.time.nosc.us");
            System.out.println(client.getDate());
        } finally {
            client.disconnect();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}
}

1.您需要Apache Commons Net 库才能使其工作。下载库并添加到您的项目构建路径。

(或者你也可以在这里使用修剪过的 Apache Commons Net Library:https://www.dropbox.com/s/bjxjv7phkb8xfhh/commons-net-3.1.jar。这足以从互联网上获取时间)

2.运行程序。您将在控制台上打印时间。

【讨论】:

    猜你喜欢
    • 2018-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-22
    • 2012-12-04
    • 2012-04-29
    • 2015-11-12
    相关资源
    最近更新 更多