質問

I am trying to figure out how to make use of AFNetworking 2.0. I am trying to POST a login with a username and password.

This is my current attempt but something is not working as it does not get sent.

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *parameters = @{@"loginName": @"password"};
[manager POST:@"http://xxxx.com/login" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"JSON: %@", responseObject);



    NSLog(@"Data saved");

    MainViewController *mainView = [[MainViewController alloc] initWithNibName:@"MainViewController" bundle:nil];
    mainView.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self presentViewController: mainView animated:YES completion:nil];


} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
    NSString *error_msg = (NSString *) error;
    [self alertStatus:error_msg :@"Sign in Failed!" :0];
}];

    }
} @catch (NSException * e) {
         NSLog(@"Exception: %@", e);
         [self alertStatus:@"Sign in Failed." :@"Error!" :0];
    }

However nothing is happening, just keep getting this printout:

2014-04-16 20:14:09.903 Slidedrawer[9279:60b] Error: Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0x8e78bd0 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}

The response will be in JSON. Anyone know what I need to edit in the code or how to fix it?

JSON: {
    "_id" = 533cb1c453769a02008c2d55;
    name = dddd;
    picture = "/img/users/default.jpg";
}

How do I access the above returned JSON, trying to pick apart each returned property to a string or integer etc...

役に立ちましたか?

解決

Try this:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *params = @{@"userName": username, @"password": password};
manager.responseSerializer = [AFJSONResponseSerializer serializer]; // if response JSON format
[manager POST:@"http://asd.com/login.php" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"%@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"%@", error);
}];

For parsing the response use this the responseObject as NSDictionary:

NSString *userName = [responseObject objectForKey:@"userName"];
NSString *password = [responseObject objectForKey:@"password"];

Hope this helps.. :)

他のヒント

You may find Send Nested JSON using AFNetworking for the answer.

The error Error Domain=NSCocoaErrorDomain Code=3840 is received invalid JSON object from the server. It is probably caused by the server's improper handling your missing parameters . Your parameters dictionary has only one key object loginName.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top