gpt4 book ai didi

android - 添加到另一个用户数组解析 Android

转载 作者:太空狗 更新时间:2023-10-29 16:01:18 25 4
gpt4 key购买 nike

在 Parse 中,我在用户表中有另一列称为“ friend ”,它是一个数组。我正在尝试从我当前的用户添加到另一个用户的 friend 数组。

我试过:

//buddy = a string of the other person's username
//to add to CURRENT user's array (this is working)
UserList.user.addUnique("Friends", buddy);
UserList.user.saveEventually();

//this is to add to another user's array (NOT WORKING)
ParseQuery<ParseUser> query = ParseUser.getQuery();
query.whereEqualTo("username", buddy);
query.findInBackground(new FindCallback<ParseUser>() {
public void done(List<ParseUser> objects, ParseException e) {
if (e == null) {
// The query was successful.
for (ParseObject friend : objects) {
friend.addUnique("Friends",UserList.user.getUsername());
friend.saveEventually();
}
} else {
// Something went wrong.
Log.d("Username", "Error: " + e.getMessage());
}
}
});

最佳答案

在java中写一个方法,将数据传到云端,并通过名称调用云端的函数:

// the passes to the cloud is stored on HashMap
HashMap<String, Object> params = new HashMap<String, Object>();
// user's name to add the buddy to it
params.put("username", username);
// buddy's name to pass to the cloud
params.put("buddy", buddy);
ParseCloud.callFunctionInBackground("addBuddyToFriendsList", params, new FunctionCallback<String>(){
public void done(String result, ParseException e){
// e==null means no errors in running the function
if(e==null){
System.out.println(result);
}else{
System.out.println(e.getMessage());
}
}
});

好的,现在让我们编写要在云端使用的函数(假设您知道如何在云端部署函数):

[编辑]

Parse.Cloud.define("addBuddyToFriendsList", function (request, response){
Parse.Cloud.useMasterKey();
var query = new Parse.Query(Parse.User);
query.equalTo("username", request.params.username);
query.find({
success: function(user) {
// query.find returns an array.....
user[0].addUnique("Friends", request.params.buddy);
user[0].save(null, {
success: function(success){
response.success();
},
error: function (error){
response.error(error);
}
});
},
error: function(object, error) {}
});
});

关于android - 添加到另一个用户数组解析 Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31392685/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com