Kodi Community Forum

Full Version: Problems Connecting to XBMC API - Request Timed Out
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi Everyone,

Here is my XCODE to connect to an XBMC Media Center:

Before I actually send any commands like (play,pause,up, down...etc). I first make sure that the XBMC is alive by pinging it using the JSON RPC.

This works perfectly about 50% of the time. For some reason, at many completely random occasions, all my commands including the below code just gives me a "Request Timed Out"! I cannot wrap my head around this. Any help!?

[self isAlive:[NSString stringWithFormat:@"{\"jsonrpc\":\"2.0\", \"id\":1,\"method\":\"JSONRPC.Ping\"}"]]
-(BOOL) isAlive:(NSString *)Command
    NSData *postData = [Command dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
    NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;
    [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://%@/jsonrpc",DevIP]]];
    [request setHTTPMethod:@"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:postData];
    [request setTimeoutInterval:0.5];
    NSError *error = [[NSError alloc] init];
    NSHTTPURLResponse *response = nil;
    NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    if ([response statusCode] >=200 && [response statusCode] <300)
        return TRUE;
        return false;

I also want to add that if I keep pinging (about 50 times), it eventually connects, and all commands sent thereafter work perfectly. This equates to about 5 seconds of constant connection retries.
What is your timeout value for the API transaction? In the included example I see "setTimeoutInterval:0.5". Is this .5 seconds?

In the XBMC webinterfaces I have written the minimum timeout value I have used is 5 seconds.
Hey Kabooga, thanks for the reply.

Instead of pinging 50 times at 0.5 sec timeout, I tried your recommendation with 5 retries at a 5 sec timeout each. Unfortunately, I have the exact same results. my app does not connect to XBMC until at least 20-25 seconds (multiple retries).

Thing is, when I use other apps, like XBMC commander on IOS or Android, I never get these results, these apps connect within 2-3 seconds tops.

Any ideas, what am I doing wrong here?

I just found out something interesting. When It is not connecting my "NSHTTPURLResponse" gives me a status response 0! This must mean something is wrong with my code and definitely not with XBMC.
Let me preface this by saying I have no clue about XCODE. So any suggestions I give are that, just suggestions.

I looked at the requests being sent from my javascript and what you are doing seems correct.

One difference I do see between the headers is that I am not setting "Content-Length" (though it may get set in the javascript framework I am using). You may want to comment that line out and see what happens.
Hi, cannot load in windows xp
@Kabooga: Thanks again for the suggestion, that didn't change anything with the results I was getting. Ill continue trying out different things, I was thinking to try out a direct TCp/IP connection rather than over HTTP POST.

@harvester: windows xp very nice