我正在使用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的回调函数中,我在文本Gameobject(aka testTxt)中写入了自定义UserInfo 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...}
我被困在这里任何帮助表示赞赏。谢谢!
答案 0 :(得分:0)
在将字符串直接用于JSON或HTTP POST和GET方法时,请务必使用EscapeURL。缺乏这种处理往往会使事情变得棘手,特别是在iOS平台上。
从我所看到的,这段代码
应该将string query =&#34; me?fields = first_name,email,friends&#34 ;;
转义为
string query = WWW.EscapeURL(&#34; me?fields = first_name,email,friends&#34;);
所以字符如&#34;?&#34;不会被编码为URL符号。
我假设您不需要为您的说明性示例执行此操作,因为UnityWebRequest已在内部转义您的POST请求字符串,但我无法完全确认。