How to fetch a JSON with SparkAR networking module

荒凉一梦 提交于 2020-06-27 16:37:11

问题


I want to fetch data from an URL with SparkAR's networking module and display it.

I tried the example found in the Spark AR documentation but it doesn't do much: https://developers.facebook.com/docs/ar-studio/reference/classes/networkingmodule/

Don't forget to add "jsonplaceholder.typicode.com" to Spark AR's whitelisted domains first. :)

// Load in the required modules
const Diagnostics = require('Diagnostics');
const Networking = require('Networking');

//==============================================================================
// Create the request
//==============================================================================

// Store the URL we're sending the request to
const url = 'https://jsonplaceholder.typicode.com/posts';

// Create a request object
const request = {

  // The HTTP Method of the request
  // (https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods)
  method: 'POST',

  // The HTTP Headers of the request
  // (https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers)
  headers: {'Content-type': 'application/json; charset=UTF-8'},

  // The data to send, in string format
  body: JSON.stringify({title: 'Networking Module'})

};

//==============================================================================
// Send the request and log the results
//==============================================================================

// Send the request to the url
Networking.fetch(url, request).then(function(result) {

  // Check the status of the result
  // (https://developer.mozilla.org/en-US/docs/Web/HTTP/Status)
  if ((result.status >= 200) && (result.status < 300)) {

    // If the request was successful, chain the JSON forward
    return result.json();

  }

  // If the request was not successful, throw an error
  throw new Error('HTTP status code - ' + result.status);

}).then(function(json) {

  // Log the JSON obtained by the successful request
  Diagnostics.log('Successfully sent - ' + json.title);

}).catch(function(error) {

  // Log any errors that may have happened with the request
  Diagnostics.log('Error - ' + error.message);

});

All I get is : ">> Successfully sent - Networking Module"

Does anybody know how I could get the json content to be displayed in the console I want to store it and use it in a text object afterwards.


回答1:


In my case a have a url that give me a random item in json format.

URL: https://gabby-airbus.glitch.me/random

Result: {"item":"Item 14"}

In the code, replace the line:

 Diagnostics.log('Successfully sent - ' + json.title);  

with this:

// show json data in console
Diagnostics.log(json.item);

// Asign json data to text object
itemText.text = json.item;

First line shows the json data in console. Second line asign the json data to a text object in the scene, in this case the "itemText" text object.

Full code:

const Diagnostics = require('Diagnostics');
const Scene = require('Scene');
const Networking = require('Networking');
const URL = 'https://gabby-airbus.glitch.me/random';
var itemText = Scene.root.find('itemText');

Networking.fetch(URL).then(function(result){
    if( (result.status >=200) && (result.status < 300)){ return result.json(); } 
    else { throw new Error('HTTP Status Code: ' + result.status); }
}).then(function(json){
    // show json data in console
    Diagnostics.log(json.item);

    // Asign json data to text object
    itemText.text = json.item;
}).catch(function(error){ 
    itemText = 'Failed to start'; 
    Diagnostics.log(result.status + error); 
});


来源:https://stackoverflow.com/questions/56960033/how-to-fetch-a-json-with-sparkar-networking-module

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!