FB 中的回调函数问题.用于 Unity 的 API



我正在使用Unity 5.5.2f1 pro和facebook的SDK v 7.9.4

我有一个脚本,在登录后(在上一个场景中管理)向FB发送API请求,询问朋友,姓名和电子邮件,并将该信息作为POST发送到php网站。

法典:

[Serializable]
public struct FBData {
public string first_name;
public string email;
public string friends;
public string id;}
public class UserManagement : MonoBehaviour {
string urlSaveUserData="some php website";
public Text testTxt;
FBData parsedData;
// Use this for initialization
void Start () {
//Check if it's the first time the user is opening the app. 
if (UserInfo.FIRST_TIME) {
//update text (only used for testing, should be removed in production.) 
testTxt.text = "Your user id is: " + UserInfo.ID;
//Perform FB.API call to get User Data.
getUserData ();
//Save in SQL table. (won't get here if line in getUserData() is active)
StartCoroutine ("saveUserData");
} else {
//do something else.
}

注意:由于这是针对iOS的,因此我必须在设备上对其进行测试,因此我在屏幕上使用文本来显示信息(将其视为一个实现不佳的打印语句)。

问题:在我的FB回调函数中。API 我在文本游戏对象(又名 testTxt)中写入来自响应的解析信息,该信息保存在自定义用户信息 clss 中。它显示正确,但代码卡在那里。它不会继续下一个函数。但是,如果我删除/注释该行并且没有在文本字段中显示任何内容。代码确实继续到 POST 函数,但来自 API 调用的信息没有传递,即我的自定义类为空(导致我相信根本没有调用回调函数)。

public void getUserData(){
string query = "me?fields=first_name,email,friends";
FB.API (query, HttpMethod.GET, Apicallback, new Dictionary<string, string> ());
}
private void Apicallback(IGraphResult result){
//Parse Graph response into a specific class created for this result. 
parsedData = JsonUtility.FromJson<FBData>(result.RawResult);
//Pass each field into UserInfo class. 
UserInfo.EMAIL = parsedData.email;
UserInfo.FRIENDS = parsedData.friends;
UserInfo.NAME = parsedData.first_name;
UserInfo.FACEBOOKID = parsedData.id;
/*problem area, if I comment line below, then previous information is apparently not stored. If left as is then testTxt displays correct information but code gets stuck there.  */
testTxt.text = "This is the info from USerInfoInside the APICallback: " + UserInfo.EMAIL + UserInfo.FRIENDS + UserInfo.FACEBOOKID;
}

下面的功能是将信息发送到php网站,用于说明目的:

法典:

public IEnumerator saveUserData() {
//get user info (this information is EMPTY if line in getUserData() is commented. 
parsedData.id = UserInfo.FACEBOOKID;
parsedData.friends = UserInfo.FRIENDS;
parsedData.first_name = UserInfo.NAME;
parsedData.email = UserInfo.EMAIL;
//translate data into json
string JsonBodyData = JsonUtility.ToJson (parsedData);
//Custom  web request (POST method doesnt seem to work very well, documentation example sends empty form)
var w = new UnityWebRequest(urlSaveUserData, "POST");
byte[] bodyRaw = new System.Text.UTF8Encoding().GetBytes(JsonBodyData);
w.uploadHandler = (UploadHandler) new UploadHandlerRaw(bodyRaw);
w.downloadHandler = (DownloadHandler) new DownloadHandlerBuffer();
w.SetRequestHeader("Content-Type", "application/json");
yield return w.Send();
//work with received data...}

我被困在这里,任何帮助都是值得赞赏的。谢谢!

当直接将字符串用于 JSON 或 HTTP POST 和 GET 方法时,请确保使用 EscapeURL。缺乏这种处理往往会把事情搞砸,尤其是在iOS平台中。

据我所知,这段代码

字符串查询 = "我?字段=first_name,电子邮件,朋友";

应该转义为

字符串查询 = WWW。EscapeURL("me?fields=first_name,email,friends");

因此,像"?"这样的字符不会被编码为 URL 符号。

我假设您不需要为您的说明性示例执行此操作,因为 UnityWebRequest 已经在内部转义了您的 POST 请求字符串,但我无法完全确认这一点。

最新更新