Hello everyone.

The Mixed Reality Forums here are no longer being used or maintained.

There are a few other places we would like to direct you to for support, both from Microsoft and from the community.

The first way we want to connect with you is our mixed reality developer program, which you can sign up for at https://aka.ms/IWantMR.

For technical questions, please use Stack Overflow, and tag your questions using either hololens or windows-mixed-reality.

If you want to join in discussions, please do so in the HoloDevelopers Slack, which you can join by going to https://aka.ms/holodevelopers, or in our Microsoft Tech Communities forums at https://techcommunity.microsoft.com/t5/mixed-reality/ct-p/MicrosoftMixedReality.

And always feel free to hit us up on Twitter @MxdRealityDev.

How to call an API in the HoloLens?

Hey guys,

I am building an application to display stock quotes on boards.
When i run the application in unity the API data from the response is displayed on the boards, but when i build to the hololens there is no data on the boards. only the headers that i have hardcoded in there.

I have used the WWW and UnityWebRequest to do the API call so far but both gave the same result.

Anyone else bumped into this issue?

note: in player settings in unity, under the publishing settings, i have checked the following.

  • InternetClient
  • InternetClientServer
  • PrivateNetworkClientServer
  • Microphone
  • SpacialPerception

Best Answers

  • Devc0nDevc0n
    Answer ✓

    @jbienzms Thanx for the mention to check the messages aswell. i have searched to forum to make the debug window visible inside the emulator/device and i got a message that the InternetClient capability was unchecked. this needs to be check for using www. i had this done at first but then it got unchecked somehow (forgot to save file or something i guess.)

    @pestantium thanx for the code example provided for my issue.

    But everything is working now. If someone else is having the same issues as i had please see this post to get the DebugWindow inside of your application.

Answers

  • @Devc0n it appears you've done everything you should have done. Have you attached the Visual Studio debugger to see if there are any errors being logged or thrown?

    Our Holographic world is here

    RoadToHolo.com      WikiHolo.net      @jbienz
    I work in Developer Experiences at Microsoft. My posts are based on my own experience and don't represent Microsoft or HoloLens.

  • @jbienzms Thanx for commenting on my question.
    I have used the debugger in visual studio to watch for errors but sadly enough, nothing.... (also connected the debugger in the build settings of the project in unity).

    The strange part for me here is that it does work in unity. but on the device or emulator nothing shows up on the boards. i have no idea why there would be a difference in those two.

  • Same here. Use a Unity 5.5.1f1. In editor it works fine but in runtime I get same response on WWW, Checked in Visual Studio in Debug mode.

  • @pestantium Thanx for the example, i have used your format to create my request. but sadly enough still i am not able to see the data in the hololens.
    also Debug.Log does not seem to work for the VisualStudio debugger on HoloLens? either this or the entire script does not start when the application runs in the hololens.

    Here is the code i am using now (based on your example, works inside of unity)

    using System.Collections;
    using System.Collections.Generic;
    using System;
    using UnityEngine;
    using UnityEngine.UI;
    
    namespace Assets.Scripts {
    
        public class APIRes : MonoBehaviour {
            private int irequest = 0;
            private float updateTime = 30f;
            private string results;
            private Coroutine _coroutine;
            private Text stock, bid, offer, last, vol;
            string baseUrl = "http://finance.yahoo.com/d/quotes.csv";
            string symbolsUrl = "?s=GOOG+MSFT";
            string formatUrl = "&f=nabl1v";
    
            private void Start() {
                _coroutine = StartCoroutine(CoUpdate());
            }
    
            private void CollectResult() {
                Debug.Log("CollectResult");
                string[] lines = ParsingCSV(results);
                printToBoard(lines);
            }
    
            private void printToBoard(string[] lines) {
                //Loop through lines to paste them on UI
                for (int i = 0; i < lines.Length - 1; i++) {
                    string[] values = lines[i].Split(',');
    
                    //get rule if exists
                    if (i == 0) {
                        // object exists, init the text lines
                        Transform nameTr = gameObject.transform.Find("Console/DataRules/Rule/Panel/Name");
                        stock = nameTr.GetComponent<Text>();
    
                        Transform bidTr = gameObject.transform.Find("Console/DataRules/Rule/Panel/Bid");
                        bid = bidTr.GetComponent<Text>();
    
                        Transform offerTr = gameObject.transform.Find("Console/DataRules/Rule/Panel/Offer");
                        offer = offerTr.GetComponent<Text>();
    
                        Transform lastTr = gameObject.transform.Find("Console/DataRules/Rule/Panel/Last");
                        last = lastTr.GetComponent<Text>();
    
                        Transform volTr = gameObject.transform.Find("Console/DataRules/Rule/Panel/Vol");
                        vol = volTr.GetComponent<Text>();
    
                    } else if (i > 0) {
                        // checking duplicates, so we use the value of 'i'
                        // object exists, init the text lines
                        if (gameObject.transform.Find("Console/DataRules/Rule(" + i + ")/Panel/Name") != null) {
                            Transform nameTr = gameObject.transform.Find("Console/DataRules/Rule(" + i + ")/Panel/Name");
                            stock = nameTr.GetComponent<Text>();
    
                            Transform bidTr = gameObject.transform.Find("Console/DataRules/Rule(" + i + ")/Panel/Bid");
                            bid = bidTr.GetComponent<Text>();
    
                            Transform offerTr = gameObject.transform.Find("Console/DataRules/Rule(" + i + ")/Panel/Offer");
                            offer = offerTr.GetComponent<Text>();
    
                            Transform lastTr = gameObject.transform.Find("Console/DataRules/Rule(" + i + ")/Panel/Last");
                            last = lastTr.GetComponent<Text>();
    
                            Transform volTr = gameObject.transform.Find("Console/DataRules/Rule(" + i + ")/Panel/Vol");
                            vol = volTr.GetComponent<Text>();
    
                        }
                    }
    
                    // paste text on its containing fields
                    stock.text = values[0];
                    bid.text = values[1];
                    offer.text = values[2];
                    last.text = values[3];
                    vol.text = values[4];
                }
            }
    
            private IEnumerator CoUpdate() {
                while (true) {
                    results = "";
                    WebQuery();
                    yield return new WaitForSeconds(updateTime);
                }
            }
    
            private void WebQuery() {
                Debug.Log("WebQuery");
                //Worked
                irequest++;
                string url = baseUrl + symbolsUrl + formatUrl + "?irequest=" + irequest.ToString();
                Debug.Log("WebQuery | Url to contact: " + url );
                WWW www = new WWW(url);
    
                StartCoroutine(WaitForRequest(www, CollectResult));
            }
    
            private IEnumerator WaitForRequest(WWW www, Action onComplete) {
                Debug.Log("WaitForRequest");
                yield return www;
                if (www.error == null) {
                    results = www.text;
                    onComplete();
                    www.Dispose();
                } else {
                    Debug.Log("WaitForRequest | " + www.error);
                }
            }
    
            public string[] ParsingCSV(string csvTextParsing) {
                string[] line = csvTextParsing.Split("\n"[0]);
                return line;
            }
        }
    }
    
  • The irequest variable used in the sample @pestantium posted is to get around HTTP request caching. It's interesting that only happens at runtime and not in the editor. If so, that must be due to how UWP handles requests under the covers and Unity's WWW class must use that stack when targeting UWP. That would make sense.

    Sorry @Devc0n but I'm still at a loss as to why your code isn't working. I haven't had a chance to actually build your code and test it.

    What happens when you run under the Visual Studio debugger after deploying to HoloLens? Is www.error still null? And what is contained in www.text?

    Our Holographic world is here

    RoadToHolo.com      WikiHolo.net      @jbienz
    I work in Developer Experiences at Microsoft. My posts are based on my own experience and don't represent Microsoft or HoloLens.

  • P.S. I want to make sure we figure out what's going on with the www class since it should work and since it is "Unity Portable". But if you are in a pinch, you can look at using the underlying HttpClient class. It's very powerful and fast, but it is UWP-only, which means you'll have to use conditional compilation in your code and deal with Async (which Unity doesn't understand).

    If you want to or need to go down this path, you can look at the TextToSpeechManager class in the toolkit as an example of how to call UWP functions and deal with Async.

    Our Holographic world is here

    RoadToHolo.com      WikiHolo.net      @jbienz
    I work in Developer Experiences at Microsoft. My posts are based on my own experience and don't represent Microsoft or HoloLens.

  • Devc0nDevc0n
    Answer ✓

    @jbienzms Thanx for the mention to check the messages aswell. i have searched to forum to make the debug window visible inside the emulator/device and i got a message that the InternetClient capability was unchecked. this needs to be check for using www. i had this done at first but then it got unchecked somehow (forgot to save file or something i guess.)

    @pestantium thanx for the code example provided for my issue.

    But everything is working now. If someone else is having the same issues as i had please see this post to get the DebugWindow inside of your application.

Sign In or Register to comment.