Search Unity

How i consume from a api restful with unity tiny?

Discussion in 'Project Tiny' started by Rob_Elias, Feb 1, 2019.

  1. Rob_Elias

    Rob_Elias

    Joined:
    Mar 21, 2014
    Posts:
    13
  2. abeisgreat

    abeisgreat

    Joined:
    Nov 21, 2016
    Posts:
    45
    Code (JavaScript):
    1. function reqListener () {
    2.   const data = JSON.parse(this.responseText);
    3.   console.log(data);
    4. }
    5.  
    6. const oReq = new XMLHttpRequest();
    7. oReq.addEventListener("load", reqListener);
    8. oReq.open("GET", url);
    9. oReq.send();
     
    polytropoi likes this.
  3. Rob_Elias

    Rob_Elias

    Joined:
    Mar 21, 2014
    Posts:
    13

    Thank you! can you show me the complete script? It does not work well. The truth is i'm quite disoriented with the Unity Tiny implementations. Lol
    Thank you

    this is what i did

    namespace game {

    @ut.executeAfter(ut.Shared.UserCodeStart)
    @ut.executeBefore(ut.Shared.UserCodeEnd)
    @ut.requiredComponents(game.RequestLog)
    //var url: string = "https://jsonplaceholder.typicode.com/posts/1/comments";

    /** New System */
    export class Request extends ut.ComponentSystem {


    OnUpdate():void {
    this.world.forEach([ut.Entity, game.RequestLog, ut.Text.Text2DRenderer], (entity, request, myText)=> {
    var url: string = "https://jsonplaceholder.typicode.com/posts/1/comments";
    let qt1 = this.world.getComponentData(this.world.getEntityByName("QuestionText"), ut.Text.Text2DRenderer);
    //qt1.text = "What is your";
    const oReq = new XMLHttpRequest();
    oReq.addEventListener("load", oReq);
    oReq.open("GET", url);
    oReq.send();
    const data = JSON.parse(url);
    qt1.text = data.string;
    this.world.addComponentData(entity, data);
    });

    }
    /* reqListener ():void {
    const data = JSON.parse(this.responseText);
    console.log(data);
    }*/


    }
    }
     
    Last edited: Feb 1, 2019
  4. abeisgreat

    abeisgreat

    Joined:
    Nov 21, 2016
    Posts:
    45
    What's happening when you run this? What does "does not work well" mean?
     
  5. Rob_Elias

    Rob_Elias

    Joined:
    Mar 21, 2014
    Posts:
    13
    When i push play the game does not open the scene
     
  6. abeisgreat

    abeisgreat

    Joined:
    Nov 21, 2016
    Posts:
    45
    Are there errors in the Untiy console / terminal?
     
  7. Rob_Elias

    Rob_Elias

    Joined:
    Mar 21, 2014
    Posts:
    13
    The console does not show any errors but does not consume the service
     
  8. Rob_Elias

    Rob_Elias

    Joined:
    Mar 21, 2014
    Posts:
    13
    I already got it! thank you very much for the help provided

    namespace game {
    @ut.executeAfter(ut.Shared.PlatformRenderingFence)
    @ut.requiredComponents(game.RequestLog)

    /** New System */
    export class Request extends ut.ComponentSystem {

    OnUpdate():void {

    this.world.forEach([game.RequestLog], (request) => {
    this.loadAsync(request.url,(error, data) => {
    if (error) return;
    else{ console.log(data); }
    });
    });
    }


    loadAsync(url: string, callback: (error?: any, jsonResponse?: any) => void): void {
    var xhr = new XMLHttpRequest();
    xhr.open('GET', url, true);
    xhr.responseType = 'json';
    xhr.setRequestHeader("Content-Type", "application/json");
    xhr.onload = function () {
    var status = xhr.status;
    if (status == 200) callback(null, xhr.response);
    else callback(status);
    };
    xhr.send();
    }


    }
    }
     
  9. TheFordeD

    TheFordeD

    Joined:
    Jul 9, 2018
    Posts:
    32
    Code (JavaScript):
    1. static ApiGET(url: string, params: any, callback: (error?: any, jsonResponse?: any) => void): void {
    2.             var xhr = new XMLHttpRequest();
    3.             var query = Object.keys(params).map(function (key) {
    4.                 return key + '=' + params[key];
    5.             }).join('&');
    6.             xhr.open('GET', game.Request.API_URL+url + (query ? "?"+query : ""), true);
    7.             xhr.responseType = 'json';
    8.             xhr.timeout = 2000;
    9.             // xhr.setRequestHeader("Content-Type", "application/json");
    10.             xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    11.             xhr.onload = function () {
    12.             var status = xhr.status;
    13.                 if (status == 200) callback(null, xhr.response);
    14.                 else callback(status);
    15.             };
    16.             xhr.send();
    17.         }
    18.  
    19.         static ApiPOST(url: string, params: any, callback: (error?: any, jsonResponse?: any) => void): void {
    20.             var xhr = new XMLHttpRequest();
    21.             var sendData = this.serializeData(params);
    22.             xhr.open('POST', url, true);
    23.             xhr.responseType = 'json';
    24.             xhr.timeout = 2000;
    25.             // xhr.setRequestHeader("Content-Type", "application/json");
    26.             xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    27.             xhr.onload = function () {
    28.             var status = xhr.status;
    29.                 if (status == 200) callback(null, xhr.response);
    30.                 else callback(status);
    31.             };
    32.             xhr.send(sendData);
    33.         }
     
    Dunk86 likes this.
  10. Rob_Elias

    Rob_Elias

    Joined:
    Mar 21, 2014
    Posts:
    13
    Thanks to all for the help
     
  11. rachidDev

    rachidDev

    Joined:
    May 19, 2017
    Posts:
    10
    @-ShadowHunter- hello bro, can you please share with us example or tutorial how you get success get data from json file, I'm not familiar in tiny mode, I get confusing everytime I use TinyMode, trying to understand, and hope get help from you with a succesful example, thank in advance
     
    K1kk0z90_Unity likes this.
  12. marcus-qiiwi

    marcus-qiiwi

    Joined:
    Jul 29, 2013
    Posts:
    20
    Thanks!

    Might I ask to see how the serializeData method looks like?
     
  13. reallyhexln

    reallyhexln

    Joined:
    Jun 18, 2018
    Posts:
    69
    I assume it something like this:

    Code (JavaScript):
    1. function serializeData(params: any): FormData {
    2.     let formData: FormData = new FormData();
    3.  
    4.     for (let key in params) {
    5.         if (params.hasOwnProperty(key)) {
    6.             formData.append(key, params[key]);
    7.         }
    8.     }
    9.  
    10.     return formData;
    11. }