0
import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
import org.apache.commons.net.time.*;
import java.io.*;
import java.util.*;

public class MainActivity extends Activity
{
  /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    try {
        TimeTCPClient client = new TimeTCPClient();
        try {
            // Set timeout of 60 seconds
            client.setDefaultTimeout(60000);
            client.connect("nist.time.nosc.us");
            TextView tv= (TextView)findViewById(R.id.Text);
            Date text1 = (client.getDate());
            tv.setText(String.valueOf( text1));
        }
  finally
 {
            client.disconnect();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}
}

I want to check time from internet so user doesn't set wrong time in device and the app get wrong information so checking time from Internet is the way

But when I try this code its crashed.

dfeuer
  • 48,079
  • 5
  • 63
  • 167

1 Answers1

0

Make sure you added the proper permission in AndroidManifest.xml to use the Internet.

<uses-permission android:name="android.permission.INTERNET" /> 

Also next time if you ask the question, check your logcat output and paste it here. Furthermore, clarify what do you mean by "code its crashed". You application stops? Or simply the code does not do what you wanted it to do?

pkuszewski
  • 262
  • 1
  • 12