3

My app is calling a basic PHP script that outputs JSON data.

I am calling my PHP script like this:

NSString *buildURL = [NSString stringWithFormat: @"http://xxxx.com/api.php?action=authenticate_user&email=%@&password=%@&deviceToken=%@", _emailAddress, _password, deviceToken];
NSURL *url = [NSURL URLWithString: buildURL];
NSData *data = [NSData dataWithContentsOfURL:url];
NSString *ret = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

// Parse the result
NSString *ret2 = [ret stringByReplacingOccurrencesOfString:@"null" withString:@""];
NSError* error;
NSDictionary *JSON =
[NSJSONSerialization JSONObjectWithData: [ret2 dataUsingEncoding:NSUTF8StringEncoding]
                                options: NSJSONReadingMutableContainers
                                  error: &error];

NSString* status = [JSON objectForKey:@"status"];
NSString* email = [JSON objectForKey:@"email"];
NSString* userID = [JSON objectForKey:@"userID"];
NSString* agentID = [JSON objectForKey:@"agentID"];
NSString* picture = [JSON objectForKey:@"picture"];

For some reason when running this code on my iPhone all the variables are (null), but in the simulator I get the correct data.

The output of my php script is:

{"status":"1", "agentName":"Bill", "picture":"http://xxxx.com/thumb_ad1-8908968097.jpg", "departmentName":"", "email":"test@gmail.com", "agentID":"513", "userID":"3"}null

Any ideas what I'm doing wrong?

Gabriel.Massana
  • 8,165
  • 6
  • 62
  • 81
Alosyius
  • 8,771
  • 26
  • 76
  • 120
  • Why is there that 'null' bit at the end of the output of your PHP script? – Shaggy Frog Oct 28 '13 at 02:36
  • Its the php script that outputs it, but i figure it should not matter as it is working in the simulator – Alosyius Oct 28 '13 at 02:36
  • 1
    Good rule of thumb: never assume that just because something works in the simulator, it will work on a device. You should fix that first since that's not valid JSON. – Shaggy Frog Oct 28 '13 at 02:45
  • 2
    Add some NSLog statements, use the Xcode debugger. – zaph Oct 28 '13 at 03:04
  • 1
    NSLog the error for starters. Also 'deviceToken' as the last param seems like the most likely difference between simulator and device. Last, once you get this working, consider using an asynch request. – danh Oct 28 '13 at 03:25
  • Proxy the request and response. Show the code that actually makes use of the the `status`, `email`, etc. variables. – Carl Veazey Oct 28 '13 at 03:32

1 Answers1

2

I surely guess that your webservice is not live (your web service are on local server make them global).

Device is not connected with your current php server , from where you called webservice, from simulator it gives output because mac os is connected with server.

Toseef Khilji
  • 17,192
  • 12
  • 80
  • 121