最近APP需要做一项登录鉴权的功能,涉及到数据上报。
数据上报就涉及到时间,如果时间不对,服务器可能会拒绝你的数据;就算不拒绝,你上报的数据也是不正确的,此时就会报错。(我的是 服务端返回错误码:415)
获取本地时间会因用户的设定而造成错乱,因此需要应用去获取远程服务器时间。如果你没有自己的服务器开展业务,也可以使用互联网公司提供的时间API接口。
我结合网上的各种案例,重新编写了一份逻辑。希望能对正在浏览本篇文章的你,提供一些帮助。
我这里直接访问的是苏宁购物网站的时间地址:http://quan.suning.com/getSysTime.do
可以看到接口提供的时间格式,如下:
{ "sysTime2": "2019-04-19 10:23:04", "sysTime1": "20190419102304" }
一. 添加网络请求权限
<uses-permission android:name="android.permission.INTERNET" />
二. 写一个网络请求的类,用于获取服务器网络数据
import android.util.Log;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class HttpServerTime {
private static final String TAG = "HttpServerTime";
/**
* 直接访问 http://quan.suning.com/getSysTime.do 可以看到接口提供的时间格式:
*
* {
* "sysTime2": "2019-04-19 10:23:04",
* "sysTime1": "20190419102304"
* }
*
* 里面有两个时间数据 sysTime2 和 sysTime1,都是常见的时间格式,
* 但我们在计算和存储都是以时间戳的形式,所以获取到时间后会对其进行转换。
*/
public HttpServerTime() {
}
public String makeServiceCall(String reqUrl) {
String response = null;
try {
URL url = new URL(reqUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
// read the response
InputStream in = new BufferedInputStream(conn.getInputStream());
response = convertStreamToString(in);
} catch (MalformedURLException e) {
Log.e(TAG, "MalformedURLException: " + e.getMessage());
} catch (ProtocolException e) {
Log.e(TAG, "ProtocolException: " + e.getMessage());
} catch (IOException e) {
Log.e(TAG, "IOException: " + e.getMessage());
} catch (Exception e) {
Log.e(TAG, "Exception: " + e.getMessage());
}
return response;
}
private String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line;
try {
while ((line = reader.readLine()) != null) {
sb.append(line).append('\n');
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
public static final String getServerTimestamp() {
final HttpServerTime httpServerTime = new HttpServerTime();
final String jsonStr = httpServerTime.makeServiceCall("http://quan.suning.com/getSysTime.do");
if (jsonStr != null) {
try {
final JSONObject json = new JSONObject(jsonStr);
if (json.has("sysTime2")) {
final String date = json.getString("sysTime2");
return dateToTimestamp(date);
}
} catch (JSONException e) {
// e.printStackTrace();
}
}
// 未能获取远程时间,将获取本地时间,根据需要修改.
return new Date().getTime() + "";
}
public static final String dateToTimestamp(String time) {
final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
final Date date;
try {
date = dateFormat.parse(time);
} catch (ParseException e) {
return "0";
}
return date.getTime() + "";
}
}
三. 调用示例
//最终获取的时间值为Long类型的时间戳
String timestamp = HttpServerTime.getServerTimestamp();