private void getFromCloud() { try { groups = new ArrayList<>(); AVUser user = AVUser.getCurrentUser(); JSONArray array = user.getJSONArray("groups"); if (array != null) { for (int i = 0; i < array.length(); i++) { JSONObject object = array.getJSONObject(i); WrapGroup wrapGroup = new WrapGroup(); Group group = new Group(); group.setWidth(236); group.setHeight(354); group.setImageurl(object.getString("imageurl")); group.setTitle(object.getString("title")); group.setUrl(object.getString("url")); wrapGroup.setGroup(group); wrapGroup.setIscollected(true); wrapGroup.setDate(new Date().getTime()); wrapGroup.setGroupid(object.getString("groupid")); groups.add(wrapGroup); } saveDb(groups); mAdapter.replaceWith(groups); showSnackBar("云端同步成功"); } } catch (JSONException e) { e.printStackTrace(); KLog.e(e); } }
private void saveToCloud() { try { JSONArray array = new JSONArray(); for (WrapGroup group : groups) { JSONObject object = new JSONObject(); object.put("groupid", group.getGroupid()); object.put("title", group.getGroup().getTitle()); object.put("imageurl", group.getGroup().getImageurl()); object.put("url", group.getGroup().getUrl()); array.put(object); } AVUser user = AVUser.getCurrentUser(); user.put("groups", array); user.saveInBackground( new SaveCallback() { @Override public void done(AVException e) { if (e == null) { showSnackBar("云端同步完成"); } } }); } catch (JSONException e) { e.printStackTrace(); KLog.e(e); } }
@Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setTitle("我的收藏"); groups = WrapGroup.all(realm); KLog.e(groups.size()); mAdapter = new CollectedAdapter(this) { @Override protected void onItemClick(View v, int position) { startGroupActivity(v, position); } }; mRecyclerView.setAdapter(mAdapter); if (groups.size() > 0) { // 本地有数据才去上传 mAdapter.replaceWith(groups); if (mHasUser) { // 已经登录 saveToCloud(); } else { showSnackBar("可以登录进行云端同步啦"); } } else if (mHasUser) { // 本地没有 并且已经登录 getFromCloud(); } else { showSnackBar("可以登录进行云端同步啦"); } }
public void onError(Platform platform, int action, Throwable t) { dismissDialog(); KLog.json(t.getMessage()); if (action == Platform.ACTION_USER_INFOR) { handler.sendEmptyMessage(MSG_AUTH_ERROR); } t.printStackTrace(); }
/** * 取消注册事件 * * @param tag 标签 * @param observable 被观察者 */ public void unregister(@NonNull Object tag, @NonNull Observable observable) { final List<Subject> subjectList = subjectMapper.get(tag); if (null != subjectList) { subjectList.remove((Subject) observable); if (subjectList.isEmpty()) { // 集合数据为0的时候移map除掉tag subjectMapper.remove(tag); } } KLog.e("{unregister}subjectMapper: " + subjectMapper); }
/** * 注册事件 * * @param tag 标签 * @param clazz 类 * @param <T> 类型 * @return 被观察者 */ public <T> Observable<T> register(@NonNull Object tag, @NonNull Class<T> clazz) { List<Subject> subjectList = subjectMapper.get(tag); if (null == subjectList) { subjectList = new ArrayList<>(); subjectMapper.put(tag, subjectList); } Subject<T, T> subject; subjectList.add(subject = PublishSubject.create()); KLog.e("{register}subjectMapper: " + subjectMapper); return subject; }
/** * 包装无父评论的请求参数 * * @return */ public static String getRequestURLNoParent( String post_id, String name, String email, String content) { // 方法1 转义 // content= // MessageFormat.format("%40%3Ca+href%3D%27%23comment-{0}%27%3E{1}%3C%2Fa%3E%3A+{2}",parent_id,parent_name, content); // 方法2 URLEncoder(更优) try { name = URLEncoder.encode(name, "utf-8"); content = URLEncoder.encode(content, "utf-8"); } catch (Exception ex) { KLog.e("URLEncoder error"); } return MessageFormat.format( "{0}&post_id={1}&content={2}&email={3}&name={4}", Comment4FreshNews.URL_PUSH_COMMENT, post_id, content, email, name); }