简体   繁体   English

Facebook Unity和处理应用程序请求

[英]Facebook Unity and handling app requests

I pulled my original question because I managed to figured it out through trail and error and a lot of deep searching. 我提出了最初的问题,是因为我设法通过错误和错误以及大量的深入搜索来弄清了这个问题。 So, I understand that using the latest Facebook SDK in Unity, you can pull all pending requests for a player using: 因此,我了解到,使用Unity中最新的Facebook SDK,您可以使用以下方法拉取播放器的所有待处理请求:

FB.API("/me/apprequests", HttpMethod.GET, RequestHandler)

Where RequestHandler is an IGraphResult, which you can then parse into a Dictionary, like so: 其中RequestHandler是IGraphResult,然后可以将其解析为Dictionary,如下所示:

void RequestHandler(IGraphResult result){
    if (result != null) {
        Dictionary<string, object> reqResult = Json.Deserialize(result.RawResult) as Dictionary<string, object>;
    }

} }

The documentation explains how a singular request will be displayed in JSON format, and I've found a few examples of how to work with that information (I vaguely understand JSON's), however if pulling ALL requests for the player, how do I work with this information? 该文档说明了如何以JSON格式显示单个请求,并且我找到了一些如何使用该信息的示例(我隐约地理解了JSON),但是如果拉动播放器的所有请求,我将如何处理这条信息?

Out of the JSON, I'm just trying to pull the object ID and sender ID of each request, handle the request based on the object ID then delete the request from the graph by concatenating the two, which I think I've figured out already. 从JSON中,我只是尝试提取每个请求的对象ID和发件人ID,基于对象ID处理请求,然后通过将两者串联而从图中删除该请求,我认为我已经弄清楚了已经。

So my question is, for each request, how do I extract the object and sender ID's? 所以我的问题是,对于每个请求,如何提取对象和发件人ID?

So after a LOT of trial and error and a lot of Log checks, I've figured out a really hacky way of doing it, for those that aren't sure: 因此,经过大量的试验和错误以及大量的日志检查之后,对于那些不确定的对象,我已经找到了一种非常hacky的方法:

public void TestRequests(){
    FB.API("/me/apprequests", HttpMethod.GET, TestResponse);
}

public void TestResponse(IGraphResult result){
    if (result.Error == null) {
        //Grab all requests in the form of  a dictionary.
        Dictionary<string, object> reqResult = Json.Deserialize(result.RawResult) as Dictionary<string, object>;
        //Grab 'data' and put it in a list of objects.
        List<object> newObj = reqResult["data"] as List<object>;
        //For every item in newObj is a separate request, so iterate on each of them separately.
        for(int xx = 0; xx < newObj.Count; xx++){
            Dictionary<string, object> reqConvert = newObj[0] as Dictionary<string, object>;
            Dictionary<string, object> fromString = reqConvert["from"] as Dictionary<string, object>;
            Dictionary<string, object> toString = reqConvert["to"] as Dictionary<string, object>;
            string fromName = fromString["name"] as string;
            string fromID = fromString["id"] as string;
            string obID = reqConvert["id"] as string;
            string message = reqConvert["message"] as string;
            string toName = toString["name"] as string;
            string toID = toString["id"] as string;
            Debug.Log ("Object ID: " + obID);
            Debug.Log ("Sender message: " + message);
            Debug.Log ("Sender name: " + fromName);
            Debug.Log ("Sender ID: " + fromID);
            Debug.Log ("Recipient name: " + toName);
            Debug.Log ("Recipient ID: " + toID);
        }
    } 
    else {
        Debug.Log ("Something went wrong. " + result.Error);
    }
}

Again, this is my first experience with using JSON, and I'm sure there's a much more efficient way of doing this, but basically after a lot of breaking down and converting, I've managed to extract the object ID, sender name and ID, the message attached and the recipient name and ID. 再说一次,这是我第一次使用JSON,我敢肯定有一种更有效的方法,但是基本上,经过大量分解和转换后,我设法提取了对象ID,发件人姓名和ID,邮件附件以及收件人姓名和ID。 The object ID comes concatenated with the recipient ID, so to operate on the object ID itself, this will need to be removed, however as is it will make it easier to pass the string on to remove the request from the Graph API. 对象ID与接收者ID串联在一起,因此要对对象ID本身进行操作,则需要将其删除,但是这样可以更轻松地传递字符串以从Graph API中删除请求。

If anyone can suggest to me a more efficient way of doing this, I'd be grateful! 如果有人可以建议我这样做的更有效方法,我将不胜感激! There's always more to learn, after all. 毕竟,总会有更多的东西要学习。

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

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