在Unity中以JSON格式调用Uber API [英] Make call to Uber API in JSON format from within Unity
问题描述
我正在Unity内使用Uber API,并且能够登录然后进行身份验证以获取令牌,但是在调用实际的API时遇到了障碍.
I am using the Uber API inside of Unity and I am able to login and then authenticate to get the Token, but I have run into a roadblock when calling the actual API.
我认为我的问题是我需要以JSON格式进行呼叫,但是我不知道该怎么做.我是HTTP和API的新手.这是我的代码:
I believe my issue is that I need to be making the call in JSON format, but I don't know how to do that. I'm new to HTTP and API's in general. Here is my code:
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);
}
}
这在其他领域对我有用,但是在这一领域不起作用,我认为它与Content Type为JSON有关,但是我不知道如何以正确的格式发送它.抱歉,我无法具体说明,我只是想办法解决这个问题.
This works for me in other areas, but not in this one and I think it has something to do with the Content Type being JSON, but I can't figure out how to send it in the right format. Apologies that I can't be more specific, I'm only just getting my head around this stuff.
任何帮助将不胜感激!
推荐答案
和其他提到的一样,application.json
应该是application/json
.
Like others mentioned, application.json
should be application/json
.
这不是不是唯一的问题.由于它是一个json,因此您无需使用WWWForm
类.创建一个类来保存Json数据,然后创建它的新实例.将实例转换为json并将其传递给UnityWebRequest
Post函数的第二个参数.
This is not the only problem. Since it is a json, you don't need to use WWWForm
class. Create a class to hold the Json data then create new instance of it. Convert the instance to json and pass it to the second parameter of the UnityWebRequest
Post function.
UnityWebRequest :
对于UnityWebRequest
,请使用UnityWebRequest Post(string uri, string postData);
重载,让我们传递url和json数据.然后,使用SetRequestHeader
设置标题.
For UnityWebRequest
, use the UnityWebRequest Post(string uri, string postData);
overload which let's you pass the url and the json data. You then use SetRequestHeader
to set the headers.
[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
的错误的报告,但我个人还没有遇到.
If UnityWebRequest
did not work, use WWW
. There have been reports of bugs with UnityWebRequest
but I have not personally encountered one.
万维网:
对于WWW
,请使用public WWW(string url, byte[] postData, Dictionary<string, string> headers);
构造函数重载,它可以在一个函数调用中接收url,数据和标头.
For WWW
, use the public WWW(string url, byte[] postData, Dictionary<string, string> headers);
constructor overload which takes in the url, the data and the headers in one function call.
[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);
}
}
这篇关于在Unity中以JSON格式调用Uber API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!