-3

I'm trying to run a get request using the iOS simulator but I'm not getting any response from the server and the data is not getting sent.

NSString *theGetURL = [NSString stringWithFormat:@"http://10.2.176.104:9000/recorddata?id=ios&timestamp=%@%%20%@,%%20%@%%20%@&cpu=%@&memory=%@&battery=%@", monthString, dateString, yearString, timeString, self.proData, self.memData,self.batData];

As you can see, I have several local and global variables in the string, along with some percent symbols which I'm escaping with '%'. When I copy and paste this string into Safari in my Simulator, the data gets added to my DB.

I have tried the following code to send the get request via my app:

NSMutableURLRequest *newRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:theGetURL]
                                                           cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData
                                                       timeoutInterval:10];

[newRequest setHTTPMethod: @"GET"];

NSError *requestError;
NSURLResponse *urlResponse = nil;


NSData *response1 = [NSURLConnection sendSynchronousRequest:newRequest returningResponse:&urlResponse error:&requestError];
NSLog(@"response=%@",response1);

This code compiles and runs fine, but does not work. My DB is still empty.

Any idea what I'm doing wrong?

danb
  • 25
  • 6
  • Give a try , change timeoutInterval to 60. And also try to print the error message if you are not getting any response to get better understanding. – sateesh Feb 16 '15 at 12:08
  • @sateesh Thanks for your reply. I'm not getting an error message of any sort. Also, it should be noted that this method gets called every second. Is that a problem? – danb Feb 16 '15 at 12:17
  • @sateesh I tried increasing the timeout and removing the 'every second repeater' but the same result. – danb Feb 16 '15 at 12:22
  • 1
    Print out `theGetURL` and make sure it's legit. And fer cryin' out loud check for errors -- you're never going to see an error if you don't look for one. – Hot Licks Feb 16 '15 at 12:43

2 Answers2

0

try this

  NSString * theGetURL = @"http://www.google.co.in";
    NSMutableURLRequest *newRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:theGetURL]
                                                              cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData
                                                          timeoutInterval:10];



      [newRequest setHTTPMethod: @"GET"];

        NSError *requestError;
        NSURLResponse *urlResponse;


        NSData *response1 = [NSURLConnection sendSynchronousRequest:newRequest returningResponse:&urlResponse error:&requestError];
        NSString* newStr = [[NSString alloc] initWithData:response1 encoding:NSUTF8StringEncoding];

        NSLog(@"error = %@",requestError.localizedDescription);
        NSLog(@"response=%@",newStr);
        NSLog(@"url response = %@",urlResponse);
sateesh
  • 505
  • 4
  • 18
  • Thanks for this. I'm getting the error: `unsupported URL` and the others are null. I'm guessing something is wrong with my url? But it works fine when just run on the browser. – danb Feb 16 '15 at 12:35
  • Thanks for all your help! I found the answer. Turned out to be a space in my URL. – danb Feb 16 '15 at 12:48
0

Turns out, one of the strings in my URL had a space. It all worked fine after the space was removed.

danb
  • 25
  • 6