Search Unity

  1. Unity 2019.1 is now released.
    Dismiss Notice

How i consume from a api restful with unity tiny?

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

  1. -ShadowHunter-

    -ShadowHunter-

    Joined:
    Mar 21, 2014
    Posts:
    10
  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. -ShadowHunter-

    -ShadowHunter-

    Joined:
    Mar 21, 2014
    Posts:
    10

    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. -ShadowHunter-

    -ShadowHunter-

    Joined:
    Mar 21, 2014
    Posts:
    10
    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. -ShadowHunter-

    -ShadowHunter-

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

    -ShadowHunter-

    Joined:
    Mar 21, 2014
    Posts:
    10
    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. -ShadowHunter-

    -ShadowHunter-

    Joined:
    Mar 21, 2014
    Posts:
    10
    Thanks to all for the help