package com.gh.common.util; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.HttpURLConnection; import java.net.URL; import java.util.HashMap; import java.util.Map; import org.json.JSONException; import org.json.JSONObject; import android.content.Context; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import com.gh.common.constant.Config; public class TokenUtils { // 获取用户token public static synchronized String getToken(Context context) { 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(); // 判断token是否过期 if (time < expire) { return token; } } // Map params = DeviceUtils.getDeviceParams(context); Map params = new HashMap(); String url = Config.HOST + "v1d45/token/visit"; //判断是否已登录 if (sp.getBoolean("isLogin", false)) { // 已登录,获取用户名和密码自动登录 String username = sp.getString("username", null); String password = sp.getString("password", null); if (username == null || password == null) { url = Config.HOST + "v2/token/user?time=" + System.currentTimeMillis(); params.put("username", username); try { String s = RSEUtils.encryptByPublic(password + username + System.currentTimeMillis() / 1000); Utils.log("加密 = " + s); params.put("password", s); } catch (Exception e) { e.printStackTrace(); } } else { url = Config.HOST + "v2/token/mobile?time=" + System.currentTimeMillis(); String mobile = sp.getString("mobile", null); params.put("mobile_number", mobile); } } try { JSONObject body = new JSONObject(params); HttpURLConnection connection = (HttpURLConnection) new URL(url) .openConnection(); connection.setDoInput(true); connection.setDoOutput(true); connection.setConnectTimeout(5000); connection.setRequestMethod("POST"); connection.addRequestProperty("Content-Type", "application/json; charset=utf-8"); connection.addRequestProperty("Device", DeviceUtils.getDeviceHeader(context)); connection.connect(); OutputStreamWriter outputStream = new OutputStreamWriter( connection.getOutputStream(), "utf-8"); outputStream.write(body.toString()); outputStream.flush(); outputStream.close(); if (connection.getResponseCode() == 200) { BufferedReader reader = new BufferedReader( new InputStreamReader(connection.getInputStream())); StringBuilder builder = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { builder.append(line); } reader.close(); try { JSONObject jsonObject = new JSONObject(builder.toString()); Editor editor = sp.edit(); editor.putString("token", jsonObject.getString("token")); editor.putLong("token_expire", jsonObject.getLong("expire")); editor.apply(); return jsonObject.getString("token"); } catch (JSONException e) { e.printStackTrace(); } } } catch (IOException e) { e.printStackTrace(); } return null; } }