2014-06-21, 23:34
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!?
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.
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!?
Code:
.
.
.
[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;
}
else
{
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.