你能告诉我这里有什么问题吗? TextView 始终不返回任何内容。 我既没有得到异常(exception)也没有时间。 我还检查了时间服务器是否正常工作并且确实如此。 请帮我。这几天我一直很绝望。
TextView zeit;
public static final String TIME_SERVER = "nist1-macon.macon.ga.us";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_vorbereitung);
zeit = (TextView)findViewById(R.id.zeit);
try {
zeit.setText(Long.toString(getCurrentNetworkTime()));
} catch (Exception e) {
zeit.setText(e.getMessage());
}
}
public static long getCurrentNetworkTime() throws Exception{
NTPUDPClient timeClient = new NTPUDPClient();
InetAddress inetAddress = InetAddress.getByName(TIME_SERVER);
TimeInfo timeInfo = timeClient.getTime(inetAddress);
long returnTime = timeInfo.getMessage().getTransmitTimeStamp().getTime();
return returnTime;
}
最佳答案
尝试这样做:
try {
zeit.setText(String.valueOf(getCurrentNetworkTime()));
} catch (Exception e) {
zeit.setText(e.getMessage());
}
String
类有一个static
方法valueOf()
,它接受一个 long 并将其转换为一个 String
.
关于java - 安卓服务器时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33625151/