首页 > 代码库 > 获取网络时间失败,求大神指点

获取网络时间失败,求大神指点

private void GainTime() {
new Thread(new Runnable() {
@Override
public void run() {
try {
URL url=new URL("http://www.taobao.com");
URLConnection uc=url.openConnection();
uc.connect();
long id=uc.getDate();
Date date=new Date(id);
SimpleDateFormat formats=new SimpleDateFormat("网络时间:"+"HHmmss", Locale.CHINA);
nowTime = formats.format(date);
Log.i("时间", date.getHours() + "" + date.getMinutes() + ""
+ date.getSeconds() + "" + "\n" + nowTime);

}catch (Exception e){
e.printStackTrace();
}
handler.sendEmptyMessage(0);
}

}).start();

handler=new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);

time_texts.setText(nowTime);
if (nowTime==null||nowTime.equals("")){
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
time_texts.setText(sdf.format(new Date()) + "(" + "系统时间" + ")");
}
}

};


}
public void onClick(View view){
GainTime();
}

获取网络时间失败,求大神指点