0

Some days ago I tryid to read and parse data from server. But today it doesn't work. What can be wrong? Here I out my method:

 -(void) getDataFromServer
 {
     act = @"linkinginit";
     deviceid = @"1xyhgjs";
     fullRequest = [NSString stringWithFormat:@"http://app.ivson.by/?act=%@&deviceid=%@",    act, deviceid];
    NSLog(@"%@", fullRequest);
    NSURL *url = [NSURL URLWithString:fullRequest];
    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];

    NSData *returnData = [NSURLConnection sendSynchronousRequest: theRequest returningResponse: nil error: nil];


  NSString *strData = [[NSString alloc]initWithData:returnData encoding:NSUTF8StringEncoding];
  NSRange   searchedRange = NSMakeRange(0, [strData length]);
  NSLog(@"Data from server = %@", strData);
  NSString *json = strData;
    //   NSString *json = @"{\"code\":200,\"serviceID\":\"53d22b10e46a5\",\"sender\":1,\"hasPair\":0}";
   NSData *jsonData = [json dataUsingEncoding:NSUTF8StringEncoding];
   NSError *error = nil;
   id obj = [NSJSONSerialization JSONObjectWithData:jsonData
                                         options:0
                                           error:&error];
if (obj) {
    NSAssert([obj isKindOfClass:[NSDictionary class]], @"Expected a dictionary");
    NSDictionary *dictObj = (NSDictionary *)obj;
    NSNumber *code = dictObj[@"code"];
    NSLog(@"code:", code);
    NSString *serviceId = dictObj[@"serviceID"];
    NSLog(@"serviceId:", serviceId);
    NSNumber *sender = dictObj[@"sender"];
    NSLog(@"sender:",sender);
    NSNumber *hasPair = dictObj[@"hasPair"];
    NSLog(@"hasPair:", hasPair);

} else {
    NSLog(@"Failed to parse JSON: %@", [error localizedDescription]);
}

}

And output is like this. I need to have all variables(code, serviceId, sender and hasPair) separate.

  2014-07-28 11:46:41.640 Nanny[1445:11303] http://app.ivson.by/?   act=linkinginit&deviceid=1xyhgjs
 2014-07-28 11:46:41.923 Nanny[1445:15b03] ADDRESPONSE - ADDING TO MEMORY ONLY: http://app.ivson.by/?act=linkinginit&deviceid=1xyhgjs
 2014-07-28 11:46:41.924 Nanny[1445:11303] Data from server = {"code":200,"serviceid":"53d60c7cc35f1","sender":1,"hasPair":0}
 2014-07-28 11:46:41.924 Nanny[1445:11303] code:
 2014-07-28 11:46:41.925 Nanny[1445:11303] serviceId:
 2014-07-28 11:46:41.925 Nanny[1445:11303] sender:
 2014-07-28 11:46:41.925 Nanny[1445:11303] hasPair:

Thank you.

3
  • Why are you converting data -> string -> data? Commented Jul 28, 2014 at 8:58
  • I'm new to Objective c. Is it easy way? Thanks. Commented Jul 28, 2014 at 9:01
  • Nothing to do with being new to Objective C. You have a method that gives you NSData. You have another method that wants NSData. So why do you convert NSData to NSString and back to NSData? Commented Jul 28, 2014 at 9:15

1 Answer 1

1

in NSLog, you're not passing type of arguments. Try this

if (obj) {
    NSAssert([obj isKindOfClass:[NSDictionary class]], @"Expected a dictionary");
    NSNumber *code = obj[@"code"];
    NSLog(@"code: %@", code);
    NSString *serviceId = obj[@"serviceID"];
    NSLog(@"serviceId: %@", serviceId);
    NSNumber *sender = obj[@"sender"];
    NSLog(@"sender: %@",sender);
    NSNumber *hasPair = obj[@"hasPair"];
    NSLog(@"hasPair: %@", hasPair);

} else {
    NSLog(@"Failed to parse JSON: %@", [error localizedDescription]);
}
Sign up to request clarification or add additional context in comments.

11 Comments

Rather than "not passing type of arguments", OP doesn't use format string at all, just expecting NSLog to print string representations of all parameters.
No, it doesn't work with field serviceID:Data from server = {"code":200,"serviceid":"53d611eb0a6cb","sender":1,"hasPair":0} 2014-07-28 12:09:52.243 Nanny[1598:11303] code: 119505712 2014-07-28 12:09:52.244 Nanny[1598:11303] serviceId: (null) 2014-07-28 12:09:52.244 Nanny[1598:11303] sender: 119080880 2014-07-28 12:09:52.244 Nanny[1598:11303] hasPair: 119112448
just updated my answer check it.. It's working fine for all NSLog
It don't work too( Data from server = {"code":200,"serviceid":"53d614fd3b335","sender":1,"hasPair":0} 2014-07-28 code: 200 2014-07-28 serviceId: (null) 2014-07-28 sender: 1 2014-07-28 hasPair: 0
but on my side It's work fine, Just simply copy my code and replace on your matching code.. Because you are using "dictObj" and "dictObj" is empty dictionary .. Try my code .. I have tested this on xcode..
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.