我每次点击按钮都需要花费一些时间来进行验证,以解决手动更改移动时间的问题。我需要一个从服务器获取日期并向我返回服务器日期的函数。
我尝试过了...
public static Date getCurrentDate() {
final Date[] date = {null};
RequestParams params = new RequestParams();
HttpRestClient.postData(ApiManager.USER_TEAM_LIST, params, new AsyncHttpResponseHandler() {
@Override
public void onStart() {
super.onStart();
}
@Override
public void onFinish() {
super.onFinish();
}
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
String s = new String(responseBody);
Log.e(TAG, "onSuccess: " + s);
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
date[0] = format.parse(s);
} catch (ParseException e) {
e.printStackTrace();
}
}
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
Log.e(TAG, "onFailure: getTeam" );
/*Toast.makeText(mContext
, "Server Error...", Toast.LENGTH_SHORT).show();*/
}
});
return date[0];
}
答案 0 :(得分:0)
我刚刚找到了解决方案
//if your thread not accept than add this
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitNetwork().build();
StrictMode.setThreadPolicy(policy);
static InetAddress inetAddress = null;
static TimeInfo timeInfo = null;
String TIME_SERVER = "time.google.com";
NTPUDPClient timeClient = new NTPUDPClient();
inetAddress = InetAddress.getByName(TIME_SERVER);
timeClient.open();
timeInfo = timeClient.getTime(inetAddress);
long returnTime = 0 ;
if (timeInfo != null) {
returnTime = timeInfo.getMessage().getTransmitTimeStamp().getTime();
}