C# 获取请求不保存数据

C# 获取请求不保存数据,c#,unity3d,coroutine,C#,Unity3d,Coroutine,我正试图保存GET请求中的一些数据。我使用startcroutine进行请求,使用Lambda表达式保存数据 我的代码是: Using UnityEngine; using System.Collections; public class Test : MonoBehaviour { // Use this for initialization public void Start () { string url1 = "http://localhost/virtua

我正试图保存GET请求中的一些数据。我使用startcroutine进行请求,使用Lambda表达式保存数据

我的代码是:

  Using UnityEngine;
  using System.Collections;

  public class Test : MonoBehaviour {

 // Use this for initialization
 public void Start () {
     string url1 = "http://localhost/virtualTV/query/?risorsa=";

     string ciao = "http://desktop-pqb3a65:8080/marmotta/resource/ef299b79-35f2-4942-a33b-7e4d7b7cbfb5";
     url1 = url1 + ciao;

     WWW www1 = new WWW(url1);

     var main=new JSONObject(JSONObject.Type.OBJECT);
     var final= new JSONObject(JSONObject.Type.OBJECT);;
     StartCoroutine(firstParsing((value)=>{main = value;
         final= main.Copy();
         Debug.Log(main);
     }));
     Debug.Log(final);
 }

 public IEnumerator firstParsing( System.Action<JSONObject> callback)
 {
     string url2 = "http://localhost/virtualTV/FirstQuery/?risorsa=";
     string ciao = "http://desktop-pqb3a65:8080/marmotta/resource/ef299b79-35f2-4942-a33b-7e4d7b7cbfb5";
     url2 = url2 + ciao;
     WWW www2 = new WWW(url2);
     yield return www2;
     string json = www2.text;

     //Parsing del json con creazione di un array
     var firstjson = new JSONObject(json);
     var tempVideo = new JSONObject(JSONObject.Type.OBJECT);
     var array2 = new JSONObject(JSONObject.Type.OBJECT);

             tempVideo.AddField ("id", firstjson.GetField ("id"));
             tempVideo.AddField ("type", firstjson.GetField ("type"));
             tempVideo.AddField ("url", firstjson.GetField ("url"));
             array2.Add (tempVideo);

     yield return array2;    
     callback (array2);
     Debug.Log ("First Run" + array2);

 }

它是空的。你能帮我保存变量final中的值吗?谢谢大家。

协同程序的执行是跨多个帧进行的。当协同程序遇到
yield return
语句时,它将返回到调用方法,该方法将完成执行,直到任务完成


在您的情况下,
Start
中的
Debug.Log(final)
语句在
yield返回www2时立即执行firstParsing
中的code>。回调尚未调用,这就是
final
为空的原因

要在回调函数外部分配值后访问
final
中的值,必须在回调函数中分配
final
后设置
bool
,该值设置为
true
。大概是这样的:

StartCoroutine(firstParsing((value)=>{main = value;
    final= main.Copy();
    Debug.Log(main);
    isFinalAssigned = true;
}));

// In another method
if(isFinalAssigned)
{
    // Access final
}
您必须注意,上面的
if
语句仅在定期调用的方法(如
Update
)中有用。如果在只调用一次的方法(如
OnEnable
)中访问final,则必须等待
final
被分配。您可以使用另一个协同程序执行此任务,如

IEnumerator DoSomethingWithFinal()
{
    while(!isFinalAssigned)
       yield return null; // Wait for next frame
    // Do something with final
}
最简单的方法是在
回调中使用(访问)
final

编辑2:根据你的评论,你可以做如下的事情。您将使用协同程序,因为阻止主游戏线程不是一个好主意

private JSONObject final = null; // Make final a field
无论您在哪里使用
final
,都有两个选项

  • 如果(final==null)返回,则使用空检查
    这可能不切实际
  • 等待
    final
    在协同程序中分配,然后做一些回调操作。这是你能干净利落地做你想做的事情的唯一方法 请查看下面的实现

    // Calls callback after final has been assigned
    IEnumerator WaitForFinal(System.Action callback) 
    {
        while(final == null)
            yield return null; // Wait for next frame
        callback();
    }
    
    // This whole method depends on final. 
    // This should be similar to your method set up if you have 
    // good coding standards (not very long methods, each method does only 1 thing)
    void MethodThatUsesFinal()
    {
        if (final == null)
        {
            // Waits till final is assigned and calls this method again
            StartCoroutine(WaitForFinal(MethodThatUsesFinal));
            return;
        }
    
        // use final
    }
    

    协同程序的执行分散在多个帧上。当协同程序遇到
    yield return
    语句时,它将返回到调用方法,该方法将完成执行,直到任务完成


    在您的情况下,
    Start
    中的
    Debug.Log(final)
    语句在
    yield返回www2时立即执行firstParsing
    中的code>。回调尚未调用,这就是
    final
    为空的原因

    要在回调函数外部分配值后访问
    final
    中的值,必须在回调函数中分配
    final
    后设置
    bool
    ,该值设置为
    true
    。大概是这样的:

    StartCoroutine(firstParsing((value)=>{main = value;
        final= main.Copy();
        Debug.Log(main);
        isFinalAssigned = true;
    }));
    
    // In another method
    if(isFinalAssigned)
    {
        // Access final
    }
    
    您必须注意,上面的
    if
    语句仅在定期调用的方法(如
    Update
    )中有用。如果在只调用一次的方法(如
    OnEnable
    )中访问final,则必须等待
    final
    被分配。您可以使用另一个协同程序执行此任务,如

    IEnumerator DoSomethingWithFinal()
    {
        while(!isFinalAssigned)
           yield return null; // Wait for next frame
        // Do something with final
    }
    
    最简单的方法是在
    回调中使用(访问)
    final

    编辑2:根据你的评论,你可以做如下的事情。您将使用协同程序,因为阻止主游戏线程不是一个好主意

    private JSONObject final = null; // Make final a field
    
    无论您在哪里使用
    final
    ,都有两个选项

  • 如果(final==null)返回,则使用空检查
    这可能不切实际
  • 等待
    final
    在协同程序中分配,然后做一些回调操作。这是你能干净利落地做你想做的事情的唯一方法 请查看下面的实现

    // Calls callback after final has been assigned
    IEnumerator WaitForFinal(System.Action callback) 
    {
        while(final == null)
            yield return null; // Wait for next frame
        callback();
    }
    
    // This whole method depends on final. 
    // This should be similar to your method set up if you have 
    // good coding standards (not very long methods, each method does only 1 thing)
    void MethodThatUsesFinal()
    {
        if (final == null)
        {
            // Waits till final is assigned and calls this method again
            StartCoroutine(WaitForFinal(MethodThatUsesFinal));
            return;
        }
    
        // use final
    }
    


    Debug.Log(main)的输出是什么?它包含我要存储在final中的JSON,它不能在variable final中复制variable main,如果我将final打印到Start例程中,它包含JSON,之后它是空的。是否有任何错误?无错误,它运行良好为什么不直接从
    value
    分配它,而不是使用
    Copy
    ?Debug.Log(main)的输出是什么?它包含我要存储在final中的JSON,它不能在variable final中复制variable main,如果我将final打印到Start例程中,它包含JSON,之后它是空的。是否有任何错误?无错误,它运行良好为什么不直接从
    value
    赋值,而不是使用
    Copy
    ?你能给我举一个最浪费的访问ad变量final的方法的例子吗?因为我尝试使用第一种方法,但它从未分配变量final。多亏了洛蒂,我在调用StartRoutine的相同方法中使用了变量final,您是否可以将代码移动到另一个方法?如果需要访问局部变量,可以将访问代码移动到回调中。您能确切地告诉我们您想用
    final
    做什么吗?我所说的第一种方法在
    Start
    方法中不起作用,而第二种方法会起作用。我可以移动代码,但我需要在调用Start例程函数后,在variable final中获得查询结果,所以我想调用Start例程,等待它完成,并在程序的其余部分使用我的variable final。Start例程可以调用其他IEnumerator或其他函数,但我希望等待它完成并有最终结果。@user3836982检查我编辑的答案。我还建议您删除额外的副本(删除
    main
    并设置
    final=value
    ),您能给我一个最浪费的访问ad变量final的方法的例子吗?因为我尝试使用第一种方法,但它从未分配变量final。多亏了洛蒂,我在调用StartRoutine的相同方法中使用了变量final,您是否可以将代码移动到另一个方法?如果需要访问局部变量,可以将访问代码移动到回调中。您能确切地告诉我们您想用
    final
    做什么吗?第一种方法我