C# 有没有一种方法可以通过使用不同的url多次执行get请求来使用api?

C# 有没有一种方法可以通过使用不同的url多次执行get请求来使用api?,c#,api,unity3d,get-request,C#,Api,Unity3d,Get Request,我试图通过获取给定位置(lat,lon)的currentspeed来消耗TomTomApi。对于每个位置,我需要生成一个url来执行get请求。但是找不到一个好办法。所以最后我想生成5个URL并获得5个不同的数据(所以我的函数应该为每个位置返回5个当前速度) 公共类TomTomApiTrafficFlow:monobhavior{ //参数设置lat&lon+响应类型(json)或api提供的键 [序列化字段] 常量字符串lat=“50.843829”; [序列化字段] 常量字符串lon=“4.

我试图通过获取给定位置(lat,lon)的currentspeed来消耗TomTomApi。对于每个位置,我需要生成一个url来执行get请求。但是找不到一个好办法。所以最后我想生成5个URL并获得5个不同的数据(所以我的函数应该为每个位置返回5个当前速度)

公共类TomTomApiTrafficFlow:monobhavior{
//参数设置lat&lon+响应类型(json)或api提供的键
[序列化字段]
常量字符串lat=“50.843829”;
[序列化字段]
常量字符串lon=“4.369384”;
[序列化字段]
const string typeFile=“json”;//xml ou json
[序列化字段]
常量字符串API_KEY=“API_KEY”;
//我们的请求寄往哪里
常量字符串默认值\u URL=”https://api.tomtom.com/traffic/services/4/flowSegmentData/relative-delay/10/json/";
字符串targetUrl=DEFAULT_URL+“?key=“+API_key+”&point=“+lat+”,“+lon;
//跟踪我们得到的信息
字符串recentData=“”;
无效唤醒()
{
this.startcroutine(this.RequestRoutine(this.targetUrl,this.ResponseCallback));
}
//Web请求通常是异步完成的,因此Unity的Web请求系统
//在等待响应时返回yield指令。
//
私有IEnumerator请求例程(字符串url,操作回调=null)
{
//常量字符串URL=”https://api.tomtom.com/traffic/services/4/flowSegmentData/relative-delay/10/json/“+”?key=“+API_key+”&point=“+lat+”,“+lon;
//使用静态构造函数
var request=UnityWebRequest.Get(url);
//等待响应,然后获取我们的数据
生成返回请求。SendWebRequest();
var data=request.downloadHandler.text;
//这不是必需的,但我更喜欢传入回调,以便
//在此函数之外对响应数据进行操作
if(回调==null)
Log(回调);
回调(数据);
}
//回调以处理我们的响应数据
专用void ResponseCallback(字符串数据)
{
//List LcurSpeed=新列表();
调试日志(数据);
最近数据=数据;
JObject o=JObject.Parse(recentData.ToString());
JToken token=o.SelectToken(“flowSegmentData.currentSpeed”);
recentData=token.ToString();
}
//显示示例的老式GUI系统
void OnGUI()
{
this.targetUrl=GUI.TextArea(新的Rect(0,0500,50),this.targetUrl);
TextArea(新的Rect(0,60,50,50),recentData);
}}

我找到了一种方法,将数据添加到列表并返回UI中的每个元素! 公共类TomTomapitTrafficFlow:单行为 {

//获取当前速度列表的参数
//跟踪我们得到的信息
字符串recentData=“”;
List LrecentData=新列表();
字符串生成器(字符串类型文件、浮点lat、浮点lon)
{
常量字符串API_KEY=“?KEY=hcbqhqcbcqkbclcblcbcbqjq”;
常量字符串默认值\u URL=”https://api.tomtom.com/traffic/services/4/flowSegmentData/relative-delay/10/";
typeFile=typeFile+“/”;//xml或json
字符串geoloc=“&point=“+lat.ToString()+”,“+lon.ToString()”;
字符串targetUrl=默认URL+typeFile+API\U键+geoloc;
返回targetUrl.ToString();
}
//Web请求通常是异步完成的,因此Unity的Web请求系统
//在等待响应时返回yield指令。
//
私有IEnumerator请求例程(操作回调=null){
//List Lurl=新列表();
字符串url0、url1、url2、url3、url4;
url0=GenerateUrls(“json”,50.843829f,4.369384f);
url1=GenerateUrls(“json”,50.844773f,4.356664f);
url2=GenerateUrls(“json”,50.846885f,4.362358f);
url3=GenerateUrls(“json”,50.852922f,4.360137f);
url4=GenerateUrls(“json”,50.852440f,4.367985f);
var Lurl=new List(){url0,url1,url2,url3,url4};
var Ldata=新列表();
foreach(Lurl中的字符串x)
{
var请求=UnityWebRequest.Get(x);
生成返回请求。SendWebRequest();
var data=request.downloadHandler.text;
Ldata.Add(数据);
if(回调!=null)
Log(回调);
回调(数据);}
//回调以处理我们的响应数据
专用void ResponseCallback(字符串数据)
{  
调试日志(数据);
最近数据=数据;
JObject o=JObject.Parse(recentData.ToString());
JToken token=o.SelectToken(“flowSegmentData.currentSpeed”);
recentData=token.ToString();
LrecentData.Add(recentData);
}
//显示示例的老式GUI系统
void OnGUI()
{
TextArea(新的Rect(0,0500,50),LrecentData[0].ToString());
TextArea(新的Rect(0,60,500,50),LrecentData[1].ToString());
TextArea(新的Rect(0,120,500,50),LrecentData[2].ToString());
TextArea(newrect(0180500,50),LrecentData[3].ToString());
TextArea(newrect(02400500,50),LrecentData[4].ToString();}

困惑,只需将新url发送到您的请求例程^^我已经尝试并找到了解决方案
public class TomTomApiTrafficFlow: MonoBehaviour{

// parameter to set lat & lon + type of response (json) or the key providing by the api
[SerializeField]
const string lat = "50.843829";
[SerializeField]
const string lon = "4.369384";
[SerializeField]
const string typeFile = "json";// xml ou json
[SerializeField]
const string API_KEY = "API_KEY";
// Where to send our request
const string DEFAULT_URL = "https://api.tomtom.com/traffic/services/4/flowSegmentData/relative-delay/10/json/";
string targetUrl = DEFAULT_URL + "?key=" + API_KEY + "&point=" + lat + "," + lon;

// Keep track of what we got back
string recentData = "";

void Awake()
{
    this.StartCoroutine(this.RequestRoutine(this.targetUrl,this.ResponseCallback ));
}

// Web requests are typically done asynchronously, so Unity's web request system
// returns a yield instruction while it waits for the response.
//
private IEnumerator RequestRoutine(string url, Action<string> callback = null)
{
    //const string URL = "https://api.tomtom.com/traffic/services/4/flowSegmentData/relative-delay/10/json/" + "?key=" + API_KEY + "&point=" + lat + "," + lon;
    // Using the static constructor
    var request = UnityWebRequest.Get(url);
    // Wait for the response and then get our data
    yield return request.SendWebRequest();
    var data = request.downloadHandler.text;
    // This isn't required, but I prefer to pass in a callback so that I can
    // act on the response data outside of this function
    if (callback == null)
        Debug.Log(callback);
    callback(data);
}

// Callback to act on our response data
private void ResponseCallback(string data)
{
    //List<string> LcurSpeed = new List<string>();
    Debug.Log(data);
    recentData = data;
    JObject o = JObject.Parse(recentData.ToString());
    JToken token = o.SelectToken("flowSegmentData.currentSpeed");
    recentData = token.ToString();
}
// Old fashioned GUI system to show the example
void OnGUI()
{
    this.targetUrl = GUI.TextArea(new Rect(0, 0, 500, 50), this.targetUrl);
    GUI.TextArea(new Rect(0, 60, 50, 50), recentData);
}}
// parameter to get list of currentSpeed
// Keep track of what we got back
string recentData = "";
List<string> LrecentData = new List<string>();



string GenerateUrls(string typeFile,float lat,float lon)
{
    const string API_KEY= "?key=hcbqhqcbcqkbclcblcbcbqjq";

    const string DEFAULT_URL = "https://api.tomtom.com/traffic/services/4/flowSegmentData/relative-delay/10/";
    typeFile = typeFile + "/";// xml or json
    string geoloc= "&point=" + lat.ToString() + "," + lon.ToString();
    string targetUrl = DEFAULT_URL + typeFile + API_KEY + geoloc;

    return targetUrl.ToString();
}

// Web requests are typically done asynchronously, so Unity's web request system
// returns a yield instruction while it waits for the response.
//
private IEnumerator RequestRoutine(Action<string> callback = null){
    //List<string> Lurl = new List<string>();
    string url0, url1, url2, url3, url4;

    url0 = GenerateUrls("json", 50.843829f, 4.369384f);
    url1 = GenerateUrls("json", 50.844773f, 4.356664f);
    url2 = GenerateUrls("json", 50.846885f, 4.362358f);
    url3 = GenerateUrls("json", 50.852922f, 4.360137f);
    url4 = GenerateUrls("json", 50.852440f, 4.367985f);
    var Lurl = new List<string>() { url0, url1, url2, url3, url4 };
    var Ldata = new List<string>();

    foreach (string x in Lurl)
    {
        var request = UnityWebRequest.Get(x);
        yield return request.SendWebRequest();
        var data = request.downloadHandler.text;
        Ldata.Add(data);
        if (callback != null)
            Debug.Log(callback);
        callback(data);}}

// Callback to act on our response data
private void ResponseCallback(string data)
{  
    Debug.Log(data);
    recentData = data;
    JObject o = JObject.Parse(recentData.ToString());
    JToken token = o.SelectToken("flowSegmentData.currentSpeed");
    recentData = token.ToString();
    LrecentData.Add(recentData);
}
// Old fashioned GUI system to show the example
void OnGUI()
{

    GUI.TextArea(new Rect(0, 0, 500, 50), LrecentData[0].ToString());
    GUI.TextArea(new Rect(0, 60, 500, 50), LrecentData[1].ToString());
    GUI.TextArea(new Rect(0, 120, 500, 50), LrecentData[2].ToString());
    GUI.TextArea(new Rect(0, 180, 500, 50), LrecentData[3].ToString());
    GUI.TextArea(new Rect(0, 240, 500, 50), LrecentData[4].ToString()); }}