JSON-RPC over HTTP?

  Thread Rating:
  • 1 Votes - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Post Reply
Montellese Online
Team-XBMC Developer
Posts: 3,844
Joined: Jan 2009
Reputation: 36
Location: Switzerland
Post: #16
The "address" parameter of the class I posted takes the IP of your computer running XBMC. If you run your script on the same computer as XBMC this is "127.0.0.1". The port is either the HTTP or TCP port (depending on what you chose as protocol). The HTTP port is configurable in XBMCs network settings. The TCP port is set to 9090 and cannot be changed atm.
All available methods can be retrieved by calling the "JSONRPC.Introspect" method which returns a list of all available methods and some comments about how to use them.

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]
find quote
TheyKilledKenny Offline
Junior Member
Posts: 2
Joined: Jan 2011
Reputation: 0
Post: #17
Hi!
I need to develop a custom interface with Flash and actionscript 2 (not AS3), but I am not able to figure it out how to send the json request to xbmc.

With loadVars.load() I can open an http connection to the server and, if the server need no other parameter, i can receive the json string and cast it into my object.
XBMC server needs to receive a json object with the call to obtain an answer.

With loadVars.sendAndLoad() I can send variables to the server in post mode, but I need to know the variable name that the xbmc server is expecting from my client.
All your examples stream the string (byte array) directly to the server immediately after opening a connection, but no variable name is specified.

Following you can see the 2 ways i tryed yet, obtaining only a -32700 parse error message, someone can help me with this?

Code:
var snd:LoadVars = new LoadVars();
var rcv:LoadVars = new LoadVars();

rcv.onData = function(thedata) { trace("Data Received: " + thedata); }

snd.jsonrpc = '{"jsonrpc": "2.0", "method": "JSONRPC.Introspect", "id": "1"}'
snd.sendAndLoad("http://xbmc.ip.address/jsonrpc", rcv, "POST");
and
Code:
var snd:LoadVars = new LoadVars();
var rcv:LoadVars = new LoadVars();

rcv.onData = function(thedata) { trace("Data Received: " + thedata); }

snd.jsonrpc = "2.0";
snd.method = "JSONRPC.Introspect";
snd.id = "1";
snd.sendAndLoad("http://xbmc.ip.address/jsonrpc", rcv, "POST");

I'am not able to use the tcp socket because AS2 needs a null char to terminate the string transmision and xbmc seems not sending a null char at the endo of the json strings.

Sorry for bad English!

Please help.

Thank you.

Bye!
(This post was last modified: 2011-01-07 13:23 by TheyKilledKenny.)
find quote
Montellese Online
Team-XBMC Developer
Posts: 3,844
Joined: Jan 2009
Reputation: 36
Location: Switzerland
Post: #18
TheyKilledKenny Wrote:All your examples stream the string (byte array) directly to the server immediately after opening a connection, but no variable name is specified.

What do you mean by "variable name"? I have no idea about Flash or ActionScript so I can't help you there but if you could take a look into XBMCs log file you should see an entry for the json parse error where you can see the exact string that XBMC received and couldn't parse. That might provide helpful information about what is going wrong.

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]
find quote
TheyKilledKenny Offline
Junior Member
Posts: 2
Joined: Jan 2011
Reputation: 0
Post: #19
Thanks for your reply.

By "variable name" I mean the name of the field that contains the data I have to send in post.

Like a html form for example:
Code:
<form method="POST" action="http://my-server.address/page.php">
   <input type="text" name="phone" value="1234"/>
   <input type="text" name="address" value="my street"/>
</form>

So I have to find the field name containing the json stiring to send to xbmc, like:
Code:
<form method="POST" action="http://xbmx.ip.address/jsonrpc">
   <input type="text" name="jsonrpc???" value='{"jsonrpc": "2.0", "method": "JSONRPC.Introspect", "id": "1"}'/>
</form>

I hope is more clear now.

Thanks.
Bye.
find quote
Montellese Online
Team-XBMC Developer
Posts: 3,844
Joined: Jan 2009
Reputation: 36
Location: Switzerland
Post: #20
There is no such thing. The json rpc request (everything between { and }) should be the only thing in the whole HTTP POST message.

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]
find quote
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-Kodi Developer
Posts: 4,176
Joined: Dec 2007
Reputation: 13
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-Kodi Developer
Posts: 4,176
Joined: Dec 2007
Reputation: 13
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