细节修复、部分bug修复
This commit is contained in:
@ -36,7 +36,7 @@ import java.util.Map;
|
||||
public class TokenUtils {
|
||||
|
||||
// 注册设备
|
||||
public static synchronized void register(final Context context) {
|
||||
public static synchronized String register(final Context context) {
|
||||
HashMap<String, String> params = new HashMap<>();
|
||||
String android_id = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
|
||||
if (!TextUtils.isEmpty(android_id)) {
|
||||
@ -50,31 +50,63 @@ public class TokenUtils {
|
||||
String mac = wm.getConnectionInfo().getMacAddress();
|
||||
if (!TextUtils.isEmpty(mac)) {
|
||||
params.put("MAC", mac);
|
||||
} else {
|
||||
SharedPreferences sp = context.getSharedPreferences(Config.PREFERENCE, Context.MODE_PRIVATE);
|
||||
sp.edit().putBoolean("isUploadMac", false).apply();
|
||||
}
|
||||
String mid = StatConfig.getMid(context);
|
||||
if (!TextUtils.isEmpty(mid) || !"0".equals(mid)) {
|
||||
params.put("MTA_ID", mid);
|
||||
} else {
|
||||
SharedPreferences sp = context.getSharedPreferences(Config.PREFERENCE, Context.MODE_PRIVATE);
|
||||
sp.edit().putBoolean("isUploadMid", false).apply();
|
||||
}
|
||||
JsonObjectExtendedRequest request = new JsonObjectExtendedRequest(Request.Method.POST,
|
||||
"http://user.ghzhushou.com/v1d0/device/register", new JSONObject(params).toString(),
|
||||
new Response.Listener<JSONObject>() {
|
||||
@Override
|
||||
public void onResponse(JSONObject response) {
|
||||
Utils.log("onResponse = " + response);
|
||||
try {
|
||||
// 保存device_id
|
||||
saveDeviceId(context, response.getString("device_id"));
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}, new Response.ErrorListener() {
|
||||
@Override
|
||||
public void onErrorResponse(VolleyError error) {
|
||||
Utils.log("onErrorResponse = " + error);
|
||||
}
|
||||
});
|
||||
AppController.addToRequestQueue(request, TokenUtils.class);
|
||||
String url = "http://user.ghzhushou.com/v1d0/device/register";
|
||||
HttpURLConnection connection = null;
|
||||
try {
|
||||
JSONObject body = new JSONObject(params);
|
||||
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 response = new JSONObject(builder.toString());
|
||||
String device_id = response.getString("device_id");
|
||||
// 保存device_id
|
||||
saveDeviceId(context, device_id);
|
||||
Utils.log("device_id = " + device_id);
|
||||
return device_id;
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
// 获取用户token
|
||||
|
||||
Reference in New Issue
Block a user