com.socks.jiandan.model.Commentator Java Examples

The following examples show how to use com.socks.jiandan.model.Commentator. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JDApi.java    From JianDanRxJava with Apache License 2.0 6 votes vote down vote up
public static Observable<ArrayList<Commentator>> getCommentator(final String thread_key, LoadFinishCallBack callBack) {

        return Observable.create(new Observable.OnSubscribe<ArrayList<Commentator>>() {

            @Override
            public void call(Subscriber<? super ArrayList<Commentator>> subscriber) {
                String url = Commentator.getUrlCommentList(thread_key);
                try {
                    subscriber.onNext(new CommentListParser(callBack).parse(OkHttpProxy.get()
                            .url(url)
                            .execute()));
                    subscriber.onCompleted();
                } catch (IOException e) {
                    subscriber.onError(e);
                }
            }
        }).compose(applySchedulers());
    }
 
Example #2
Source File: CommentAdapter.java    From JianDanRxJava with Apache License 2.0 6 votes vote down vote up
public void loadData() {
    Subscription subscriptions = JDApi.getCommentator(mThread_key, obj -> thread_id = (String) obj)
            .subscribe(commentators1 -> {
                if (commentators1.size() == 0) {
                    mLoadResultCallBack.onSuccess(LoadResultCallBack.SUCCESS_NONE, null);
                } else {
                    mCommentators.clear();
                    Commentator.generateCommentator(commentators1, mCommentators);
                    notifyDataSetChanged();
                    mLoadResultCallBack.onSuccess(LoadResultCallBack.SUCCESS_OK, null);
                }
            }, e -> {
                mLoadResultCallBack.onError(LoadResultCallBack.ERROR_NET);
            });
    mActivity.addSubscription(subscriptions);
}
 
Example #3
Source File: CommentAdapter.java    From JianDan_OkHttpWithVolley with Apache License 2.0 5 votes vote down vote up
private List<Commentator> addFloors(Commentator commentator) {
    //只有一层
    if (commentator.getFloorNum() == 1) {
        return null;
    }
    List<String> parentIds = Arrays.asList(commentator.getParents());
    ArrayList<Commentator> commentators = new ArrayList<>();
    for (Commentator comm : this.commentators) {
        if (parentIds.contains(comm.getPost_id())) {
            commentators.add(comm);
        }
    }
    Collections.reverse(commentators);
    return commentators;
}
 
Example #4
Source File: Request4CommentList.java    From JianDan_OkHttp with Apache License 2.0 5 votes vote down vote up
public Request4CommentList(String url, Response
        .Listener<ArrayList<Commentator>> listener,
                           Response.ErrorListener errorListener, LoadFinishCallBack callBack) {
    super(Method.GET, url, errorListener);
    this.listener = listener;
    this.callBack = callBack;
}
 
Example #5
Source File: CommentAdapter.java    From JianDan_OkHttp with Apache License 2.0 5 votes vote down vote up
private List<Commentator> addFloors(Commentator commentator) {
    //只有一层
    if (commentator.getFloorNum() == 1) {
        return null;
    }
    List<String> parentIds = Arrays.asList(commentator.getParents());
    ArrayList<Commentator> commentators = new ArrayList<>();
    for (Commentator comm : this.commentators) {
        if (parentIds.contains(comm.getPost_id())) {
            commentators.add(comm);
        }
    }
    Collections.reverse(commentators);
    return commentators;
}
 
Example #6
Source File: CommentAdapter.java    From JianDan_OkHttp with Apache License 2.0 5 votes vote down vote up
@Override
public CommentViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    switch (viewType) {
        case Commentator.TYPE_HOT:
        case Commentator.TYPE_NEW:
            return new CommentViewHolder(mActivity.getLayoutInflater().inflate(R.layout
                    .item_comment_flag, parent, false));
        case Commentator.TYPE_NORMAL:
            return new CommentViewHolder(mActivity.getLayoutInflater().inflate(R.layout.item_comment, parent,
                    false));
        default:
            return null;
    }
}
 
Example #7
Source File: JDApi.java    From JianDanRxJava with Apache License 2.0 5 votes vote down vote up
public static Observable<Boolean> pushComment4DuoShuo(Map<String, String> params) {

        return Observable.create(new Observable.OnSubscribe<Boolean>() {
            @Override
            public void call(Subscriber<? super Boolean> subscriber) {
                try {
                    subscriber.onNext(new PushCommentParser().parse(OkHttpProxy.post().url(Commentator.URL_PUSH_COMMENT).setParams(params).execute()));
                    subscriber.onCompleted();
                } catch (IOException e) {
                    e.printStackTrace();
                    subscriber.onError(e);
                }
            }
        }).compose(applySchedulers());
    }
 
Example #8
Source File: CommentAdapter.java    From JianDanRxJava with Apache License 2.0 5 votes vote down vote up
private List<Commentator> addFloors(Commentator commentator) {
    if (commentator.getFloorNum() == 1) {
        return null;
    }
    List<String> parentIds = Arrays.asList(commentator.getParents());
    ArrayList<Commentator> commentators = new ArrayList<>();
    for (Commentator comm : mCommentators) {
        if (parentIds.contains(comm.getPost_id())) {
            commentators.add(comm);
        }
    }
    Collections.reverse(commentators);
    return commentators;
}
 
Example #9
Source File: CommentAdapter.java    From JianDanRxJava with Apache License 2.0 5 votes vote down vote up
@Override
public CommentViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    switch (viewType) {
        case Commentator.TYPE_HOT:
        case Commentator.TYPE_NEW:
            return new CommentViewHolder(mActivity.getLayoutInflater().inflate(R.layout
                    .item_comment_flag, parent, false));
        case Commentator.TYPE_NORMAL:
            return new CommentViewHolder(mActivity.getLayoutInflater().inflate(R.layout.item_comment, parent,
                    false));
        default:
            return null;
    }
}
 
Example #10
Source File: Request4CommentList.java    From JianDan with Apache License 2.0 5 votes vote down vote up
public Request4CommentList(String url, Response
        .Listener<ArrayList<Commentator>> listener,
                           Response.ErrorListener errorListener, LoadFinishCallBack callBack) {
    super(Method.GET, url, errorListener);
    this.listener = listener;
    this.callBack = callBack;
}
 
Example #11
Source File: CommentAdapter.java    From JianDan with Apache License 2.0 5 votes vote down vote up
private List<Commentator> addFloors(Commentator commentator) {
    //只有一层
    if (commentator.getFloorNum() == 1) {
        return null;
    }
    List<String> parentIds = Arrays.asList(commentator.getParents());
    ArrayList<Commentator> commentators = new ArrayList<>();
    for (Commentator comm : this.commentators) {
        if (parentIds.contains(comm.getPost_id())) {
            commentators.add(comm);
        }
    }
    Collections.reverse(commentators);
    return commentators;
}
 
Example #12
Source File: CommentAdapter.java    From JianDan with Apache License 2.0 5 votes vote down vote up
@Override
public CommentViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    switch (viewType) {
        case Commentator.TYPE_HOT:
        case Commentator.TYPE_NEW:
            return new CommentViewHolder(mActivity.getLayoutInflater().inflate(R.layout
                    .item_comment_flag, parent, false));
        case Commentator.TYPE_NORMAL:
            return new CommentViewHolder(mActivity.getLayoutInflater().inflate(R.layout.item_comment, parent,
                    false));
        default:
            return null;
    }
}
 
Example #13
Source File: Request4CommentList.java    From JianDan_OkHttpWithVolley with Apache License 2.0 5 votes vote down vote up
public Request4CommentList(String url, Response
        .Listener<ArrayList<Commentator>> listener,
                           Response.ErrorListener errorListener, LoadFinishCallBack callBack) {
    super(Method.GET, url, errorListener);
    this.listener = listener;
    this.callBack = callBack;
}
 
Example #14
Source File: CommentAdapter.java    From JianDan_OkHttpWithVolley with Apache License 2.0 5 votes vote down vote up
@Override
public CommentViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    switch (viewType) {
        case Commentator.TYPE_HOT:
        case Commentator.TYPE_NEW:
            return new CommentViewHolder(mActivity.getLayoutInflater().inflate(R.layout
                    .item_comment_flag, parent, false));
        case Commentator.TYPE_NORMAL:
            return new CommentViewHolder(mActivity.getLayoutInflater().inflate(R.layout.item_comment, parent,
                    false));
        default:
            return null;
    }
}
 
Example #15
Source File: Request4CommentList.java    From JianDan with Apache License 2.0 4 votes vote down vote up
@Override
protected Response<ArrayList<Commentator>> parseNetworkResponse(NetworkResponse response) {

    try {
        //获取到所有的数据
        String jsonStr = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
        JSONObject resultJson = new JSONObject(jsonStr);
        String allThreadId = resultJson.getString("response").replace("[", "").replace
                ("]", "").replace("\"", "");
        String[] threadIds = allThreadId.split("\\,");

        callBack.loadFinish(resultJson.optJSONObject("thread").optString("thread_id"));

        if (TextUtils.isEmpty(threadIds[0])) {
            return Response.success(new ArrayList<Commentator>(), HttpHeaderParser
                    .parseCacheHeaders(response));
        } else {

            //然后根据thread_id再去获得对应的评论和作者信息
            JSONObject parentPostsJson = resultJson.getJSONObject("parentPosts");
            //找出热门评论
            String hotPosts = resultJson.getString("hotPosts").replace("[", "").replace
                    ("]", "").replace("\"", "");
            String[] allHotPosts = hotPosts.split("\\,");

            ArrayList<Commentator> commentators = new ArrayList<>();
            List<String> allHotPostsArray = Arrays.asList(allHotPosts);

            for (String threadId : threadIds) {
                Commentator commentator = new Commentator();
                JSONObject threadObject = parentPostsJson.getJSONObject(threadId);

                //解析评论,打上TAG
                if (allHotPostsArray.contains(threadId)) {
                    commentator.setTag(Commentator.TAG_HOT);
                } else {
                    commentator.setTag(Commentator.TAG_NORMAL);
                }

                commentator.setPost_id(threadObject.optString("post_id"));
                commentator.setParent_id(threadObject.optString("parent_id"));

                String parentsString = threadObject.optString("parents").replace("[", "").replace
                        ("]", "").replace("\"", "");

                String[] parents = parentsString.split("\\,");
                commentator.setParents(parents);

                //如果第一个数据为空,则只有一层
                if (TextUtil.isNull(parents[0])) {
                    commentator.setFloorNum(1);
                } else {
                    commentator.setFloorNum(parents.length + 1);
                }

                commentator.setMessage(threadObject.optString("message"));
                commentator.setCreated_at(threadObject.optString("created_at"));
                JSONObject authorObject = threadObject.optJSONObject("author");
                commentator.setName(authorObject.optString("name"));
                commentator.setAvatar_url(authorObject.optString("avatar_url"));
                commentator.setType(Commentator.TYPE_NORMAL);
                commentators.add(commentator);
            }

            return Response.success(commentators, HttpHeaderParser.parseCacheHeaders(response));
        }
    } catch (Exception e) {
        e.printStackTrace();
        return Response.error(new ParseError(e));
    }
}
 
Example #16
Source File: Request4CommentList.java    From JianDan with Apache License 2.0 4 votes vote down vote up
@Override
protected void deliverResponse(ArrayList<Commentator> response) {
    listener.onResponse(response);
}
 
Example #17
Source File: CommentAdapter.java    From JianDan with Apache License 2.0 4 votes vote down vote up
public void loadData() {
    RequestManager.addRequest(new Request4CommentList(Commentator.getUrlCommentList(thread_key), new Response
            .Listener<ArrayList<Commentator>>() {
        @Override
        public void onResponse(ArrayList<Commentator> response) {

            if (response.size() == 0) {
                mLoadResultCallBack.onSuccess(LoadResultCallBack.SUCCESS_NONE, null);
            } else {
                commentators.clear();

                ArrayList<Commentator> hotCommentator = new ArrayList<>();
                ArrayList<Commentator> normalComment = new ArrayList<>();

                //添加热门评论
                for (Commentator commentator : response) {
                    if (commentator.getTag().equals(Commentator.TAG_HOT)) {
                        hotCommentator.add(commentator);
                    } else {
                        normalComment.add(commentator);
                    }
                }

                //添加热门评论标签
                if (hotCommentator.size() != 0) {
                    Collections.sort(hotCommentator);
                    Commentator hotCommentFlag = new Commentator();
                    hotCommentFlag.setType(Commentator.TYPE_HOT);
                    hotCommentator.add(0, hotCommentFlag);
                    commentators.addAll(hotCommentator);
                }

                //添加最新评论及标签
                if (normalComment.size() != 0) {
                    Commentator newCommentFlag = new Commentator();
                    newCommentFlag.setType(Commentator.TYPE_NEW);
                    commentators.add(newCommentFlag);
                    Collections.sort(normalComment);
                    commentators.addAll(normalComment);
                }

                notifyDataSetChanged();
                mLoadResultCallBack.onSuccess(LoadResultCallBack.SUCCESS_OK, null);
            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            mLoadResultCallBack.onError(LoadResultCallBack.ERROR_NET, error.getMessage());
        }
    }, new LoadFinishCallBack() {
        @Override
        public void loadFinish(Object obj) {
            thread_id = (String) obj;
        }
    }), mActivity);
}
 
Example #18
Source File: CommentListParser.java    From JianDanRxJava with Apache License 2.0 4 votes vote down vote up
@Nullable
@Override
public ArrayList<Commentator> parse(Response response) {

    if (!response.isSuccessful())
        return null;


    try {
        //获取到所有的数据
        String jsonStr = response.body().string();
        JSONObject resultJson = new JSONObject(jsonStr);
        String allThreadId = resultJson.getString("response").replace("[", "").replace
                ("]", "").replace("\"", "");
        String[] threadIds = allThreadId.split("\\,");

        mCallBack.loadFinish(resultJson.optJSONObject("thread").optString("thread_id"));

        if (TextUtils.isEmpty(threadIds[0])) {
            return new ArrayList<>();
        } else {

            //然后根据thread_id再去获得对应的评论和作者信息
            JSONObject parentPostsJson = resultJson.getJSONObject("parentPosts");
            //找出热门评论
            String hotPosts = resultJson.getString("hotPosts").replace("[", "").replace
                    ("]", "").replace("\"", "");
            String[] allHotPosts = hotPosts.split("\\,");

            ArrayList<Commentator> commentators = new ArrayList<>();
            List<String> allHotPostsArray = Arrays.asList(allHotPosts);

            for (String threadId : threadIds) {
                Commentator commentator = new Commentator();
                JSONObject threadObject = parentPostsJson.getJSONObject(threadId);

                //解析评论,打上TAG
                if (allHotPostsArray.contains(threadId)) {
                    commentator.setTag(Commentator.TAG_HOT);
                } else {
                    commentator.setTag(Commentator.TAG_NORMAL);
                }

                commentator.setPost_id(threadObject.optString("post_id"));
                commentator.setParent_id(threadObject.optString("parent_id"));

                String parentsString = threadObject.optString("parents").replace("[", "").replace
                        ("]", "").replace("\"", "");

                String[] parents = parentsString.split("\\,");
                commentator.setParents(parents);

                //如果第一个数据为空,则只有一层
                if (TextUtil.isNull(parents[0])) {
                    commentator.setFloorNum(1);
                } else {
                    commentator.setFloorNum(parents.length + 1);
                }

                commentator.setMessage(threadObject.optString("message"));
                commentator.setCreated_at(threadObject.optString("created_at"));
                JSONObject authorObject = threadObject.optJSONObject("author");
                commentator.setName(authorObject.optString("name"));
                commentator.setAvatar_url(authorObject.optString("avatar_url"));
                commentator.setType(Commentator.TYPE_NORMAL);
                commentators.add(commentator);
            }

            return commentators;
        }
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}
 
Example #19
Source File: Request4CommentList.java    From JianDan_OkHttpWithVolley with Apache License 2.0 4 votes vote down vote up
@Override
protected void deliverResponse(ArrayList<Commentator> response) {
    listener.onResponse(response);
}
 
Example #20
Source File: Request4CommentList.java    From JianDan_OkHttpWithVolley with Apache License 2.0 4 votes vote down vote up
@Override
protected Response<ArrayList<Commentator>> parseNetworkResponse(NetworkResponse response) {

    try {
        //获取到所有的数据
        String jsonStr = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
        JSONObject resultJson = new JSONObject(jsonStr);
        String allThreadId = resultJson.getString("response").replace("[", "").replace
                ("]", "").replace("\"", "");
        String[] threadIds = allThreadId.split("\\,");

        callBack.loadFinish(resultJson.optJSONObject("thread").optString("thread_id"));

        if (TextUtils.isEmpty(threadIds[0])) {
            return Response.success(new ArrayList<Commentator>(), HttpHeaderParser
                    .parseCacheHeaders(response));
        } else {

            //然后根据thread_id再去获得对应的评论和作者信息
            JSONObject parentPostsJson = resultJson.getJSONObject("parentPosts");
            //找出热门评论
            String hotPosts = resultJson.getString("hotPosts").replace("[", "").replace
                    ("]", "").replace("\"", "");
            String[] allHotPosts = hotPosts.split("\\,");

            ArrayList<Commentator> commentators = new ArrayList<>();
            List<String> allHotPostsArray = Arrays.asList(allHotPosts);

            for (String threadId : threadIds) {
                Commentator commentator = new Commentator();
                JSONObject threadObject = parentPostsJson.getJSONObject(threadId);

                //解析评论,打上TAG
                if (allHotPostsArray.contains(threadId)) {
                    commentator.setTag(Commentator.TAG_HOT);
                } else {
                    commentator.setTag(Commentator.TAG_NORMAL);
                }

                commentator.setPost_id(threadObject.optString("post_id"));
                commentator.setParent_id(threadObject.optString("parent_id"));

                String parentsString = threadObject.optString("parents").replace("[", "").replace
                        ("]", "").replace("\"", "");

                String[] parents = parentsString.split("\\,");
                commentator.setParents(parents);

                //如果第一个数据为空,则只有一层
                if (TextUtil.isNull(parents[0])) {
                    commentator.setFloorNum(1);
                } else {
                    commentator.setFloorNum(parents.length + 1);
                }

                commentator.setMessage(threadObject.optString("message"));
                commentator.setCreated_at(threadObject.optString("created_at"));
                JSONObject authorObject = threadObject.optJSONObject("author");
                commentator.setName(authorObject.optString("name"));
                commentator.setAvatar_url(authorObject.optString("avatar_url"));
                commentator.setType(Commentator.TYPE_NORMAL);
                commentators.add(commentator);
            }

            return Response.success(commentators, HttpHeaderParser.parseCacheHeaders(response));
        }
    } catch (Exception e) {
        e.printStackTrace();
        return Response.error(new ParseError(e));
    }
}
 
Example #21
Source File: CommentListParser.java    From JianDan_OkHttp with Apache License 2.0 4 votes vote down vote up
@Nullable
@Override
public ArrayList<Commentator> parse(Response response) {

    code = wrapperCode(response.code());
    if (!response.isSuccessful())
        return null;


    try {
        //获取到所有的数据
        String jsonStr = response.body().string();
        JSONObject resultJson = new JSONObject(jsonStr);
        String allThreadId = resultJson.getString("response").replace("[", "").replace
                ("]", "").replace("\"", "");
        String[] threadIds = allThreadId.split("\\,");

        callBack.loadFinish(resultJson.optJSONObject("thread").optString("thread_id"));

        if (TextUtils.isEmpty(threadIds[0])) {
            return new ArrayList<>();
        } else {

            //然后根据thread_id再去获得对应的评论和作者信息
            JSONObject parentPostsJson = resultJson.getJSONObject("parentPosts");
            //找出热门评论
            String hotPosts = resultJson.getString("hotPosts").replace("[", "").replace
                    ("]", "").replace("\"", "");
            String[] allHotPosts = hotPosts.split("\\,");

            ArrayList<Commentator> commentators = new ArrayList<>();
            List<String> allHotPostsArray = Arrays.asList(allHotPosts);

            for (String threadId : threadIds) {
                Commentator commentator = new Commentator();
                JSONObject threadObject = parentPostsJson.getJSONObject(threadId);

                //解析评论,打上TAG
                if (allHotPostsArray.contains(threadId)) {
                    commentator.setTag(Commentator.TAG_HOT);
                } else {
                    commentator.setTag(Commentator.TAG_NORMAL);
                }

                commentator.setPost_id(threadObject.optString("post_id"));
                commentator.setParent_id(threadObject.optString("parent_id"));

                String parentsString = threadObject.optString("parents").replace("[", "").replace
                        ("]", "").replace("\"", "");

                String[] parents = parentsString.split("\\,");
                commentator.setParents(parents);

                //如果第一个数据为空,则只有一层
                if (TextUtil.isNull(parents[0])) {
                    commentator.setFloorNum(1);
                } else {
                    commentator.setFloorNum(parents.length + 1);
                }

                commentator.setMessage(threadObject.optString("message"));
                commentator.setCreated_at(threadObject.optString("created_at"));
                JSONObject authorObject = threadObject.optJSONObject("author");
                commentator.setName(authorObject.optString("name"));
                commentator.setAvatar_url(authorObject.optString("avatar_url"));
                commentator.setType(Commentator.TYPE_NORMAL);
                commentators.add(commentator);
            }

            return commentators;
        }
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}
 
Example #22
Source File: CommentAdapter.java    From JianDan_OkHttpWithVolley with Apache License 2.0 4 votes vote down vote up
public void loadData() {
    RequestManager.addRequest(new Request4CommentList(Commentator.getUrlCommentList(thread_key), new Response
            .Listener<ArrayList<Commentator>>() {
        @Override
        public void onResponse(ArrayList<Commentator> response) {

            if (response.size() == 0) {
                mLoadResultCallBack.onSuccess(LoadResultCallBack.SUCCESS_NONE, null);
            } else {
                commentators.clear();

                ArrayList<Commentator> hotCommentator = new ArrayList<>();
                ArrayList<Commentator> normalComment = new ArrayList<>();

                //添加热门评论
                for (Commentator commentator : response) {
                    if (commentator.getTag().equals(Commentator.TAG_HOT)) {
                        hotCommentator.add(commentator);
                    } else {
                        normalComment.add(commentator);
                    }
                }

                //添加热门评论标签
                if (hotCommentator.size() != 0) {
                    Collections.sort(hotCommentator);
                    Commentator hotCommentFlag = new Commentator();
                    hotCommentFlag.setType(Commentator.TYPE_HOT);
                    hotCommentator.add(0, hotCommentFlag);
                    commentators.addAll(hotCommentator);
                }

                //添加最新评论及标签
                if (normalComment.size() != 0) {
                    Commentator newCommentFlag = new Commentator();
                    newCommentFlag.setType(Commentator.TYPE_NEW);
                    commentators.add(newCommentFlag);
                    Collections.sort(normalComment);
                    commentators.addAll(normalComment);
                }

                notifyDataSetChanged();
                mLoadResultCallBack.onSuccess(LoadResultCallBack.SUCCESS_OK, null);
            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            mLoadResultCallBack.onError(LoadResultCallBack.ERROR_NET, error.getMessage());
        }
    }, new LoadFinishCallBack() {
        @Override
        public void loadFinish(Object obj) {
            thread_id = (String) obj;
        }
    }), mActivity);
}
 
Example #23
Source File: Request4CommentList.java    From JianDan_OkHttp with Apache License 2.0 4 votes vote down vote up
@Override
protected Response<ArrayList<Commentator>> parseNetworkResponse(NetworkResponse response) {

    try {
        //获取到所有的数据
        String jsonStr = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
        JSONObject resultJson = new JSONObject(jsonStr);
        String allThreadId = resultJson.getString("response").replace("[", "").replace
                ("]", "").replace("\"", "");
        String[] threadIds = allThreadId.split("\\,");

        callBack.loadFinish(resultJson.optJSONObject("thread").optString("thread_id"));

        if (TextUtils.isEmpty(threadIds[0])) {
            return Response.success(new ArrayList<Commentator>(), HttpHeaderParser
                    .parseCacheHeaders(response));
        } else {

            //然后根据thread_id再去获得对应的评论和作者信息
            JSONObject parentPostsJson = resultJson.getJSONObject("parentPosts");
            //找出热门评论
            String hotPosts = resultJson.getString("hotPosts").replace("[", "").replace
                    ("]", "").replace("\"", "");
            String[] allHotPosts = hotPosts.split("\\,");

            ArrayList<Commentator> commentators = new ArrayList<>();
            List<String> allHotPostsArray = Arrays.asList(allHotPosts);

            for (String threadId : threadIds) {
                Commentator commentator = new Commentator();
                JSONObject threadObject = parentPostsJson.getJSONObject(threadId);

                //解析评论,打上TAG
                if (allHotPostsArray.contains(threadId)) {
                    commentator.setTag(Commentator.TAG_HOT);
                } else {
                    commentator.setTag(Commentator.TAG_NORMAL);
                }

                commentator.setPost_id(threadObject.optString("post_id"));
                commentator.setParent_id(threadObject.optString("parent_id"));

                String parentsString = threadObject.optString("parents").replace("[", "").replace
                        ("]", "").replace("\"", "");

                String[] parents = parentsString.split("\\,");
                commentator.setParents(parents);

                //如果第一个数据为空,则只有一层
                if (TextUtil.isNull(parents[0])) {
                    commentator.setFloorNum(1);
                } else {
                    commentator.setFloorNum(parents.length + 1);
                }

                commentator.setMessage(threadObject.optString("message"));
                commentator.setCreated_at(threadObject.optString("created_at"));
                JSONObject authorObject = threadObject.optJSONObject("author");
                commentator.setName(authorObject.optString("name"));
                commentator.setAvatar_url(authorObject.optString("avatar_url"));
                commentator.setType(Commentator.TYPE_NORMAL);
                commentators.add(commentator);
            }

            return Response.success(commentators, HttpHeaderParser.parseCacheHeaders(response));
        }
    } catch (Exception e) {
        e.printStackTrace();
        return Response.error(new ParseError(e));
    }
}
 
Example #24
Source File: Request4CommentList.java    From JianDan_OkHttp with Apache License 2.0 4 votes vote down vote up
@Override
protected void deliverResponse(ArrayList<Commentator> response) {
    listener.onResponse(response);
}