简体   繁体   English

将数组的 POST 请求写入 JSON 和 Retrofit 2 Android

[英]POST request of an array into JSON with Retrofit 2 Android

I am a begginer in android. I want to add a comment into a post, I tried many solutions but none worked.我是android的初学者。我想在帖子中添加评论,我尝试了很多解决方案但都没有用。 When I test the API in postman it works but I don't know how to make it in android.当我在 postman 中测试 API 时,它可以工作,但我不知道如何在 android 中进行测试。

This is my Entity in nodejs这是我在 nodejs 中的实体

    _id: mongoose.Schema.Types.ObjectId,
    text: String,
    likes: { type: Number, default: 0 },
    date: Date,
    image: String,
    idUser: String,
    comments: [{
        text: String,
        date: Date,
        idUser: String,
    }],

This is the API这是API

router.post('/comments/:idPost', (req, res, next) => {
    const id = req.params.idPost;
    Post.updateOne({ _id: id }, {
        $addToSet: {
            comments: [{
                text: req.body.comments.text,
                date: Date.now(),
                idUser: req.body.comments.idUser
            }],
        }
    }, function (err, result) {
        if (err) {
            res.send(err);
        } else {
            res.send(result);
        }
    });
});

This is my Entity in Java这是我在 Java 中的实体

    @SerializedName("_id")
    private String idPost;
    @SerializedName("text")
    private String text;
    @SerializedName("likes")
    private int likes;
    @SerializedName("date")
    private Date date;
    @SerializedName("image")
    private String image;
    @SerializedName("idUser")
    private String idUser;
    @SerializedName("comments")
    private ArrayList<Comment> CommentArrayList;

    public static class Comment {
        @SerializedName("_id")
        private String idComment;
        @SerializedName("text")
        private String text;
        @SerializedName("date")
        private Date date;
        @SerializedName("idUser")
        private String idUser; }
}

This is the request I made这是我提出的要求

@POST("/posts/comments/{idPost}")
    Call<ResponseBody> addComment(@Path("idPost") String idPost, @Body JSONObject comment);

I can't understand your question well.我不能很好地理解你的问题。 I can explain how to send data into node JS using retrofit2我可以解释如何使用 retrofit2 将数据发送到节点 JS

In android, add this line in the Retrofit interface android,在Retrofit界面添加这一行

@POST("/user/phone/verify")
Call<Void> addNewComment(@Body HashMap<String, String> map);

And add your details about comment here:并在此处添加有关评论的详细信息:

HashMap<String,String> map = new HashMap<>();
        map.put("id","your id");
        map.put("image","your image");
        map.put("comment","your comment");
        map.put("password",password);

Then this code call the post method as the above HashMap as body然后这段代码调用 post 方法作为上面的 HashMap as body

Call<Void> call = retrofitInterface.executeSignUp(map);
call.enqueue(new Callback<Void>() {
            @Override
            public void onResponse(Call<Void> call, Response<Void> response) {
                if (response.code()==200){
                    //Success
                   
                }else {
                    //An error ocuurred
                    
                }
            }

            @Override
            public void onFailure(Call<Void> call, Throwable t) {
                Log.e("ServerCallback", "onFailure: ",t);
            }
        });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM