0

This is my JSON Response when i test my web service on REST CONSOLE

{
"d": "{\"token\":\"45b95300-e1ff-4bc1-b45c-9b87babc4df3\",\"timestamp\":\"09/04/2014 12:40:12 PM\"}"
}

These is my Log Details:

Server Response :<NSHTTPURLResponse: 0x8ea8b70> { URL: http://dev-jrn.stits.co:777/Authonticate.aspx/authenticate } { status code: 500, headers {
"Content-Length" = 91;
"Content-Type" = "application/json; charset=utf-8";
Date = "Thu, 04 Sep 2014 07:18:30 GMT";
Server = "Microsoft-IIS/7.5";
"X-Powered-By" = "ASP.NET";
jsonerror = true;
} }

My client side code

NSOperationQueue *queue = [[NSOperationQueue alloc] init];
        NSString *post =[[NSString alloc] initWithFormat:@"username=%@&password=%@",[self.textUserName text],[self.textPassword text]];
        NSLog(@"PostData: %@",post);

        NSURL *url=[NSURL URLWithString:@"http://dev-jrn.stits.co:777/Authonticate.aspx/authenticate"];
        NSDictionary *jsonDict = [[NSDictionary alloc] initWithObjectsAndKeys:
                                  @"username", @"abc",
                                  @"password", @"abc",
                                  nil];

        NSError *error;
        NSData *postData = [NSJSONSerialization dataWithJSONObject:jsonDict options:0 error:&error];

        NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];

        NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
        [request setURL:url];
        [request setHTTPMethod:@"POST"];
        [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
        [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
        [request setValue:@"application/json; character=utf-8" forHTTPHeaderField:@"Content-Type"];
        [request setHTTPBody:postData];

        //[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]];
        [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response,NSData *data, NSError *error){


       // NSLog(@"Response code: %ld", (long)[response statusCode]);
            if(error || !data){
               NSLog(@"Server Error : %@", error);
            }
        else
        {
            NSLog(@"Server Response :%@",response);
            NSError* error;
            NSDictionary* json = [NSJSONSerialization
                                  JSONObjectWithData:data
                                  options:kNilOptions
                                  error:&error];

            NSArray* latest = [json objectForKey:@"d"];
            NSLog(@"items: %@", latest);//Returning items: (null)
        }
        }
         ];

code is working properly but i know i m missing out something .Please Help. Thanks in advance

Asmi237
  • 33
  • 8

2 Answers2

0
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json; character=utf-8" forHTTPHeaderField:@"Content-Type"];

Replace this

[request setValue:@"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
Ganesh Kumar
  • 708
  • 6
  • 25
  • { URL: http://dev-jrn.stits.co:777/Authonticate.aspx/authenticate } { status code: 200, headers { "Cache-Control" = private; "Content-Length" = 409; "Content-Type" = "text/html; charset=utf-8"; Date = "Thu, 04 Sep 2014 09:59:31 GMT"; Server = "Microsoft-IIS/7.5"; "X-AspNet-Version" = "4.0.30319"; "X-Powered-By" = "ASP.NET"; } } 2014-09-04 18:03:41.355 LoginDemoUsingWebService[22086:3a03] items: (null) i am still not getting required after replacing can u suggest how to parse this response json array or how do i fetch – Asmi237 Sep 04 '14 at 10:08
  • in your NSLog(@"items: %@", latest) => items: (null) value.Put break point and check whether you have getting any null string values.Can you check it . – Ganesh Kumar Sep 04 '14 at 10:23
  • i have checked your code.In username and password getting null values then how will you get response. – Ganesh Kumar Sep 05 '14 at 06:14
  • yea i am getting null values.can you tell me how can i solve this issue – Asmi237 Sep 05 '14 at 06:34
  • Check those links link http://www.edumobile.org/iphone/iphone-programming-tutorials/how-to-receive-data-from-the-server-in-iphone/. http://stackoverflow.com/questions/15749486/sending-an-http-post-request-on-ios – Ganesh Kumar Sep 05 '14 at 06:55
  • thanks for your help .i tried edumobile.org/iphone/iphone-programming-tutorials/…. n m getting this output can u explain how can i get json response
    – Asmi237 Sep 05 '14 at 08:04
0

instead of this

NSDictionary *jsonDict = [[NSDictionary alloc] initWithObjectsAndKeys:
                              @"username", @"abc",
                              @"password", @"abc",
                              nil];

try

NSDictionary *jsonDict = [[NSDictionary alloc] initWithObjectsAndKeys:
                              @"abc", @"username",
                              @"dch", @"password",
                              nil];