从Unity内部以JSON格式调用Uber API

时间:2017-05-05 19:46:09

标签: c# json unity3d uber-api

我在Unity内部使用Uber API,我可以登录然后进行身份验证以获取令牌,但是在调用实际API时遇到了障碍。

我认为我的问题是我需要以JSON格式拨打电话,但我不知道该怎么做。我是HTTP和API的新手。这是我的代码:

    private IEnumerator TestRequest(){
    Debug.Log(sToken);
    WWWForm form = new WWWForm();
    //WWW www = new WWW();
    form.headers["Content-Type"] = "application.json";
    form.headers["Authorization"] = "Bearer " +sToken;
    form.AddField( "fare_id", "abcd");
    form.AddField("product_id", "a1111c8c-c720-46c3-8534-2fcdd730040d");
    form.AddField("start_latitude", "37.761492");
    form.AddField("start_longitude", "-122.42394");
    form.AddField("end_latitude", "37.775393");
    form.AddField("end_longitude", "-122.417546");

    yield return null;

    using(UnityWebRequest uweb = UnityWebRequest.Post("https://sandbox-
    api.uber.com/v1.2/requests", form)){
        yield return uweb.Send();
        if(uweb.isError) Debug.Log(uweb.error);
        else Debug.Log(uweb.downloadHandler.text);
        //GetVals(uweb.downloadHandler.text);
    }
}

这在我的其他方面适用,但不适用于此,我认为它与内容类型为JSON有关,但我无法弄清楚如何以正确的格式发送它。抱歉,我可以更具体,我只是想了解这些事情。

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:2)

与其他人一样,application.json应为application/json

这是唯一的问题。由于它是json,因此您不需要使用WWWForm类。创建一个类来保存Json数据,然后创建它的新实例。将实例转换为json并将其传递给UnityWebRequest Post函数的第二个参数。

<强> UnityWebRequest

对于UnityWebRequest,请使用UnityWebRequest Post(string uri, string postData);重载,以便传递网址和json数据。然后使用SetRequestHeader设置标头。

[Serializable]
public class UberJson
{
    public string fare_id;
    public string product_id;
    public double start_latitude;
    public double start_longitude;
    public double end_latitude;
    public double end_longitude;
}

void Start()
{
    postJson();
}

string createUberJson()
{
    UberJson uberJson = new UberJson();

    uberJson.fare_id = "abcd";
    uberJson.product_id = "a1111c8c-c720-46c3-8534-2fcdd730040d";
    uberJson.start_latitude = 37.761492f;
    uberJson.start_longitude = -122.42394f;
    uberJson.end_latitude = 37.775393f;
    uberJson.end_longitude = -122.417546f;

    //Convert to Json
    return JsonUtility.ToJson(uberJson);
}

void postJson()
{
    string URL = "https://sandbox-api.uber.com/v1.2/requests";

    //string json = "{ \"fare_id\": \"abcd\", \"product_id\": \"a1111c8c-c720-46c3-8534-2fcdd730040d\", \"start_latitude\": 37.761492, \"start_longitude\": -122.423941, \"end_latitude\": 37.775393, \"end_longitude\": -122.417546 }";

    string json = createUberJson();

    string sToken = "";

    //Set the Headers
    UnityWebRequest uwrq = UnityWebRequest.Post(URL, json);
    uwrq.SetRequestHeader("Content-Type", "application/json");
    uwrq.SetRequestHeader("Authorization", "Bearer " + sToken);

    StartCoroutine(WaitForRequest(uwrq));
}

IEnumerator WaitForRequest(UnityWebRequest uwrq)
{
    //Make the request
    yield return uwrq.Send();
    if (String.IsNullOrEmpty(null))
    {
        Debug.Log(uwrq.downloadHandler.text);
    }
    else
    {
        Debug.Log("Error while rececing: " + uwrq.error);
    }
}

如果UnityWebRequest无效,请使用WWW。有报告称有UnityWebRequest的错误,但我个人没遇到过。

<强> WWW:

对于WWW,使用public WWW(string url, byte[] postData, Dictionary<string, string> headers);构造函数重载,它在一个函数调用中接收url,数据和标题。

[Serializable]
public class UberJson
{
    public string fare_id;
    public string product_id;
    public double start_latitude;
    public double start_longitude;
    public double end_latitude;
    public double end_longitude;
}

void Start()
{
    postJson();
}

string createUberJson()
{
    UberJson uberJson = new UberJson();

    uberJson.fare_id = "abcd";
    uberJson.product_id = "a1111c8c-c720-46c3-8534-2fcdd730040d";
    uberJson.start_latitude = 37.761492f;
    uberJson.start_longitude = -122.42394f;
    uberJson.end_latitude = 37.775393f;
    uberJson.end_longitude = -122.417546f;

    //Convert to Json
    return JsonUtility.ToJson(uberJson);
}


void postJson()
{
    string URL = "https://sandbox-api.uber.com/v1.2/requests";

    //string json = "{ \"fare_id\": \"abcd\", \"product_id\": \"a1111c8c-c720-46c3-8534-2fcdd730040d\", \"start_latitude\": 37.761492, \"start_longitude\": -122.423941, \"end_latitude\": 37.775393, \"end_longitude\": -122.417546 }";

    string json = createUberJson();

    string sToken = "";

    //Set the Headers
    Dictionary<string, string> headers = new Dictionary<string, string>();
    headers.Add("Content-Type", "application/json");
    headers.Add("Authorization", "Bearer " + sToken);
    //headers.Add("Content-Length", json.Length.ToString());

    //Encode the JSON string into a bytes
    byte[] postData = System.Text.Encoding.UTF8.GetBytes(json);

    WWW www = new WWW(URL, postData, headers);
    StartCoroutine(WaitForRequest(www));
}

IEnumerator WaitForRequest(WWW www)
{
    yield return www;
    if (String.IsNullOrEmpty(null))
    {
        Debug.Log(www.text);
    }
    else
    {
        Debug.Log("Error while rececing: " + www.error);
    }
}