解决token 401问题
This commit is contained in:
@ -9,7 +9,11 @@ import android.provider.Settings;
|
||||
import android.telephony.TelephonyManager;
|
||||
import android.text.TextUtils;
|
||||
|
||||
import com.android.volley.Request;
|
||||
import com.android.volley.Response;
|
||||
import com.gh.base.AppController;
|
||||
import com.gh.common.constant.Config;
|
||||
import com.gh.gamecenter.volley.extended.StringExtendedRequest;
|
||||
import com.tencent.stat.StatConfig;
|
||||
|
||||
import org.json.JSONException;
|
||||
@ -66,7 +70,6 @@ public class TokenUtils {
|
||||
connection.setRequestMethod("POST");
|
||||
|
||||
connection.addRequestProperty("Content-Type", "application/json; charset=utf-8");
|
||||
// connection.addRequestProperty("Device", DeviceUtils.getDeviceHeader(context));
|
||||
|
||||
connection.connect();
|
||||
|
||||
@ -103,19 +106,24 @@ public class TokenUtils {
|
||||
return null;
|
||||
}
|
||||
|
||||
// 获取用户token
|
||||
public static synchronized String getToken(Context context) {
|
||||
//TODO 时间准确判断
|
||||
SharedPreferences sp = context.getSharedPreferences(Config.PREFERENCE,
|
||||
Context.MODE_PRIVATE);
|
||||
return getToken(context, true);
|
||||
}
|
||||
|
||||
// 获取用户token
|
||||
public static synchronized String getToken(Context context, boolean isCheck) {
|
||||
|
||||
SharedPreferences sp = context.getSharedPreferences(Config.PREFERENCE, Context.MODE_PRIVATE);
|
||||
String token = sp.getString("token", null);
|
||||
if (token != null) {
|
||||
long expire = sp.getLong("token_expire", 0) * 1000 - 10 * 1000;
|
||||
long time = System.currentTimeMillis();//TODO 用户有可能修改手机时间,导致没能判断Token是否过期
|
||||
// 判断token是否过期
|
||||
if (time < expire) {
|
||||
// token未过期
|
||||
return token;
|
||||
if (isCheck) {
|
||||
if (token != null) {
|
||||
long expire = sp.getLong("token_expire", 0) * 1000 - 10 * 1000;
|
||||
long time = Utils.getTime(context);
|
||||
// 判断token是否过期
|
||||
if (time < expire) {
|
||||
// token未过期
|
||||
return token;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -132,7 +140,6 @@ public class TokenUtils {
|
||||
connection.setRequestMethod("POST");
|
||||
|
||||
connection.addRequestProperty("Content-Type", "application/json; charset=utf-8");
|
||||
// connection.addRequestProperty("Device", DeviceUtils.getDeviceHeader(context));
|
||||
|
||||
connection.connect();
|
||||
|
||||
@ -160,6 +167,10 @@ public class TokenUtils {
|
||||
editor.putString("token", jsonObject.getString("value"));
|
||||
editor.putLong("token_expire", jsonObject.getLong("expire"));
|
||||
editor.apply();
|
||||
if (token != null && token.equals(jsonObject.getString("value"))) {
|
||||
// 服务器返回的token和本地已存的token相同,更新本地时间
|
||||
getTime(context);
|
||||
}
|
||||
return jsonObject.getString("value");
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
@ -171,12 +182,36 @@ public class TokenUtils {
|
||||
return null;
|
||||
}
|
||||
|
||||
// 获取服务器时间
|
||||
public static synchronized void getTime(Context context) {
|
||||
final SharedPreferences sp = context.getSharedPreferences(Config.PREFERENCE, Context.MODE_PRIVATE);
|
||||
StringExtendedRequest request = new StringExtendedRequest(
|
||||
Request.Method.GET, Config.HOST + "support/time/current",
|
||||
new Response.Listener<String>() {
|
||||
@Override
|
||||
public void onResponse(String response) {
|
||||
if (response.matches("^[0-9]{10}$")) {
|
||||
try {
|
||||
Editor editor = sp.edit();
|
||||
editor.putLong("server_time", Long.parseLong(response));
|
||||
editor.putLong("client_time", System.currentTimeMillis() / 1000);
|
||||
editor.apply();
|
||||
} catch (NumberFormatException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}, null);
|
||||
request.setShouldCache(false);
|
||||
AppController.addToRequestQueue(request);
|
||||
}
|
||||
|
||||
public static synchronized void saveDeviceId(Context context, String device_id){
|
||||
saveSharedPreferences(context, device_id);
|
||||
saveDataFile(context, device_id);
|
||||
svaeSDCard(device_id, "/gh-uuid");//SDCard根目录
|
||||
svaeSDCard(device_id, "/system");//SDCard system目录
|
||||
svaeSDCard(device_id, "/data");//SDCard data目录
|
||||
svaeSDCard(device_id, "/gh-uuid");// SDCard根目录
|
||||
svaeSDCard(device_id, "/system"); // SDCard system目录
|
||||
svaeSDCard(device_id, "/data"); // SDCard data目录
|
||||
}
|
||||
|
||||
//将uuid存到sp
|
||||
|
||||
Reference in New Issue
Block a user