Facebook Unity和处理应用程序请求

时间:2016-02-12 04:00:34

标签: android json facebook unity3d facebook-unity-sdk

我提出了原来的问题,因为我设法通过追踪和错误以及大量深度搜索来解决问题。 因此,我了解在Unity中使用最新的Facebook SDK,您可以使用以下方式提取玩家的所有待处理请求:

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

其中RequestHandler是IGraphResult,然后您可以将其解析为字典,如下所示:

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

}

该文档解释了如何以JSON格式显示单个请求,并且我已经找到了一些如何使用该信息的示例(我模糊地理解JSON&#39;)但是如果提取所有请求对于播放器,我该如何处理这些信息?

在JSON中,我只是试图提取每个请求的对象ID和发件人ID,根据对象ID处理请求,然后通过连接两者来从图中删除请求,我认为我已经发现了。

所以我的问题是,对于每个请求,我如何提取对象和发件人ID?

1 个答案:

答案 0 :(得分:0)

所以经过大量的试验和错误以及大量的日志检查之后,对于那些不确定的人来说,我已经找到了一种真正的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);
    }
}

同样,这是我第一次使用JSON的体验,我确信这样做的效率要高得多,但基本上经过大量的细分和转换后,我设法提取了对象ID,发件人姓名和ID,附加的邮件以及收件人姓名和ID。对象ID与收件人ID连接在一起,因此要对对象ID本身进行操作,需要将其删除,但是这样可以更容易地传递字符串以从Graph API中删除请求。

如果有人能告诉我一个更有效的方法,我将不胜感激!毕竟,还有更多需要学习的东西。