2

i have a url given to me to used for syncing the server time to my application, Question is how to do that? anyone can explain to me what to do here thank i really appreciate it.

here's the URL = http://server10.instaforex.com:2012/TimeService/TimeService.svc/CurrentTime

here is my mainactivity code.

public class MainActivity extends Activity {

    Timer timeoutTimer;
    final Random myRandom = new Random();
    GenerateTask genTask = new GenerateTask();
    static String RAN_TEXT = "text";

    class GenerateTask extends TimerTask {
        boolean started = false;
        public void run() {
            if (started) {
                System.out.println("generating");
                final TextView textGenerateNumber = (TextView)findViewById(R.id.generatenumber);
                RAN_TEXT = "";

                for(int k=0;k<7;k++){
                    RAN_TEXT += myRandom.nextInt(10) + " ";
                }
                runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        textGenerateNumber.setText(RAN_TEXT);
                    }
                });
            }
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button buttonGenerate = (Button)findViewById(R.id.generateme);

        buttonGenerate.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {
                System.out.println("click");
                if (!genTask.started) {
                    genTask.started=true;
                    timeoutTimer = new Timer();
                    timeoutTimer.scheduleAtFixedRate(genTask, 0, 30000);
                } else {
                    genTask.started=false;
                    timeoutTimer.cancel();
                }
            }
       });  
    }
}
jun
  • 35
  • 2
  • 12

2 Answers2

1

Try this..

First try to get the source of that html link using either one of solutions from the links of SO solution 1 or solution 2

Second you can parse its content using parsers.

Third Display the parsed content as required time format.

Let me know if you face any difficulty..

Community
  • 1
  • 1
Adarsh H S
  • 1,208
  • 6
  • 21
  • 42
  • 1
    The link returns a XML. Why would you load it into a webview and then extract it when you can parse the contents directly ? The answer you linked to does that - http://stackoverflow.com/questions/2376471/how-do-i-get-the-web-page-contents-from-a-webview?answertab=votes# – Deepak Bala Apr 04 '13 at 13:01
  • @deepak and Adarsh can you please show me how to do this base on my code above?? – jun Apr 05 '13 at 02:11
1

It is unclear what your main activity is up to from the code you posted. It just concatenates some random numbers and sets them in a text area.

If you're looking to parse XML output returned by a HTTP client, there are many examples out there that show you how to do this. Take a look at this question - Android: parse XML from string problems

If NTP time is what you are really interested in, there are NTP clients for android that can help you sycn time correctly.

Use one of those solutions and you're good to go.

Community
  • 1
  • 1
Deepak Bala
  • 11,095
  • 2
  • 38
  • 49
  • my bad i didnt explain my code, so here i'll explain, and you are correct actually it's a password generator code once button is click it will generate random 7 digits for every 30 secs. I want to sync the time of my application to the URL they gave me, as you can see the application has its own time so i want it to sync to their server. can you please give me and example base on my code above?? for future reference also – jun Apr 05 '13 at 10:40