Wednesday, 9 May 2018

Get Online Time from Google Server




public static void getTimeFromGoogleServerNoAsync(final Context context) {


    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("Google Time "+dateStr);
            PreferencesHelper preferencesHelper = new PreferencesHelper(context);
            preferencesHelper.setGoogleTime(dateStr);

            SimpleDateFormat sdf3 = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z", Locale.ENGLISH);
            Date d1 = null;
            try {
                d1 = sdf3.parse(dateStr);
            } catch (Exception e) {
                e.printStackTrace();
            }

            System.out.println("OTP Google Time" + d1);
            Calendar calendar = Calendar.getInstance();
            System.out.println("OTP Device Time" + calendar.getTime());

        } 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());
    }

}



No comments:

Post a Comment

Run/install/debug Android applications over Wi-Fi ?

Open Teminal/cmd --------------- Below steps is for Android 10 or lower Step 1 - Connect the device via USB and make sure debugging is work...