如何建立评论模型?

时间:2019-03-18 08:50:24

标签: android

Hello Web API为我提供了以下JSON。供评论。

{
  "comment_count": 9,
  "comments": [
    {
      "comment_ID": "2",
      "comment_post_ID": "167",
      "comment_author": "admin",
      "comment_author_email": "xxxx@gmail.com",
      "comment_author_url": "",
      "comment_author_IP": "::1",
      "comment_date": "2019-01-21 10:45:59",
      "comment_date_gmt": "2019-01-21 02:45:59",
      "comment_content": "asdada asda sda sd asdsada sd as",
      "comment_karma": "0",
      "comment_approved": "1",
      "comment_agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64)",
      "comment_type": "",
      "comment_parent": "0",
      "user_id": "1",
      "like_cnt": "1",
      "image": "",
      "author_image": "52263886_2292810744295258_5904172631346642944_n-150x150.jpg",
      "is_liked": true
    },
    {
      "comment_ID": "3",
      "comment_post_ID": "167",
      "comment_author": "admin",
      "comment_author_email": "xxxx@gmail.com",
      "comment_author_url": "",
      "comment_author_IP": "::1",
      "comment_date": "2019-01-21 11:12:37",
      "comment_date_gmt": "2019-01-21 03:12:37",
      "comment_content": "a",
      "comment_karma": "0",
      "comment_approved": "1",
      "comment_agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64)",
      "comment_type": "",
      "comment_parent": "0",
      "user_id": "1",
      "like_cnt": "0",
      "image": "",
      "author_image": "52263886_2292810744295258_5904172631346642944_n-150x150.jpg",
      "is_liked": false
    }
  ]
}

评论回复级别不受限制。它可以是多个级别。但我想像Facebook评论部分那样做。意思是:

A: comment - level 0
B: comment - level 0
   C: reply of comment B - level 1
      D: reply of comment C - level 2 

评论D级为2。D评论是C评论的回复。 我想在D注释中标记C注释的作者姓名。因为我不想添加级别2。

但是如何将数据存储在适配器中?如您所见,没有层次。 到目前为止,我写的是:/ Edited /

  for(int i = 0; i < result.getComments().size(); i++){
                    Comment comment = result.getComments().get(i);
                    if(comment.getComment_parent().equals("0")) continue;
                    String parentId = comment.getComment_parent();
                    boolean parentFound = false;
                    boolean rootFound = false;

                    while(!parentFound || !rootFound){

                        int parentPosition = binarySearch(result.getComments(),0, result.getComments().size(), parentId);
                        if(parentPosition == -1) break;
                        Comment temp = result.getComments().get(parentPosition);

                        if(temp.getComment_ID().equals(parentId)){
                            if (!parentFound){
                                parentFound = true;
                                comment.setTag(temp.getComment_author());
                            }

                            if (temp.getComment_parent().equals("0")) {
                                rootFound = true;
                                comment.setRootId(temp.getComment_ID());
                                temp.addChildComment(comment);
                            } else {
                                parentId = temp.getComment_parent();
                            }
                        }
                    }
                }


                Iterator itr = result.getComments().iterator();
                while (itr.hasNext()) {
                    Comment comment = (Comment) itr.next();
                    if(!comment.getComment_parent().equals("0")){
                        itr.remove();
                    }
                }

private int binarySearch(List<Comment> arr, int l, int r, String x)
    {
        if (r>=l)
        {
        int mid = l + (r - l)/2;
        if (arr.get(mid).getComment_ID().equals(x))
            return mid;

        if (Integer.valueOf(arr.get(mid).getComment_ID()) > Integer.valueOf(x))
            return binarySearch(arr, l, mid-1, x);

        return binarySearch(arr, mid+1, r, x);
    }
    return -1;
}

公共类评论{

private String comment_ID;
private String comment_post_ID;
private String comment_author;
private String comment_author_email;
private String comment_author_url;
private String comment_author_IP;
private String comment_date;
private String comment_date_gmt;
private String comment_content;
private String comment_karma;
private String comment_approved;
private String comment_agent;
private String comment_type;
private String comment_parent;
private String user_id;
private boolean isSend = true;
private List<Comment> commentList;

private String rootId;
private String tag;

3 个答案:

答案 0 :(得分:2)

不要手动创建Model类,我建议您使用ROBOPOJO Generator来创建Model / POJO类。它将为您生成All模型类,您不需要自己创建它,这也会有所帮助您将来在创建模型类时。它只需要JSON字符串,然后单击即可完成您的工作

答案 1 :(得分:0)

您应该创建2个类-一个用于Retrofit来解析WebAPI响应的类,另一个是Comment实体本身。
以下是改造响应的类:

public class Comments{
    @SerializedName("comment_count")
    private int commentCount; //this could be ignored
    private List<Comment> comments;
}

@SerializedName("comment_count")告诉将JSON键改装为commentCount字段,没有此注释,您必须声明与JSON键同名的字段,例如private int comment_count;
评论实体本身:

public class Comment {

    private String comment_ID;
    private String comment_post_ID;
    private String comment_author;
    private String comment_author_email;
    private String comment_author_url;
    private String comment_author_IP;
    private String comment_date;
    private String comment_date_gmt;
    private String comment_content;
    private String comment_karma;
    private String comment_approved;
    private String comment_agent;
    private String comment_type;
    private String comment_parent;
    private String user_id;
    private boolean isSend = true;
}

您不必自己解析响应,让Retrofit来完成此工作,您将获得List<Comment>,可用于自由填充适配器或将该数组存储到DB中。

下面显示了如何使用翻新获得响应。您在代码中的某个地方调用了方法

private void requestComments() {
    ApiManager.getCommentsAdapter().getComments(new IApiCallBackSuccess<Comments>() {
        @Override
        public void onApiSuccess(Comments response) {
            onGotComments(response); // do there whatever you like
        }
    }, this);
}

在ApiManager中:

public final class ApiManager {
    public static CommentsApiAdapter getCommentsAdapter() {
        return new CommentsApiAdapter();
    }
}

CommentsApiAdapter:

public class CommentsApiAdapter extends MyApiAdapter<CommentsApiService> {

    @Override
    Class<CommentsApiService> provideServiceClass() {
        return CommentsApiService.class;
    }

    public void getComments(final IApiCallBackSuccess<Comments> callBackSuccess,
                             final IApiCallBackError callBackError) {
        getService().getComments().enqueue();
    }
}

MyApiAdapter,ApiUrlHelper.BASE_URL-只是带有基本URL的静态最终字符串,例如“ www.mywebapi.com”

public abstract class MyApiAdapter<ApiService> extends BaseApiAdapter<ApiService> {

    @Override
    String getBaseUrl() {
        return ApiUrlHelper.BASE_URL;
    }
}

CommentsApiService(代表翻新服务)

public interface CommentsApiService {

    @GET("/api/comments")
    Call<Comments> getComments();
}

BaseApiAdapter

abstract class BaseApiAdapter<ApiService> {

    private final int TIME_OUT = ApiUrlHelper.TIME_OUT;
    private final ApiService mApiService;
    private final Gson mGson;

    BaseApiAdapter() {
        final OkHttpClient.Builder okClientBuilder = new OkHttpClient.Builder()
                .connectTimeout(TIME_OUT, TimeUnit.MILLISECONDS)
                .readTimeout(TIME_OUT, TimeUnit.MILLISECONDS)
                .writeTimeout(TIME_OUT, TimeUnit.MILLISECONDS);


        mGson = gsonBuilder.create();

        final Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(getBaseUrl())
                .addConverterFactory(GsonConverterFactory.create(mGson))
                .client(okClientBuilder.build())
                .build();
        mApiService = retrofit.create(provideServiceClass());
    }

    abstract Class<ApiService> provideServiceClass();
}

答案 2 :(得分:0)

只需使用jsonschema2pojo转换器工具即可获得普通的旧Java对象。根据API响应,这将为您提供一个非常有效的数据对象,您可以使用应用程序中所需的任何逻辑对其进行进一步扩展。