diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml
index 6d87485fbb..9c51aeb056 100644
--- a/app/src/main/AndroidManifest.xml
+++ b/app/src/main/AndroidManifest.xml
@@ -160,7 +160,7 @@
android:name="com.gh.gamecenter.LibaoActivity"
android:screenOrientation="portrait"/>
());
- }
- }).start();
+ TokenUtils.getToken(context, true)
+ .flatMap(new Func1>() {
+ @Override
+ public Observable call(String token) {
+ JSONArray params = new JSONArray();
+ params.put(gameId);
+ RequestBody body = RequestBody.create(MediaType.parse("application/json"), params.toString());
+ return RetrofitManager.getUser().postConcern(token, body);
+ }
+ }).subscribeOn(Schedulers.io())
+ .observeOn(Schedulers.io())
+ .subscribe();
}
public static void deleteConcernData(final Context context, final String gameId) {
- new Thread(new Runnable() {
- @Override
- public void run() {
- RetrofitManager.getUser().deleteConcern(TokenUtils.getToken(context), gameId)
- .subscribeOn(Schedulers.io())
- .observeOn(AndroidSchedulers.mainThread())
- .subscribe(new Response());
- }
- }).start();
+ TokenUtils.getToken(context, true)
+ .flatMap(new Func1>() {
+ @Override
+ public Observable call(String token) {
+ return RetrofitManager.getUser().deleteConcern(token, gameId);
+ }
+ }).subscribeOn(Schedulers.io())
+ .observeOn(Schedulers.io())
+ .subscribe();
}
public static void updateConcernData(final Context context, final JSONArray data) {
- new Thread(new Runnable() {
- @Override
- public void run() {
- RequestBody body = RequestBody.create(MediaType.parse("application/json"),
- data.toString());
- RetrofitManager.getUser().putConcern(TokenUtils.getToken(context), body)
- .subscribeOn(Schedulers.io())
- .observeOn(AndroidSchedulers.mainThread())
- .subscribe(new Response());
- }
- }).start();
+ TokenUtils.getToken(context, true)
+ .flatMap(new Func1>() {
+ @Override
+ public Observable call(String token) {
+ RequestBody body = RequestBody.create(MediaType.parse("application/json"),
+ data.toString());
+ return RetrofitManager.getUser().putConcern(token, body);
+ }
+ })
+ .subscribeOn(Schedulers.io())
+ .observeOn(Schedulers.io())
+ .subscribe();
}
}
diff --git a/app/src/main/java/com/gh/common/util/LibaoUtils.java b/app/src/main/java/com/gh/common/util/LibaoUtils.java
index bfa8c781fd..1aa7b0e5ae 100644
--- a/app/src/main/java/com/gh/common/util/LibaoUtils.java
+++ b/app/src/main/java/com/gh/common/util/LibaoUtils.java
@@ -28,8 +28,11 @@ import org.json.JSONObject;
import java.util.List;
import de.greenrobot.event.EventBus;
+import okhttp3.ResponseBody;
import retrofit2.adapter.rxjava.HttpException;
+import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
+import rx.functions.Func1;
import rx.schedulers.Schedulers;
/**
@@ -59,120 +62,96 @@ public class LibaoUtils {
}
//初始化存号箱 获取存号箱所有礼包
- public static void getCunHaoXiang(final Context context, final LibaoDao libaoDao) {
- getCunHaoXiang(context, true, libaoDao);
+ public static void getCunHaoXiang(final Context context, final boolean isCheck) {
+ TokenUtils.getToken(context, isCheck)
+ .flatMap(new Func1>>() {
+ @Override
+ public Observable> call(String token) {
+ return RetrofitManager.getLibao().getCunHaoXiang(token);
+ }
+ }).subscribeOn(Schedulers.io())
+ .observeOn(AndroidSchedulers.mainThread())
+ .subscribe(new Response>() {
+ @Override
+ public void onResponse(List response) {
+ LibaoDao libaoDao = new LibaoDao(context);
+ for (LibaoEntity libaoEntity : response) {
+ libaoDao.add(LibaoInfo.createLibaoInfo(libaoEntity));
+ }
+
+ EventBus.getDefault().post(new EBReuse("libaoChanged"));
+ }
+
+ @Override
+ public void onFailure(Throwable e) {
+ if (e instanceof HttpException) {
+ HttpException exception = (HttpException) e;
+ if (exception.code() == 401) {
+ getCunHaoXiang(context, false);
+ }
+ }
+ }
+ });
}
- private static void getCunHaoXiang(final Context context, final boolean isCheck, final LibaoDao libaoDao) {
+ private static void postLibaoLing(final Context context, final String libaoId, final boolean isCheck,
+ final PostLibaoListener listener) {
+ TokenUtils.getToken(context, isCheck)
+ .flatMap(new Func1>() {
+ @Override
+ public Observable call(String token) {
+ return RetrofitManager.getLibao().postLibaoLing(token, libaoId);
+ }
+ }).subscribeOn(Schedulers.io())
+ .observeOn(AndroidSchedulers.mainThread())
+ .subscribe(new JSONObjectResponse() {
+ @Override
+ public void onResponse(JSONObject response) {
+ listener.postSucced(response);
+ }
- new Thread(new Runnable() {
- @Override
- public void run() {
- RetrofitManager.getLibao().getCunHaoXiang(TokenUtils.getToken(context, isCheck))
- .subscribeOn(Schedulers.io())
- .observeOn(AndroidSchedulers.mainThread())
- .subscribe(new Response>(){
- @Override
- public void onResponse(List response) {
- super.onResponse(response);
- for (LibaoEntity libaoEntity : response) {
- libaoDao.add(LibaoInfo.createLibaoInfo(libaoEntity));
- }
-
- EventBus.getDefault().post(new EBReuse("libaoChanged"));
+ @Override
+ public void onFailure(Throwable e) {
+ if (e instanceof HttpException) {
+ HttpException exception = (HttpException) e;
+ if (exception.code() == 401) {
+ postLibaoLing(context, libaoId, false, listener);
+ return;
}
-
- @Override
- public void onFailure(Throwable e) {
- super.onFailure(e);
- if (e instanceof HttpException) {
- HttpException exception = (HttpException) e;
- if (exception.code() == 401) {
- getCunHaoXiang(context, false, libaoDao);
- return;
- }
- }
- }
- });
-
- }
- }).start();
+ }
+ listener.postFailed(e);
+ }
+ });
}
- public static void postLibaoLing(final Context context, final String libaoId, final PostLibaoListener listener) {
- postLibaoLing(context, libaoId, true, listener);
- }
+ private static void postLibaoTao(final Context context, final String libaoId, final boolean isCheck,
+ final PostLibaoListener listener) {
+ TokenUtils.getToken(context, isCheck)
+ .flatMap(new Func1>() {
+ @Override
+ public Observable call(String token) {
+ return RetrofitManager.getLibao().postLibaoTao(token, libaoId);
+ }
+ }).subscribeOn(Schedulers.io())
+ .observeOn(AndroidSchedulers.mainThread())
+ .subscribe(new JSONObjectResponse() {
+ @Override
+ public void onResponse(JSONObject response) {
+ listener.postSucced(response);
+ }
- private static void postLibaoLing(final Context context, final String libaoId, final boolean isCheck
- , final PostLibaoListener listener) {
-
- new Thread(new Runnable() {
- @Override
- public void run() {
- RetrofitManager.getLibao().postLibaoLing(TokenUtils.getToken(context, isCheck), libaoId)
- .subscribeOn(Schedulers.io())
- .observeOn(AndroidSchedulers.mainThread())
- .subscribe(new JSONObjectResponse() {
- @Override
- public void onResponse(JSONObject response) {
- super.onResponse(response);
- listener.postSucced(response);
+ @Override
+ public void onFailure(Throwable e) {
+ if (e instanceof HttpException) {
+ HttpException exception = (HttpException) e;
+ if (exception.code() == 401) {
+ postLibaoLing(context, libaoId, false, listener);
+ return;
}
-
- @Override
- public void onFailure(Throwable e) {
- super.onFailure(e);
- if (e instanceof HttpException) {
- HttpException exception = (HttpException) e;
- if (exception.code() == 401) {
- postLibaoLing(context, libaoId, false, listener);
- return;
- }
- }
- listener.postFailed(e);
- }
- });
-
- }
- }).start();
-
- }
-
- public static void postLibaoTao(final Context context, final String libaoId, final PostLibaoListener listener) {
- postLibaoTao(context, libaoId, true, listener);
- }
-
- private static void postLibaoTao(final Context context, final String libaoId, final boolean isCheck
- , final PostLibaoListener listener) {
-
- new Thread(new Runnable() {
- @Override
- public void run() {
- RetrofitManager.getLibao().postLibaoTao(TokenUtils.getToken(context, isCheck), libaoId)
- .subscribeOn(Schedulers.io())
- .observeOn(AndroidSchedulers.mainThread())
- .subscribe(new JSONObjectResponse(){
- @Override
- public void onResponse(JSONObject response) {
- super.onResponse(response);
- listener.postSucced(response);
- }
-
- @Override
- public void onFailure(Throwable e) {
- super.onFailure(e);
- if (e instanceof HttpException) {
- HttpException exception = (HttpException) e;
- if (exception.code() == 401) {
- postLibaoLing(context, libaoId, false, listener);
- return;
- }
- }
- listener.postFailed(e);
- }
- });
- }
- }).start();
+ }
+ listener.postFailed(e);
+ }
+ });
}
public static void getLibaoStatus(String ids, final PostLibaoListener listener) {
@@ -182,13 +161,11 @@ public class LibaoUtils {
.subscribe(new Response>() {
@Override
public void onResponse(List response) {
- super.onResponse(response);
listener.postSucced(response);
}
@Override
public void onFailure(Throwable e) {
- super.onFailure(e);
listener.postFailed(e);
}
});
@@ -280,7 +257,7 @@ public class LibaoUtils {
libaoBtn.getContext().startActivity(intent);
break;
case "领取":
- postLibaoLing(libaoBtn.getContext(), libaoEntity.getId(), new PostLibaoListener() {
+ postLibaoLing(libaoBtn.getContext(), libaoEntity.getId(), true, new PostLibaoListener() {
@Override
public void postSucced(Object response) {
@@ -345,7 +322,7 @@ public class LibaoUtils {
Utils.toast(libaoBtn.getContext(), "礼包领取时间已结束");
} else if ("fetched".equals(detail)) {
Utils.toast(libaoBtn.getContext(), "你已领过这个礼包了");
- getCunHaoXiang(libaoBtn.getContext(), libaoDao);
+ getCunHaoXiang(libaoBtn.getContext(), true);
libaoBtn.setText("复制");
libaoBtn.setBackgroundResource(R.drawable.textview_blue_style);
} else if ("try tao".equals(detail) || "used up".equals(detail)) {
@@ -369,7 +346,7 @@ public class LibaoUtils {
break;
case "淘号":
- postLibaoTao(libaoBtn.getContext(), libaoEntity.getId(), new PostLibaoListener() {
+ postLibaoTao(libaoBtn.getContext(), libaoEntity.getId(), true, new PostLibaoListener() {
@Override
public void postSucced(Object response) {
@@ -436,7 +413,7 @@ public class LibaoUtils {
Utils.toast(libaoBtn.getContext(), "礼包领取时间已结束");
} else if ("fetched".equals(detail)) {
Utils.toast(libaoBtn.getContext(), "你已领过这个礼包了");
- getCunHaoXiang(libaoBtn.getContext(), libaoDao);
+ getCunHaoXiang(libaoBtn.getContext(), true);
libaoBtn.setText("复制");
libaoBtn.setBackgroundResource(R.drawable.textview_blue_style);
} else if ("try tao".equals(detail) || "used up".equals(detail)) {
diff --git a/app/src/main/java/com/gh/common/util/PostCommentUtils.java b/app/src/main/java/com/gh/common/util/PostCommentUtils.java
index 20ee55e9ff..97e56ef41a 100644
--- a/app/src/main/java/com/gh/common/util/PostCommentUtils.java
+++ b/app/src/main/java/com/gh/common/util/PostCommentUtils.java
@@ -12,7 +12,9 @@ import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.adapter.rxjava.HttpException;
+import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
+import rx.functions.Func1;
import rx.schedulers.Schedulers;
/**
@@ -21,118 +23,110 @@ import rx.schedulers.Schedulers;
*/
public class PostCommentUtils {
- public static void addCommentData(Context context, String newsId, String content, PostCommentListener listener) {
- addCommentData(context, newsId, content, true, listener);
- }
-
public static void addCommentData(final Context context, final String newsId, final String content,
final boolean isCheck, final PostCommentListener listener) {
- new Thread(new Runnable() {
- @Override
- public void run() {
- RequestBody body = RequestBody.create(MediaType.parse("application/json"), content);
- RetrofitManager.getComment().postNewsComment(TokenUtils.getToken(context, isCheck), newsId, body)
- .subscribeOn(Schedulers.io())
- .observeOn(AndroidSchedulers.mainThread())
- .subscribe(new JSONObjectResponse() {
- @Override
- public void onResponse(JSONObject response) {
- if (response.length() != 0) {
- if (listener != null){
- listener.postSucced(response);
- }
- } else {
- Utils.toast(context, "提交失败,请检查网络设置");
- }
- }
-
- @Override
- public void onFailure(Throwable e) {
- if (e instanceof HttpException) {
- HttpException exception = (HttpException) e;
- if (exception.code() == 401) {
- addCommentData(context, newsId, content, false, listener);
- return;
- }
- }
- if (listener != null){
- listener.postFailed(e);
- }
- }
- });
- }
- }).start();
- }
-
- public static void addCommentVoto(final Context context, final String commentId, final PostCommentListener listener) {
- addCommentVoto(context, commentId, true, listener);
- }
-
- public static void addCommentVoto(final Context context, final String commentId, final boolean isCheck
- , final PostCommentListener listener) {
- new Thread(new Runnable() {
- @Override
- public void run() {
- RetrofitManager.getComment().postCommentVote(TokenUtils.getToken(context, isCheck), commentId)
- .subscribeOn(Schedulers.io())
- .observeOn(AndroidSchedulers.mainThread())
- .subscribe(new Response() {
- @Override
- public void onResponse(ResponseBody response) {
- if (listener != null) {
- listener.postSucced(null);
- }
- }
-
- @Override
- public void onFailure(Throwable e) {
- if (e instanceof HttpException) {
- HttpException exception = (HttpException) e;
- if (exception.code() == 401) {
- addCommentVoto(context, commentId, false, listener);
- return;
- }
- }
- if (listener != null) {
- listener.postFailed(e);
- }
- }
- });
- }
- }).start();
- }
-
- public static void addReportData(Context context, String reportData, PostCommentListener listener) {
- addReportData(context, reportData, listener, true);
- }
-
- private static void addReportData(final Context context, final String reportData,
- final PostCommentListener listener, boolean isCheck) {
- RequestBody body = RequestBody.create(MediaType.parse("application/json"), reportData);
- RetrofitManager.getComment().postReportData(body,TokenUtils.getToken(context, isCheck))
+ TokenUtils.getToken(context, isCheck)
+ .flatMap(new Func1>() {
+ @Override
+ public Observable call(String token) {
+ RequestBody body = RequestBody.create(MediaType.parse("application/json"), content);
+ return RetrofitManager.getComment().postNewsComment(token, newsId, body);
+ }
+ })
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
- .subscribe(new Response(){
+ .subscribe(new JSONObjectResponse() {
@Override
- public void onNext(ResponseBody response) {
- super.onNext(response);
+ public void onResponse(JSONObject response) {
+ if (response.length() != 0) {
+ if (listener != null){
+ listener.postSucced(response);
+ }
+ } else {
+ Utils.toast(context, "提交失败,请检查网络设置");
+ }
+ }
+
+ @Override
+ public void onFailure(Throwable e) {
+ if (e instanceof HttpException) {
+ HttpException exception = (HttpException) e;
+ if (exception.code() == 401) {
+ addCommentData(context, newsId, content, false, listener);
+ return;
+ }
+ }
+ if (listener != null){
+ listener.postFailed(e);
+ }
+ }
+ });
+ }
+
+ public static void addCommentVoto(final Context context, final String commentId, final boolean isCheck,
+ final PostCommentListener listener) {
+ TokenUtils.getToken(context, isCheck)
+ .flatMap(new Func1>() {
+ @Override
+ public Observable call(String token) {
+ return RetrofitManager.getComment().postCommentVote(token, commentId);
+ }
+ })
+ .subscribeOn(Schedulers.io())
+ .observeOn(AndroidSchedulers.mainThread())
+ .subscribe(new Response() {
+ @Override
+ public void onResponse(ResponseBody response) {
+ if (listener != null) {
+ listener.postSucced(null);
+ }
+ }
+
+ @Override
+ public void onFailure(Throwable e) {
+ if (e instanceof HttpException) {
+ HttpException exception = (HttpException) e;
+ if (exception.code() == 401) {
+ addCommentVoto(context, commentId, false, listener);
+ return;
+ }
+ }
+ if (listener != null) {
+ listener.postFailed(e);
+ }
+ }
+ });
+ }
+
+ public static void addReportData(final Context context, final String reportData, boolean isCheck,
+ final PostCommentListener listener) {
+ TokenUtils.getToken(context, isCheck)
+ .flatMap(new Func1>() {
+ @Override
+ public Observable call(String token) {
+ RequestBody body = RequestBody.create(MediaType.parse("application/json"), reportData);
+ return RetrofitManager.getComment().postReportData(body, token);
+ }
+ })
+ .subscribeOn(Schedulers.io())
+ .observeOn(AndroidSchedulers.mainThread())
+ .subscribe(new Response() {
+ @Override
+ public void onResponse(ResponseBody response) {
listener.postSucced(null);
}
@Override
- public void onError(Throwable e) {
- super.onError(e);
+ public void onFailure(Throwable e) {
if (e instanceof HttpException) {
if (((HttpException) e).code() == 401) {
- addReportData(context, reportData, listener, false);
+ addReportData(context, reportData, false, listener);
return;
}
}
- Utils.log("addReportData=onError::" + e.toString());
listener.postFailed(e);
}
});
-
}
public interface PostCommentListener {
diff --git a/app/src/main/java/com/gh/common/util/TokenUtils.java b/app/src/main/java/com/gh/common/util/TokenUtils.java
index 74389de811..68362a1004 100644
--- a/app/src/main/java/com/gh/common/util/TokenUtils.java
+++ b/app/src/main/java/com/gh/common/util/TokenUtils.java
@@ -11,6 +11,7 @@ import android.telephony.TelephonyManager;
import android.text.TextUtils;
import com.gh.common.constant.Config;
+import com.gh.gamecenter.retrofit.JSONObjectResponse;
import com.gh.gamecenter.retrofit.Response;
import com.gh.gamecenter.retrofit.RetrofitManager;
import com.gh.gamecenter.retrofit.StringResponse;
@@ -19,30 +20,28 @@ import com.tencent.stat.StatConfig;
import org.json.JSONException;
import org.json.JSONObject;
-import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
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 okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
+import rx.Observable;
+import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
+import rx.functions.Func1;
import rx.schedulers.Schedulers;
public class TokenUtils {
// 注册设备
- public static synchronized String register(final Context context) {
- HashMap params = new HashMap<>();
+ public static synchronized void register(final Context context) {
+ Map params = new HashMap<>();
params.put("MANUFACTURER", Build.MANUFACTURER);
params.put("MODEL", Build.MODEL);
params.put("ANDROID_SDK", String.valueOf(Build.VERSION.SDK_INT));
@@ -70,125 +69,83 @@ public class TokenUtils {
SharedPreferences sp = context.getSharedPreferences(Config.PREFERENCE, Context.MODE_PRIVATE);
sp.edit().putBoolean("isUploadMid", false).apply();
}
- String url = Config.USER_HOST + "device/register";
- 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.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;
- }
-
- public static synchronized String getToken(Context context) {
- return getToken(context, true);
+ RequestBody body = RequestBody.create(MediaType.parse("application/json"), new JSONObject(params).toString());
+ RetrofitManager.getUser().postRegister(body)
+ .subscribeOn(Schedulers.io())
+ .observeOn(Schedulers.io())
+ .subscribe(new JSONObjectResponse() {
+ @Override
+ public void onResponse(JSONObject response) {
+ if (response.length() != 0) {
+ try {
+ // 保存device_id
+ saveDeviceId(context, response.getString("device_id"));
+ Utils.log("device_id = " + response.getString("device_id"));
+ } catch (JSONException e) {
+ e.printStackTrace();
+ }
+ }
+ }
+ });
}
// 获取用户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 (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;
- }
- }
- }
-
- // 重新获取token
- String url = Config.USER_HOST + "login";
- Map params = new HashMap<>();
- params.put("device_id", getDeviceId(context));
- 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.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 {
- Editor editor = sp.edit();
- JSONObject jsonObject = new JSONObject(builder.toString());
- editor.putString("user_name", jsonObject.getString("name"));
- editor.putString("user_icon", jsonObject.getString("icon"));
- jsonObject = jsonObject.getJSONObject("token");
- 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);
+ public static synchronized Observable getToken(final Context context, final boolean isCheck) {
+ return Observable.create(new Observable.OnSubscribe() {
+ @Override
+ public void call(Subscriber super String> subscriber) {
+ String token = null;
+ if (isCheck) {
+ SharedPreferences sp = context.getSharedPreferences(Config.PREFERENCE, Context.MODE_PRIVATE);
+ token = sp.getString("token", null);
+ if (token != null) {
+ long expire = sp.getLong("token_expire", 0) * 1000 - 10 * 1000;
+ long time = Utils.getTime(context);
+ // 判断token是否过期
+ if (time >= expire) {
+ // token已过期
+ token = null;
+ }
}
- return jsonObject.getString("value");
- } catch (JSONException e) {
- e.printStackTrace();
}
+ subscriber.onNext(token);
+ subscriber.onCompleted();
}
- } catch (IOException e) {
- e.printStackTrace();
- }
- return null;
+ }).flatMap(new Func1>() {
+ @Override
+ public Observable call(String token) {
+ if (token == null) {
+ Map params = new HashMap<>();
+ params.put("device_id", getDeviceId(context));
+ return RetrofitManager.getUser()
+ .postLogin(RequestBody.create(MediaType.parse("application/json"), params.toString()))
+ .flatMap(new Func1>() {
+ @Override
+ public Observable call(ResponseBody responseBody) {
+ String value = null;
+ try {
+ SharedPreferences sp = context.getSharedPreferences(Config.PREFERENCE, Context.MODE_PRIVATE);
+ Editor editor = sp.edit();
+ JSONObject response = new JSONObject(responseBody.string());
+ editor.putString("user_name", response.getString("name"));
+ editor.putString("user_icon", response.getString("icon"));
+ response = response.getJSONObject("token");
+ editor.putString("token", response.getString("value"));
+ editor.putLong("token_expire", response.getLong("expire"));
+ editor.apply();
+ // 服务器返回的token和本地已存的token相同,更新本地时间
+ getTime(context);
+ value = response.getString("value");
+ } catch (IOException | JSONException e) {
+ e.printStackTrace();
+ }
+ return Observable.just(value);
+ }
+ });
+ }
+ return Observable.just(token);
+ }
+ });
}
// 检查设备信息是否已经上传完整
@@ -215,27 +172,26 @@ public class TokenUtils {
}
}
if (params.size() != 0) {
- new Thread(){
- @Override
- public void run() {
- RequestBody body = RequestBody.create(MediaType.parse("application/json"),
- new JSONObject(params).toString());
- RetrofitManager.getUser().postDevice(TokenUtils.getToken(context), body,
- TokenUtils.getDeviceId(context))
- .subscribeOn(Schedulers.io())
- .observeOn(AndroidSchedulers.mainThread())
- .subscribe(new Response() {
- @Override
- public void onResponse(ResponseBody response) {
- SharedPreferences.Editor editor = sp.edit();
- editor.putBoolean("isUploadExtra", true);
- editor.putBoolean("isUploadMac", true);
- editor.putBoolean("isUploadMid", true);
- editor.apply();
- }
- });
- }
- }.start();
+ TokenUtils.getToken(context, true)
+ .flatMap(new Func1>() {
+ @Override
+ public Observable call(String token) {
+ RequestBody body = RequestBody.create(MediaType.parse("application/json"),
+ new JSONObject(params).toString());
+ return RetrofitManager.getUser().postDevice(token, body, TokenUtils.getDeviceId(context));
+ }
+ }).subscribeOn(Schedulers.io())
+ .observeOn(AndroidSchedulers.mainThread())
+ .subscribe(new Response() {
+ @Override
+ public void onResponse(ResponseBody response) {
+ SharedPreferences.Editor editor = sp.edit();
+ editor.putBoolean("isUploadExtra", true);
+ editor.putBoolean("isUploadMac", true);
+ editor.putBoolean("isUploadMid", true);
+ editor.apply();
+ }
+ });
}
}
diff --git a/app/src/main/java/com/gh/gamecenter/CropImageActivity.java b/app/src/main/java/com/gh/gamecenter/CropImageActivity.java
index 41cdd6cffd..ac12f56696 100644
--- a/app/src/main/java/com/gh/gamecenter/CropImageActivity.java
+++ b/app/src/main/java/com/gh/gamecenter/CropImageActivity.java
@@ -29,6 +29,12 @@ import java.io.File;
import java.lang.ref.SoftReference;
import java.net.HttpURLConnection;
+import rx.Observable;
+import rx.Subscriber;
+import rx.functions.Action1;
+import rx.functions.Func1;
+import rx.schedulers.Schedulers;
+
public class CropImageActivity extends BaseActivity {
private CropImageCustom cropimage_custom;
@@ -67,51 +73,85 @@ public class CropImageActivity extends BaseActivity {
confirm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
- final Dialog dialog = DialogUtils.showWaitDialog(
- CropImageActivity.this, "上传中...");
- new Thread(new Runnable() {
+ final Dialog dialog = DialogUtils.showWaitDialog(CropImageActivity.this, "上传中...");
+ final String path = getCacheDir() + File.separator + System.currentTimeMillis() + ".jpg";
+ Observable.create(new Observable.OnSubscribe() {
@Override
- public void run() {
- String path = getCacheDir() + File.separator
- + System.currentTimeMillis() + ".jpg";
- if (cropimage_custom.savePicture(path)) {
- // 上传图片
- JSONObject result = FileUtils.uploadFile(Config.USER_HOST + "icon",
- path, TokenUtils.getToken(CropImageActivity.this));
+ public void call(Subscriber super Boolean> subscriber) {
+ subscriber.onNext(cropimage_custom.savePicture(path));
+ subscriber.onCompleted();
+ }
+ }).flatMap(new Func1>() {
+ @Override
+ public Observable call(Boolean status) {
+ if (status == null || !status) {
+ dialog.dismiss();
+ handler.sendEmptyMessage(1);
+ return null;
+ }
+ return TokenUtils.getToken(CropImageActivity.this, true);
+ }
+ }).flatMap(new Func1>() {
+ @Override
+ public Observable call(String token) {
+ if (token != null) {
+ return Observable.just(FileUtils.uploadFile(Config.USER_HOST + "icon", path, token));
+ }
+ dialog.dismiss();
+ handler.sendEmptyMessage(1);
+ return null;
+ }
+ }).flatMap(new Func1>() {
+ @Override
+ public Observable call(JSONObject result) {
+ if (result != null) {
try {
- if (result != null && result.getInt("statusCode") == 401) {
- result = FileUtils.uploadFile(Config.USER_HOST + "icon",
- path, TokenUtils.getToken(CropImageActivity.this, false));
- }
- if (result != null) {
- try {
- int statusCode = result.getInt("statusCode");
- if (statusCode == HttpURLConnection.HTTP_OK) {
- Intent data = new Intent();
- data.putExtra("url", result.getString("icon"));
- setResult(RESULT_OK, data);
- finish();
- handler.sendEmptyMessage(0);
- } else if (statusCode == HttpURLConnection.HTTP_FORBIDDEN
- && "too frequent".equals(result.getString("detail"))) {
- handler.sendEmptyMessage(2);
- }
- } catch (JSONException e) {
- e.printStackTrace();
- }
- } else {
- handler.sendEmptyMessage(1);
+ if (result.getInt("statusCode") == 401) {
+ return TokenUtils.getToken(CropImageActivity.this, false)
+ .flatMap(new Func1>() {
+ @Override
+ public Observable call(String token) {
+ return Observable.just(FileUtils.uploadFile(Config.USER_HOST + "icon", path, token));
+ }
+ });
+ }
+ } catch (JSONException e) {
+ e.printStackTrace();
+ }
+ return Observable.just(result);
+ }
+ dialog.dismiss();
+ handler.sendEmptyMessage(1);
+ return null;
+ }
+ })
+ .subscribeOn(Schedulers.io())
+ .observeOn(Schedulers.io())
+ .subscribe(new Action1() {
+ @Override
+ public void call(JSONObject result) {
+ if (result != null) {
+ try {
+ int statusCode = result.getInt("statusCode");
+ if (statusCode == HttpURLConnection.HTTP_OK) {
+ Intent data = new Intent();
+ data.putExtra("url", result.getString("icon"));
+ setResult(RESULT_OK, data);
+ finish();
+ handler.sendEmptyMessage(0);
+ } else if (statusCode == HttpURLConnection.HTTP_FORBIDDEN
+ && "too frequent".equals(result.getString("detail"))) {
+ handler.sendEmptyMessage(2);
}
} catch (JSONException e) {
e.printStackTrace();
}
- dialog.dismiss();
} else {
- dialog.dismiss();
handler.sendEmptyMessage(1);
}
+ dialog.dismiss();
}
- }).start();
+ });
}
});
RelativeLayout.LayoutParams rparams = new RelativeLayout.LayoutParams(
diff --git a/app/src/main/java/com/gh/gamecenter/MainActivity.java b/app/src/main/java/com/gh/gamecenter/MainActivity.java
index 707a559eb2..aa789ef164 100644
--- a/app/src/main/java/com/gh/gamecenter/MainActivity.java
+++ b/app/src/main/java/com/gh/gamecenter/MainActivity.java
@@ -44,7 +44,6 @@ import com.gh.download.DataWatcher;
import com.gh.download.DownloadEntity;
import com.gh.download.DownloadManager;
import com.gh.download.DownloadStatus;
-import com.gh.gamecenter.db.LibaoDao;
import com.gh.gamecenter.db.info.ConcernInfo;
import com.gh.gamecenter.db.info.GameInfo;
import com.gh.gamecenter.entity.ApkEntity;
@@ -91,6 +90,7 @@ import okhttp3.ResponseBody;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
+import rx.functions.Func1;
import rx.schedulers.Schedulers;
/**
@@ -392,7 +392,7 @@ public class MainActivity extends BaseFragmentActivity implements OnClickListene
TokenUtils.getToken(MainActivity.this, false);
TokenUtils.checkDeviceInfo(MainActivity.this);
initConcern(); // 初始化关注
- LibaoUtils.getCunHaoXiang(MainActivity.this, new LibaoDao(MainActivity.this));
+ LibaoUtils.getCunHaoXiang(MainActivity.this, true);
}
}.start();
}
@@ -417,31 +417,30 @@ public class MainActivity extends BaseFragmentActivity implements OnClickListene
final String TD_CHANNEL_ID = (String) PackageUtils.getMetaData(MainActivity.this, getPackageName(), "TD_CHANNEL_ID");
if ((TextUtils.isEmpty(version_code) || TextUtils.isEmpty(version_name) || TextUtils.isEmpty(channel))
|| (!version_code.equals(versionCode) || !version_name.equals(versionName) || !channel.equals(TD_CHANNEL_ID))) {
- new Thread(){
- @Override
- public void run() {
- Map params = new ArrayMap<>();
- params.put("version_code", versionCode);
- params.put("version_name", versionName);
- params.put("channel", TD_CHANNEL_ID);
-
- RequestBody body = RequestBody.create(MediaType.parse("application/json"),
- new JSONObject(params).toString());
- RetrofitManager.getUser().postGhzs(TokenUtils.getToken(MainActivity.this), body)
- .subscribeOn(Schedulers.io())
- .observeOn(AndroidSchedulers.mainThread())
- .subscribe(new Response() {
- @Override
- public void onResponse(ResponseBody response) {
- SharedPreferences.Editor editor = sp.edit();
- editor.putString("version_code", versionCode);
- editor.putString("version_name", versionName);
- editor.putString("channel", TD_CHANNEL_ID);
- editor.apply();
- }
- });
- }
- }.start();
+ TokenUtils.getToken(MainActivity.this, true)
+ .flatMap(new Func1>() {
+ @Override
+ public Observable call(String token) {
+ Map params = new ArrayMap<>();
+ params.put("version_code", versionCode);
+ params.put("version_name", versionName);
+ params.put("channel", TD_CHANNEL_ID);
+ RequestBody body = RequestBody.create(MediaType.parse("application/json"),
+ new JSONObject(params).toString());
+ return RetrofitManager.getUser().postGhzs(token, body);
+ }
+ }).subscribeOn(Schedulers.io())
+ .observeOn(Schedulers.io())
+ .subscribe(new Response() {
+ @Override
+ public void onResponse(ResponseBody response) {
+ SharedPreferences.Editor editor = sp.edit();
+ editor.putString("version_code", versionCode);
+ editor.putString("version_name", versionName);
+ editor.putString("channel", TD_CHANNEL_ID);
+ editor.apply();
+ }
+ });
}
}
@@ -455,14 +454,19 @@ public class MainActivity extends BaseFragmentActivity implements OnClickListene
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Response>() {
@Override
- public void onResponse(List response) {
+ public void onResponse(final List response) {
if (response.size() != 0) {
- RequestBody body = RequestBody.create(MediaType.parse("application/json"),
- new JSONArray(response).toString());
- RetrofitManager.getUser().putConcern(TokenUtils.getToken(MainActivity.this), body)
- .subscribeOn(Schedulers.io())
- .observeOn(AndroidSchedulers.mainThread())
- .subscribe(new Response());
+ TokenUtils.getToken(MainActivity.this, true)
+ .flatMap(new Func1>() {
+ @Override
+ public Observable call(String token) {
+ RequestBody body = RequestBody.create(MediaType.parse("application/json"),
+ new JSONArray(response).toString());
+ return RetrofitManager.getUser().putConcern(token, body);
+ }
+ }).subscribeOn(Schedulers.io())
+ .observeOn(Schedulers.io())
+ .subscribe();
}
sp.edit().putBoolean("isSwitchConcern", true).apply();
getConcernDigest(response);
@@ -472,9 +476,14 @@ public class MainActivity extends BaseFragmentActivity implements OnClickListene
new Thread(){
@Override
public void run() {
- RetrofitManager.getUser().getConcern(TokenUtils.getToken(MainActivity.this))
- .subscribeOn(Schedulers.io())
- .observeOn(AndroidSchedulers.mainThread())
+ TokenUtils.getToken(MainActivity.this, true)
+ .flatMap(new Func1>>() {
+ @Override
+ public Observable> call(String token) {
+ return RetrofitManager.getUser().getConcern(token);
+ }
+ }).subscribeOn(Schedulers.io())
+ .observeOn(Schedulers.io())
.subscribe(new Response>() {
@Override
public void onResponse(List response) {
@@ -699,17 +708,17 @@ public class MainActivity extends BaseFragmentActivity implements OnClickListene
}
// 更新用户已安装游戏
- new Thread(){
- @Override
- public void run() {
- RequestBody body = RequestBody.create(MediaType.parse("application/json"),
- new JSONArray(installed).toString());
- RetrofitManager.getUser().putPackage(TokenUtils.getToken(MainActivity.this), body)
- .subscribeOn(Schedulers.io())
- .observeOn(AndroidSchedulers.mainThread())
- .subscribe(new Response());
- }
- }.start();
+ TokenUtils.getToken(MainActivity.this, true)
+ .flatMap(new Func1>() {
+ @Override
+ public Observable call(String token) {
+ RequestBody body = RequestBody.create(MediaType.parse("application/json"),
+ new JSONArray(installed).toString());
+ return RetrofitManager.getUser().putPackage(token, body);
+ }
+ }).subscribeOn(Schedulers.io())
+ .observeOn(Schedulers.io())
+ .subscribe();
handler.postDelayed(new Runnable() {
@Override
@@ -1166,33 +1175,34 @@ public class MainActivity extends BaseFragmentActivity implements OnClickListene
}
}
// 更新已安装游戏
- new Thread(){
- @Override
- public void run() {
- JSONArray params = new JSONArray();
- params.put(packageName);
- RequestBody body = RequestBody.create(MediaType.parse("application/json"), params.toString());
- RetrofitManager.getUser().postPackage(TokenUtils.getToken(MainActivity.this), body)
- .subscribeOn(Schedulers.io())
- .observeOn(AndroidSchedulers.mainThread())
- .subscribe(new Response());
- }
- }.start();
+ TokenUtils.getToken(MainActivity.this, true)
+ .flatMap(new Func1>() {
+ @Override
+ public Observable call(String token) {
+ JSONArray params = new JSONArray();
+ params.put(packageName);
+ RequestBody body = RequestBody.create(
+ MediaType.parse("application/json"), params.toString());
+ return RetrofitManager.getUser().postPackage(token, body);
+ }
+ }).subscribeOn(Schedulers.io())
+ .observeOn(Schedulers.io())
+ .subscribe();
}
});
}
if ("卸载".equals(busFour.getType())) {
// 更新已安装游戏
- new Thread(){
- @Override
- public void run() {
- RetrofitManager.getUser().deletePackage(TokenUtils.getToken(MainActivity.this), packageName)
- .subscribeOn(Schedulers.io())
- .observeOn(AndroidSchedulers.mainThread())
- .subscribe(new Response());
- }
- }.start();
+ TokenUtils.getToken(MainActivity.this, true)
+ .flatMap(new Func1>() {
+ @Override
+ public Observable call(String token) {
+ return RetrofitManager.getUser().deletePackage(token, packageName);
+ }
+ }).subscribeOn(Schedulers.io())
+ .observeOn(Schedulers.io())
+ .subscribe();
}
DataCollectionUtils.uploadInorunstall(this, busFour.getType(), busFour.getPackageName());
diff --git a/app/src/main/java/com/gh/gamecenter/MessageDetailActivity.java b/app/src/main/java/com/gh/gamecenter/MessageDetailActivity.java
index fb034771d9..6478365ea6 100644
--- a/app/src/main/java/com/gh/gamecenter/MessageDetailActivity.java
+++ b/app/src/main/java/com/gh/gamecenter/MessageDetailActivity.java
@@ -150,7 +150,7 @@ public class MessageDetailActivity extends BaseActivity implements MessageDetail
new Thread(new Runnable() {
@Override
public void run() {
- TokenUtils.getToken(MessageDetailActivity.this);
+ TokenUtils.getToken(MessageDetailActivity.this, true);
MessageDetailActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
@@ -318,7 +318,7 @@ public class MessageDetailActivity extends BaseActivity implements MessageDetail
newsId = mConcernEntity.getId();
}
- PostCommentUtils.addCommentData(MessageDetailActivity.this, newsId, jsonObject.toString(),
+ PostCommentUtils.addCommentData(MessageDetailActivity.this, newsId, jsonObject.toString(), true,
new PostCommentUtils.PostCommentListener() {
@Override
public void postSucced(JSONObject response) {
diff --git a/app/src/main/java/com/gh/gamecenter/adapter/MessageDetailAdapter.java b/app/src/main/java/com/gh/gamecenter/adapter/MessageDetailAdapter.java
index b6fc1ad8dd..d73a53ddf5 100644
--- a/app/src/main/java/com/gh/gamecenter/adapter/MessageDetailAdapter.java
+++ b/app/src/main/java/com/gh/gamecenter/adapter/MessageDetailAdapter.java
@@ -572,7 +572,8 @@ public class MessageDetailAdapter extends RecyclerView.Adapter statusList = (List) response;
- LibaoDao libaoDao = new LibaoDao(context);
-
- for (LibaoInfo libaoInfo : libaoDao.getAll()) {
- for (int i = 0; i < statusList.size(); i++) {
- if (libaoInfo.getLibaoId().equals(statusList.get(i).getId())) {
- if ("ling".equals(libaoInfo.getStatus())) {
- statusList.get(i).setStatus("linged");
- } else {
- statusList.get(i).setStatus("taoed");
+ RetrofitManager.getLibao().getLibaoStatus(ids)
+ .map(new Func1, Object>() {
+ @Override
+ public Object call(List list) {
+ LibaoDao libaoDao = new LibaoDao(context);
+ for (LibaoInfo libaoInfo : libaoDao.getAll()) {
+ for (int i = 0; i < list.size(); i++) {
+ if (libaoInfo.getLibaoId().equals(list.get(i).getId())) {
+ if ("ling".equals(libaoInfo.getStatus())) {
+ list.get(i).setStatus("linged");
+ } else {
+ list.get(i).setStatus("taoed");
+ }
+ }
}
}
- }
- }
-
- for (int i = 0; i < libaoList.size(); i++) {
- LibaoEntity libaoEntity = libaoList.get(i);
- for (LibaoStatusEntity libaoStatusEntity : statusList) {
- if (libaoEntity.getId().equals(libaoStatusEntity.getId())) {
- libaoEntity.setStatus(libaoStatusEntity.getStatus());
- libaoEntity.setAvailable(libaoStatusEntity.getAvailable());
- libaoEntity.setTotal(libaoStatusEntity.getTotal());
- }
- if ("finish".equals(libaoEntity.getStatus())) {
- libaoList.remove(i);
- i--;
- break;
+ for (int i = 0; i < libaoList.size(); i++) {
+ LibaoEntity libaoEntity = libaoList.get(i);
+ for (LibaoStatusEntity libaoStatusEntity : list) {
+ if ("finish".equals(libaoEntity.getStatus())) {
+ libaoList.remove(i);
+ i--;
+ break;
+ }
+ if (libaoEntity.getId().equals(libaoStatusEntity.getId())) {
+ libaoEntity.setStatus(libaoStatusEntity.getStatus());
+ libaoEntity.setAvailable(libaoStatusEntity.getAvailable());
+ libaoEntity.setTotal(libaoStatusEntity.getTotal());
+ }
+ }
}
+ initPosition();
+ return null;
}
- }
-
- notifyDataSetChanged();
- initPosition();
- }
-
- @Override
- public void postFailed(Throwable error) {
-
- }
- });
+ })
+ .subscribeOn(Schedulers.io())
+ .observeOn(AndroidSchedulers.mainThread())
+ .subscribe(new Action1