JSON-RPC over HTTP?

  Thread Rating:
  • 1 Votes - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Post Reply
ArcticGiraffe Offline
Junior Member
Posts: 3
Joined: Aug 2011
Reputation: 0
Post: #21
Hello,

I apologise in advance if I ask any stupid questions - you might gather from the number of posts I've made on this forum that I'm a bit new to all this ;-)

I feel that I'm close to knowing what I need to do having read this thread ... can someone confirm - or prod me in the right direction if not?

I want my XBMC (on linux) to start playing my favourite radio station when I press a shortcut button on my mobile phone.

From what I read on this thread I have to:
1. Make sure my (linux) XBMC server side is all set up and ready to receive JSON RPC

2. Set up a server side streaming file for XBMC to play - containing the web address of the radio stream. I've called it ~/arctic/antenne_bayern.strm Smile

3. Now - this is the bit I'm still a bit unsure about how to do ...
I want to initiate a command to my XBMC from my phone. I don't want to use a remote control app - I want to have a single shortcut icon that I only have to tap once from my home screen - or trigger by any number of other amusing ways using a great app called tasker that I found Smile.

It seems there are plenty of ways of getting a message to an XBMC server - but can I initiate a POST request simply by the act of loading a webpage? By invoking a javascript function when the page loads?

Can this POST message people are talking about above be kicked off from a bit of javascript - or ajax? If this is true then all I have to do is make a shortcut to the webpage on the home screen of my phone - hit the shortcut and off it goes Smile then I don't need to mess about trying to find mobile versions of various script interpreters ...

3a. So assuming all that is possible: Will a phone browser be able to deal with AJAX in an html file? Will my XBMC server want a username and password first? Will that stop this from working?
What do I need to write in my javascript function to get XBMC to start playing my file?

Here is my guess having read this thread and your wiki - apologies if it seems obvious to everyone else, but I'm new Smile any help greatly appreciated.

Code:
<html>
<head>
  <script>
function XbmcPlayFile()
{
    var data = '{"jsonrpc": "2.0", "method": "XBMC.Play", "params": { "file": "/home/arctic/antenne_bayern.strm"}, "id": 1}' http://myIPaddress:9019/jsonrpc;

    var xhr;
    xhr = new XMLHttpRequest(); // for an Opera like mobile browser?
  
    xhr.onreadystatechange = function()
    {
        // do something with xhr.responseText;
    };

    xhr.open("POST", "ajax-post-response-text.php", true, myLogin, myPassword);
    // line below is for a form - do I bother with this - what's it need to be?
    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");                  
    xhr.send(data);}
  </script>
</head>
                
<body>
  <script type="text/javascript" language="JavaScript">
    XbmcPlayFile();
  </script>
</body>
</html>

So - there is my first hair-brained scheme - does this look like it might work? or is there an easier way to fire off a POST request from a smart phone? Bearing in mind that I need it to be a one shot execution - so I can play with how I kick it off later!
find quote
topfs2 Offline
Team-XBMC Developer
Posts: 3,825
Joined: Dec 2007
Reputation: 8
Post: #22
Check the default interface ( https://github.com/xbmc/xbmc/tree/master...ce.default ), it does exactly what you want. Your data you send looks a bit wrong (you don't need the url part of it)

EDIT: My initial json-rpc webinterface might be simpler to understand https://github.com/xbmc/xbmc/tree/c6771d...oc_jsonrpc In that revision it didn't handle play but you could probably derive that from the code (or continue further after that revision until it did support it)

If you have problems please read this before posting

Always read the XBMC online-manual, FAQ and search the forum before posting.
Do not e-mail XBMC-Team members directly asking for support. Read/follow the forum rules.
For troubleshooting and bug reporting please make sure you read this first.


[Image: badge.gif]

"Well Im gonna download the code and look at it a bit but I'm certainly not a really good C/C++ programer but I'd help as much as I can, I mostly write in C#."
(This post was last modified: 2011-08-13 10:46 by topfs2.)
find quote
ArcticGiraffe Offline
Junior Member
Posts: 3
Joined: Aug 2011
Reputation: 0
Post: #23
Thanks for the pointers Smile I took inspiration from your json-rpc webinterface and created this:

[HTML]<html>
<body>
<h1>Play Test</h1>
<script type="text/javascript">
var http_request = new XMLHttpRequest();
http_request.open( "POST", "jsonrpc", false );
http_request.send("{\"jsonrpc\": \"2.0\", \"method\": \"XBMC.Play\", \"params\": { \"file\": \"/home/arctic/8 - Sullivan Street.m4a\"}, \"id\": 1}");
</script>
</body>
</html>[/HTML]
I found this code works when I put it in a file called index.html, and stash that in a new webinterface addon directory: ~/.xbmc/addons/webinterface.example

It also needed an addon.xml in the webinterface.example directory:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<addon
  id="webinterface.example"
  version="0.1"
  name="example"
  provider-name="test">
  <requires>
    <import addon="xbmc.gui" version="2.11"/>
  </requires>
  <extension
    point="xbmc.gui.webinterface"/>
  <extension point="xbmc.addon.metadata">
    <summary>test</summary>
    <description>test</description>
    <platform>all</platform>
  </extension>
</addon>
I then had to configure my XBMC to use my new 'example' web interface and I could then use my phone to surf to my XBMC webpage. As soon as I opened it the file started playing.

That's great, but not quite what I want ...

I want to send a JSON command to XBMC from my phone. I want the code that does that to be on my phone too - not set as the web interface for XBMC (because that would be embarrassing :o).

I tried changing my open command from above to:
[HTML]http_request.open( "POST", "http://10.0.0.5:8080/jsonrpc", true, "username", "password" );
[/HTML]

Then tried running the html file directly - but this failed, though it still worked through the web interface.

I noticed while googling around a bit that I might have a problem doing that anyway:
Wikipedia Wrote:In the early development of the World Wide web, it was found possible to breach users' security by the use of JavaScript to exchange information from one web site with that from another less reputable one. All modern browsers therefore implement a same origin policy that prevents many such attacks, such as cross-site scripting. XMLHttpRequest data is subject to this security policy.

Any suggestions? Am I missing the point somehow? Should I try using jQuery like the default interface? but I'm guessing this will have the same problem for me though.

I'm mentally preparing myself to try something else ... most phones can run java apps ...
find quote
topfs2 Offline
Team-XBMC Developer
Posts: 3,825
Joined: Dec 2007
Reputation: 8
Post: #24
I would begin by trying it in chrome etc, if it works there then the js is most likely right. Then work yourself from there. Some webbrowsers on phones simply can't do javascript correctly

If you have problems please read this before posting

Always read the XBMC online-manual, FAQ and search the forum before posting.
Do not e-mail XBMC-Team members directly asking for support. Read/follow the forum rules.
For troubleshooting and bug reporting please make sure you read this first.


[Image: badge.gif]

"Well Im gonna download the code and look at it a bit but I'm certainly not a really good C/C++ programer but I'd help as much as I can, I mostly write in C#."
find quote
ArcticGiraffe Offline
Junior Member
Posts: 3
Joined: Aug 2011
Reputation: 0
Post: #25
I think the javascript is good because it works when I use it as the XBMC web interface - works on my phone too.

I have tried it on chrome on the same machine as XBMC is on, but loading the html file path directly as opposed to connecting to the XBMC web interface - this fails. I could have a go at printing the response to screen I suppose.

Could it be that security thing I mentioned above? My next plan is to try java if I can't solve it this way.
find quote
Post Reply