Cómo obtener la fecha y la hora del servidor?

Rishav Raina:

Quiero aprovechar la hora del servidor o cualquier API porque cuando el cambio de usuario que datan del móvil. Él / ella puede obtener esa recompensa días cambiando la fecha. ¿Alguien puede decirle cómo implementar la hora del servidor. Por lo tanto, si el cambio de usuario que la fecha móvil no obtuvo la recompensa fecha.

import com.google.firebase.database.FirebaseDatabase;

import java.util.Calendar;

public class DailyCheckins extends AppCompatActivity {

    boolean showedToday = false;
    private TextView coins2;
    private Calendar calendar;
    private int weekday;
    private SharedPreferences coins;
    private ImageButton sun, mon, tue, wed, thu, fri, sat;
    private String todayString;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_daily_checkins);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        coins = getSharedPreferences("Rewards", MODE_PRIVATE);

        calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        weekday = calendar.get(Calendar.DAY_OF_WEEK);
        todayString = year + "" + month + "" + day;

        sun = (ImageButton) findViewById(R.id.imgSun);
        mon = (ImageButton) findViewById(R.id.imgMon);
        tue = (ImageButton) findViewById(R.id.imgTue);
        wed = (ImageButton) findViewById(R.id.imgWed);
        thu = (ImageButton) findViewById(R.id.imgThu);
        fri = (ImageButton) findViewById(R.id.imgFri);
        sat = (ImageButton) findViewById(R.id.imgSat);

        sun.setEnabled(false);
        sun.setAlpha(.5f);
        mon.setEnabled(false);
        mon.setAlpha(.5f);
        tue.setEnabled(false);
        tue.setAlpha(.5f);
        wed.setEnabled(false);
        wed.setAlpha(.5f);
        thu.setEnabled(false);
        thu.setAlpha(.5f);
        fri.setEnabled(false);
        fri.setAlpha(.5f);
        sat.setEnabled(false);
        sat.setAlpha(.5f);

        if (weekday==1){
            sun.setEnabled(true);
            sun.setAlpha(1f);
        }
        else if (weekday==2){
            mon.setEnabled(true);
            mon.setAlpha(1f);
        }
        else if (weekday==3){
            tue.setEnabled(true);
            tue.setAlpha(1f);
        }
        else if (weekday==4){
            wed.setEnabled(true);
            wed.setAlpha(1f);
        }
        else if (weekday==5){
            thu.setEnabled(true);
            thu.setAlpha(1f);
        }
        else if (weekday==6){
            fri.setEnabled(true);
            fri.setAlpha(1f);
        }
        else if (weekday==7){
            sat.setEnabled(true);
            sat.setAlpha(1f);
        }
    }

    public void monCheck(View view) {
        SharedPreferences dailyChecks = getSharedPreferences("DAILYCHECKS", 0);
        boolean currentDay = dailyChecks.getBoolean(todayString, false);

        if (!currentDay){
            Toast.makeText(this, "10 Coins Recieved!", Toast.LENGTH_SHORT).show();
            SharedPreferences.Editor daily = dailyChecks.edit();
            daily.putBoolean(todayString, true);
            daily.apply();
            int coinCount = Integer.parseInt(coins.getString("Coins", "0"));
            coinCount = coinCount + 10;
            SharedPreferences.Editor coinsEdit = coins.edit();
            coinsEdit.putString("Coins", String.valueOf(coinCount));
            coinsEdit.apply();
            FirebaseDatabase database = FirebaseDatabase.getInstance();
            DatabaseReference myRef = database.getReference("History");
            myRef.push().setValue("Monday Daily Checkin - (+10 Coins)");
        }
        else {
            Toast.makeText(this, "Reward already recieved", Toast.LENGTH_SHORT).show();
        }
    }

    public void tueCheck(View view) {
        SharedPreferences dailyChecks = getSharedPreferences("DAILYCHECKS", 0);
        boolean currentDay = dailyChecks.getBoolean(todayString, false);

        if (!currentDay){
            Toast.makeText(this, "10 Coins Recieved!", Toast.LENGTH_SHORT).show();
            SharedPreferences.Editor daily = dailyChecks.edit();
            daily.putBoolean(todayString, true);
            daily.apply();
            int coinCount = Integer.parseInt(coins.getString("Coins", "0"));
            coinCount = coinCount + 10;
            SharedPreferences.Editor coinsEdit = coins.edit();
            coinsEdit.putString("Coins", String.valueOf(coinCount));
            coinsEdit.apply();
            FirebaseDatabase database = FirebaseDatabase.getInstance();
            DatabaseReference myRef = database.getReference("History");
            myRef.push().setValue("Tuesday Daily Checkin - (+10 Coins)");
        }
        else {
            Toast.makeText(this, "Reward already recieved", Toast.LENGTH_SHORT).show();
        }
    }

    public void wedCheck(View view) {
        SharedPreferences dailyChecks = getSharedPreferences("DAILYCHECKS", 0);
        boolean currentDay = dailyChecks.getBoolean(todayString, false);

        if (!currentDay){
            Toast.makeText(this, "20 Coins Recieved!", Toast.LENGTH_SHORT).show();
            SharedPreferences.Editor daily = dailyChecks.edit();
            daily.putBoolean(todayString, true);
            daily.apply();
            int coinCount = Integer.parseInt(coins.getString("Coins", "0"));
            coinCount = coinCount + 20;
            SharedPreferences.Editor coinsEdit = coins.edit();
            coinsEdit.putString("Coins", String.valueOf(coinCount));
            coinsEdit.apply();
            FirebaseDatabase database = FirebaseDatabase.getInstance();
            DatabaseReference myRef = database.getReference("History");
            myRef.push().setValue("Wednesday Daily Checkin - (+20 Coins)");
        }
        else {
            Toast.makeText(this, "Reward already recieved", Toast.LENGTH_SHORT).show();
        }
    }

    public void thuCheck(View view) {
        SharedPreferences dailyChecks = getSharedPreferences("DAILYCHECKS", 0);
        boolean currentDay = dailyChecks.getBoolean(todayString, false);

        if (!currentDay){
            Toast.makeText(this, "20 Coins Recieved!", Toast.LENGTH_SHORT).show();
            SharedPreferences.Editor daily = dailyChecks.edit();
            daily.putBoolean(todayString, true);
            daily.apply();
            int coinCount = Integer.parseInt(coins.getString("Coins", "0"));
            coinCount = coinCount + 20;
            SharedPreferences.Editor coinsEdit = coins.edit();
            coinsEdit.putString("Coins", String.valueOf(coinCount));
            coinsEdit.apply();
            FirebaseDatabase database = FirebaseDatabase.getInstance();
            DatabaseReference myRef = database.getReference("History");
            myRef.push().setValue("Thursday Daily Checkin - (+20 Coins)");
        }
        else {
            Toast.makeText(this, "Reward already recieved", Toast.LENGTH_SHORT).show();
        }
    }

    public void friCheck(View view) {
        SharedPreferences dailyChecks = getSharedPreferences("DAILYCHECKS", 0);
        boolean currentDay = dailyChecks.getBoolean(todayString, false);

        if (!currentDay){
            Toast.makeText(this, "30 Coins Recieved!", Toast.LENGTH_SHORT).show();
            SharedPreferences.Editor daily = dailyChecks.edit();
            daily.putBoolean(todayString, true);
            daily.apply();
            int coinCount = Integer.parseInt(coins.getString("Coins", "0"));
            coinCount = coinCount + 30;
            SharedPreferences.Editor coinsEdit = coins.edit();
            coinsEdit.putString("Coins", String.valueOf(coinCount));
            coinsEdit.apply();
            FirebaseDatabase database = FirebaseDatabase.getInstance();
            DatabaseReference myRef = database.getReference("History");
            myRef.push().setValue("Friday Daily Checkin - (+30 Coins)");
        }
        else {
            Toast.makeText(this, "Reward already recieved", Toast.LENGTH_SHORT).show();
        }
    }

    public void satCheck(View view) {
        SharedPreferences dailyChecks = getSharedPreferences("DAILYCHECKS", 0);
        boolean currentDay = dailyChecks.getBoolean(todayString, false);

        if (!currentDay){
            Toast.makeText(this, "30 Coins Recieved!", Toast.LENGTH_SHORT).show();
            SharedPreferences.Editor daily = dailyChecks.edit();
            daily.putBoolean(todayString, true);
            daily.apply();
            int coinCount = Integer.parseInt(coins.getString("Coins", "0"));
            coinCount = coinCount + 30;
            SharedPreferences.Editor coinsEdit = coins.edit();
            coinsEdit.putString("Coins", String.valueOf(coinCount));
            coinsEdit.apply();
            FirebaseDatabase database = FirebaseDatabase.getInstance();
            DatabaseReference myRef = database.getReference("History");
            myRef.push().setValue("Saturday Daily Checkin - (+30 Coins)");
        }
        else {
            Toast.makeText(this, "Reward already recieved", Toast.LENGTH_SHORT).show();
        }
    }

    public void sunCheck(View view) {
        SharedPreferences dailyChecks = getSharedPreferences("DAILYCHECKS", 0);
        boolean currentDay = dailyChecks.getBoolean(todayString, false);

        if (!currentDay){
            Toast.makeText(this, "50 Coins Recieved!", Toast.LENGTH_SHORT).show();
            SharedPreferences.Editor daily = dailyChecks.edit();
            daily.putBoolean(todayString, true);
            daily.apply();
            int coinCount = Integer.parseInt(coins.getString("Coins", "0"));
            coinCount = coinCount + 50;
            SharedPreferences.Editor coinsEdit = coins.edit();
            coinsEdit.putString("Coins", String.valueOf(coinCount));
            coinsEdit.apply();
            FirebaseDatabase database = FirebaseDatabase.getInstance();
            DatabaseReference myRef = database.getReference("History");
            myRef.push().setValue("Sunday Daily Checkin - (+50 Coins)");
        }
        else {
            Toast.makeText(this, "Reward already recieved", Toast.LENGTH_SHORT).show();
        }
    }
}

alguien me refieren https://timezonedb.com/api pero yo no sabía cómo poner en práctica.

Muntasir Aonik:

Utilizando el HttpGet, Cliente y respuesta, me las arreglo para obtener la hora actual de un servidor de la cabecera de la respuesta de la fecha. Me puede llamar a este todas las veces que quieren y obtener respuestas seguras (Google es casi el 100% disponible y que puedo confiar en conseguir correcta de fecha y hora)

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

Marque este proyecto: Haga clic aquí

Explicación del proyecto:

Añadir esto en el interior de manifiesto:

 <uses-library
            android:name="org.apache.http.legacy"
            android:required="false" />

Añadir esta Biblioteca En Build.gradle

android {
        useLibrary 'org.apache.http.legacy'
    }

Compruebe el archivo Java y copiar todo.

Supongo que te gusta

Origin http://10.200.1.11:23101/article/api/json?id=389714&siteId=1
Recomendado
Clasificación