Получение серверного времени зависает и вылетает мое приложение - PullRequest
0 голосов
/ 24 сентября 2018

Я делаю приложение, в котором я нажимаю кнопку START и получаю текущее время, а нажатие кнопки STOP возвращает время снова.Я использовал системное время без каких-либо ошибок, недавно я изменил его на серверное время, которое находится в Asynctask, но приложение нестабильно, замедляется и выходит без сообщений об ошибках, но при более быстрых соединениях оно может обрабатываться.Есть идеи почему?Это мой код:

class getDST2 extends AsyncTask<Void, Void, Void> {



    @Override
    protected Void doInBackground(Void... arg0) {

        try {
            TimeTCPClient client = new TimeTCPClient();
            try {
                client.setDefaultTimeout(60000);
                client.connect("time.nist.gov");
                simpledate = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
                do_casu = simpledate.format(client.getDate());
            } finally {
                client.disconnect();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }


        return null;
    }


    protected void onPostExecute(Void result) {
        getDSTdone = true;
    }
}

Также выполняется графический таймер текущего времени с момента нажатия кнопки «Пуск», поэтому мне нужно каждую секунду получать внутри сервера время в обработчике .. код:

    handler.post(r = new Runnable() {
     public void run() {

   hasStartedtt2 = true;
   calendar = Calendar.getInstance();
   simpledate = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");

  new getDST2().execute(); // THIS IS THE ASynctask, returns the "do_casu" String


 zaciatok_hour = zaciatok.substring(11, 13);
 zaciatok_minute = zaciatok.substring(14, 16);
 koniec_hour = do_casu.substring(11, 13);
 koniec_minute = do_casu.substring(14, 16);
 zaciatok_sekundy = zaciatok.substring(17, 19);
 koniec_sekundy = do_casu.substring(17, 19);


 final_hour = ((Integer.parseInt(koniec_hour) - Integer.parseInt(zaciatok_hour)));
 final_minute = Integer.parseInt(koniec_minute) - Integer.parseInt(zaciatok_minute);
final_seconds = Integer.parseInt(koniec_sekundy) - Integer.parseInt(zaciatok_sekundy) - 1;

           }
           });

Обработчик вызывается каждую секунду.

1 Ответ

0 голосов
/ 24 сентября 2018
 ServerTimeThread sth = new ServerTimeThread();
 sth.start();
 from_time = simpledate.format(sth.time);

когда вы вызываете 'sth.time', поток только начинается и все еще выполняется.

'time' остается неинициализированным, это init в конце потока

Таким образом, при доступе к 'time' оно абсолютно равно нулю.

2 способ AsyncTask

Операция блокировки:

public class NTPDateTask extends AsyncTask<Void,Void,Date> {
@Override
   protected Date doInBackground(Void... voids) {
      Date date=fetchYourDate();
      //fetch your date here
      return date;
   }
}

, затем вызовите

Date result = new NTPDateTask().execute().get();

Операция неблокирования (шаблон обратного вызова):

    public class NTPDateTask extends AsyncTask<Void,Void,Date> {
    @Override
    protected Date doInBackground(Void... voids) {
        Date date = fetchYourDate();
        //fetch your date here
        return date;
    }

    @Override
    protected void onPostExecute(Date date) {
        //this is 'callback'
        //do the thing you want when task finish
        //onPostExecute is called when doInBackground finished,and it runs on UIThread
    }
}

, затем

new NTPDateTask().execute();

РЕДАКТИРОВАНИЕ:

class TCPTimeDisplayWorker implements Runnable {

static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");

boolean isActive = true;
private Handler targetHandler;

public TCPTimeDisplayWorker(Handler targetHandler) {
    //pass the handler ref here
    this.targetHandler = targetHandler;
}

@Override
public void run() {
    while (isActive) {
        long startTime = System.currentTimeMillis();

        Date date = fetchDateFromTCPClient();
        //fetch Server Date here

        String currentDateText = simpleDateFormat.format(date);
        targetHandler.sendMessage(Message.obtain(targetHandler, 0, currentDateText));
        long endTime = System.currentTimeMillis();
        long lapse = endTime - startTime;
        if (lapse < 1000) {
            try {
                Thread.sleep(1000 - lapse);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
  }
}

Обработчик:

 // Non-static inner class will hold outer-class reference,may risk in memory leak
static class MainHandler extends Handler {
    private WeakReference<TextView> textViewWeakReference;
    // declare as WeakRef to avoid memory leak

    public MainHandler(Looper looper, WeakReference<TextView> textViewWeakReference) {
        super(looper);
        this.textViewWeakReference = textViewWeakReference;
    }

    @Override
    public void handleMessage(Message msg) {
        if (textViewWeakReference.get() != null) {
            //handle the message from message queue here
            String text = (String) msg.obj;
            textViewWeakReference.get().setText(text);
        }
    }
}

затем

    // must use the same handler to send msg from Background thread and 
    // handle at Main Thread
    // a handler create on a thread will bound to that thread


    mainHandler = new MainHandler(Looper.getMainLooper(), new WeakReference<>(mTextViewSystemTime));
    new Thread(new TCPTimeDisplayWorker(mainHandler)).start();

кстати, CamelCase - это общее соглашение об именах в Java.

Надеюсь, что это полезно.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...