in my application i'm try to use this helpful document about get server time, but i get this error:
Caused by: android.os.NetworkOnMainThreadException at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1128)
at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
at java.net.InetAddress.getByName(InetAddress.java:289)
at ir.tsms.EitaaPro.Activities.ActivityBootstrap.getCurrentNetworkTime
my developed code is:
public static void getCurrentNetworkTime() {
NTPUDPClient timeClient = new NTPUDPClient();
timeClient.setDefaultTimeout(3000);
InetAddress inetAddress = null;
try {
inetAddress = InetAddress.getByName(G.TIME_SERVER);
TimeInfo timeInfo = null;
timeInfo = timeClient.getTime(inetAddress);
long localTime = timeInfo.getReturnTime(); //local device time
long serverTime = timeInfo.getMessage().getTransmitTimeStamp().getTime(); //server time
Log.e("Time from local: ", " " + new Date(localTime));
Log.e("Time from server: ", " " + new Date(serverTime));
}catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
try
and catch
statement doesnt work in this code and i'm set all internet
and network
permissions in manifest