Code cracker
Code cracker

Reputation: 3166

JSON text did not start with array Error with AFNetworking

I am using the below code to use perform a GET request. In reverse manner am sending the params into server and data storing in the server fine. But am getting the error * JSON text did not start with array *

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        NSDictionary *params = @{@"emails": emailid,
                                 @"password": paswrd,
                                 @"gender": gende,
                                 @"firstname":name1,
                                 @"lastname":firstname1,
                                 @"dateofBirth":dob1,
                                 @"Country":count
                                 };

        [manager GET:@"http://37.187.152.236:91/EmployeeSvc.svc/AddEmployee?"
          parameters:params
             success:^(AFHTTPRequestOperation *operation, id responseObject) {
                 NSLog(@"JSON: %@", responseObject);
             } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                 NSLog(@"Error: %@", error);
             }];

This response am getting

Upvotes: 1

Views: 3712

Answers (4)

chrisallick
chrisallick

Reputation: 1358

-(void) testHTTPS {
    AFSecurityPolicy *securityPolicy = [[AFSecurityPolicy alloc] init];
    [securityPolicy setAllowInvalidCertificates:YES];

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager setSecurityPolicy:securityPolicy];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];

    [manager GET:[NSString stringWithFormat:@"%@", HOST] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSString *string = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        NSLog(@"%@", string);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
    }];
}

Upvotes: 2

Dhrumil
Dhrumil

Reputation: 3204

This problem can be due to bad JSON string. By Bad JSON string, I mean that your JSON response might not be starting with the expected characters i.e '[' or '{'. The starting characters must always be anyone of the above two.

Also there might be a chance that your JSON response is embedded in some kind of XML string. This happened to me and for me, it was just bad JSON response like I explained and I solved it by proper parsing.

Check this image. This is what bad JSON can look like.

<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">
[{"Title":"DemoTitle","CreationDate":"06/06/2014","Description":"DemoDescription"}]
</string>

As you can see, JSON is embedded in XML string.**strong text**Also, check your JSON response on this site : http://jsonlint.com/

This will show you if the JSON you receive is valid for parsing or not.

Hope this helps.

Upvotes: 1

Bobby
Bobby

Reputation: 44

You can check your serve Content-Type,If is text/html,you need code this:

 manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];

Upvotes: 1

Nitin
Nitin

Reputation: 1543

This is the server side error when your server is not responding the JSON response OR Response is not a valid JSON because this error is appears when AF networking library unable to parse the JSON response. Please check it with your server side about the response

Upvotes: 0

Related Questions