Hi–
I am relatively new to Unity scripting, so I am sure I am making a simple error, but I can’t seem to solve my problem: I am trying to make a web request to a Particle Photon to get some data that should be returned as a JSON formatted string. If I type the URL into my browser search bar, I get back the expected result:
{
"cmd": "VarReturn",
"name": "time",
"error": null,
"result": 1130997,
"coreInfo": {
"last_app": "",
"last_heard": "2017-01-11T16:02:43.255Z",
"connected": true,
"last_handshake_at": "2017-01-11T15:43:56.371Z",
"deviceID": "XXXXXXXXXXXXXXXXXXXXXXXX",
"product_id": 6
}
}
I’ve looked at a variety of tutorials and posts, and have settled on this C# script that is attached to an empty game object in the scene:
using UnityEngine;
using System;
using System.Collections;
using UnityEngine.Networking;
public class GetPhotonVariable: MonoBehaviour {
public string deviceID;
public string accessToken;
public string variableName;
private string result;
private UnityWebRequest www;
void Start() {
InvokeRepeating("startRepeating", 1, 2.0F);
}
void startRepeating(){
StartCoroutine(GetText());
}
IEnumerator GetText() {
www = UnityWebRequest.Get("https://api.particle.io/v1/devices/" + deviceID + "/" + variableName + "?access_token=" + accessToken);
yield return www.Send();
if(www.isError) {
Debug.Log(www.error);
}
else {
// Show results as text
Debug.Log(www.downloadHandler.text);
// Or retrieve results as binary data
byte[] results = www.downloadHandler.data;
Debug.Log(Convert.ToBase64String(results));
}
}
}
The response logged to the console by Debug.Log(www.downloadHandler.text) is only:
{
"cmd": "VarReturn",
I don’t know how to get the full response.
The output of the byte array being converted to a Base64 string is:
ewogICJjbWQiOiAiVmFyUmV0dXJuIiwKICAibmFtZSI6ICJ0aW1lIiwKICAiZXJyb3IiOiBudWxsLAogICJyZXN1bHQiOiA2NjU0NzY0LAogICJjb3JlSW5mbyI6IHsKICAgICJsYXN0X2FwcCI6ICIiLAogICAgImxhc3RfaGVhcmQiOiAiMjAxNy0wMS0xMVQxNzozNDo0Ni45NDlaIiwKICAgICJjb25uZWN0ZWQiOiB0cnVlLAogICAgImxhc3RfaGFuZHNoYWtlX2F0IjogIjIwMTctMDEtMTFUMTU6NDM6NTYuMzcxWiIsCiAgICAiZGV2aWNlSUQiOiAiMmQwMDI5MDAxMzQ3MzQzNDMyMzEzMDMxIiwKICAgICJwcm9kdWN0X2lkIjogNgogIH0KfQ==
I sadly do not know what to do with this information…
Does anyone see what I’m doing wrong? Any help would be most appreciated.