Page 1 of 2

HTTP call from evothings client

Posted: 20:21, 31 Oct 2015
by andersjj
Is there a way setup to make http calls from the evothings client? I'm looking to log BLE devices that are found, but I keep getting errors. I'm guessing that the pre-compiled viewer app doesn't have a whitelist to allow all http calls? I'm running the Android version.

If there is a way to make http calls from the pre-built Android client, please post some code sample and I could take it from there.

Re: HTTP call from evothings client

Posted: 10:17, 02 Nov 2015
by Fredrik
Our example "hue-lights" does a bunch of HTTP calls using jQuery.

Of note is that all Hue servers send the header "Access-Control-Allow-Origin: *". Your server must also do this, or the client's webview will not accept the responses.

Re: HTTP call from evothings client

Posted: 10:55, 26 Nov 2015
by JimB
I think I have a related issue. I want to use a remote JSON file (long term is to call an API returning JSON) but it doesn't work. I believe I have set up the CORS correctly but I get nothing back. Would the API be more likely to succeed, or just the same?

Re: HTTP call from evothings client

Posted: 15:27, 26 Nov 2015
by alex
Hi!

If you like to, send us a working code snipped that connects the way you'd want, in order
to test in the Viewer app. We've recently incorporated a HTTP cordova plug-in an experimental build,
that I'd like to try out.

best

Alex

Re: HTTP call from evothings client

Posted: 19:16, 26 Nov 2015
by JimB
Will do! It's a home project so I'll post on here this evening (NZ time). I'm very much a rookie, despite my age.

Re: HTTP call from evothings client

Posted: 23:56, 26 Nov 2015
by alex
Alternatively, if you have an android device, you can test this extended build (regular Viewer + http plugin)
https://evothings.com/uploads/evothings2/beta2/EvothingsViewer_151120_1346.apk

In order to use the HTTP plugin, you need to invoke the native HTTP GET function in a fashion separate to how to normally pick up data via js:

cordovaHTTP.get("http://somewhere.com/some-path/some-doc", {}, {}, function(response)
{
app.status("HTTP success "+response.status);
app.onMessageArrived(response.data);
}, function(response) {
console.log(JSON.stringify(response));
});

more here: https://github.com/wymsee/cordova-HTTP

Re: HTTP call from evothings client

Posted: 09:28, 27 Nov 2015
by JimB
I used the extended build but got an error trying to run that code:
LOG: [ERR] Uncaught ReferenceError: cordovaHTTP is not defined

This is the code I'm trying. It works if the JSON file is local,but not otherwise. Any (polite) advice gratefully received.
<!doctype html>
<html>
<head>

<title>How to Parse a JSON file using jQuery</title>

<style>
body{
text-align: center;
font-family: arial;
}

.button{
margin:20px;
font-size:16px;
font-weight: bold;
padding:5px 10px;
}
</style>


</head>
<body>
<a href="http://giveandgo.nz.s3-website-ap-southeast-2.amazonaws.com/location.json">Open JSON file</a><br />
<input type="button" value="Get and parse JSON" class="button" />
<br />
<span id="results">
<select class="tabs">
</select>
</span>

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

<script>

//When DOM loaded we attach click event to button
$(document).ready(function() {

//after button is clicked we download the data
$('.button').click(function(){

//start ajax request
$.ajax({
url: "http://giveandgo.nz.s3-website-ap-southeast-2.amazonaws.com/location.json",
//force to handle it as text
dataType: "json",
success: function(data) {
//data downloaded so we call parseJSON function
//and pass downloaded data
for (var i = 0, len = data.locationlist.location.length; i < len; i++) {
//You can now access individual properties like this:
//var json = $.parseJSON(data.locationlist.location[i]);
if (i==0){var output='';}
$('#results select').append('<option value=' + data.locationlist.location[i].locationID + '>' + data.locationlist.location[i].name + '</option>');
}
}
});
});
});
</script>

</body>
</html>

Re: HTTP call from evothings client

Posted: 09:44, 27 Nov 2015
by Fredrik
Add this to your <head>:

Code: Select all

<script src="cordova.js"></script>

Re: HTTP call from evothings client

Posted: 10:56, 27 Nov 2015
by JimB
I'll have to do some rewriting to make that go. But it appears my issue (at least in part) is that the Origin header isn't being passed and I'm not sure how to make it.

Thank you.

Re: HTTP call from evothings client

Posted: 11:15, 27 Nov 2015
by Fredrik
Looks like you're using Amazon S3. They have a guide for enabling CORS.