Search Unity

How do I fetch get request results one by one?

Discussion in 'Multiplayer' started by mody1710, Sep 12, 2019.

  1. mody1710

    mody1710

    Joined:
    May 8, 2017
    Posts:
    2
    I'm using UnityWebRequest to fetch a JSON array of search results from an API. Instead of yielding until the whole array is returned, can I make it so that my code deals with the results one by one?

    Any directions are appreciated.

    Here's some code:
    Code (CSharp):
    1.  public IEnumerator GetMovies(string q)
    2. {
    3.     string uri = "http://www.omdbapi.com/?apikey=__&s=" + q;
    4.     using (UnityWebRequest r = UnityWebRequest.Get(uri))
    5.     {
    6.         yield return r.SendWebRequest();
    7.  
    8.         if(r.isHttpError || r.isNetworkError)
    9.         {
    10.             Debug.Log(r.error);
    11.         }
    12.         else
    13.         {
    14.             SearchInfo info = JsonUtility.FromJson<SearchInfo>(r.downloadHandler.text);
    15.  
    16.             if(info != null)
    17.             {
    18.  
    19.                 gameObject.GetComponent<EventManager>().onSearchInfoGet(info);
    20.             }
    21.         }
    22.     }
    23. }
     
  2. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,732
    You have to use DownloadHandlerScript, if you want to process data on the fly. But I don't know how to deal with JSON parsing on the fly, is there something like reading JSON from stream?