質問

My client webservice send me a result like this:

{"login":{"EMAIL":"none","ID":"none","NOME":"none"}}

So, in AFN doesn't work.

But, if have one more result works:

{"login":[{"EMAIL":"none","ID":"none","NOME":"none"},{"EMAIL":"none","ID":"none","NOME":"none"}]}

My code:

NSDictionary *paramLogin = [NSDictionary dictionaryWithObjectsAndKeys:_txtEmail.text, @"email",_txtSenha.text, @"senha", nil];

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

[manager GET:@"http://webservice.info" parameters:paramLogin success:^(AFHTTPRequestOperation *operation, id responseObject) {

    NSLog(@"%@" , responseObject );

    for (NSDictionary *retLogin in [responseObject valueForKeyPath:@"login"]) {

        nome  = [retLogin objectForKey:@"nome"];
        email = [retLogin objectForKey:@"email"];


    }

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {

    NSLog(@"Error: %@", error);

}];

why it is like this? or what I've to do ?

役に立ちましたか?

解決

Sometimes [responseObject valueForKeyPath:@"login"] returns and array, sometimes it returns a dictionary. You need to test for this.

id loginValue = [responseObject valueForKeyPath:@"login"];

if ([loginValue isKindOfClass:[NSDictionary class]]) {
    nome  = [loginValue objectForKey:@"nome"];
    email = [loginValue objectForKey:@"email"];
} else if ([loginValue isKindOfClass:[NSArray class]]) {
    for (NSDictionary *retLogin in [responseObject valueForKeyPath:@"login"]) {

        nome  = [retLogin objectForKey:@"nome"];
        email = [retLogin objectForKey:@"email"];


    }
} else {
    // ERROR: Unexpected value
}

When you have 1 value, then loginValue is an NSDictionary. It contains {"EMAIL":"none","ID":"none","NOME":"none"}.

When you have more than 1 value, then loginValue is an NSArray. The array contains [<NSDictionary>, <NSDictionary>]. Each of of these dictionaries contains {"EMAIL":"none","ID":"none","NOME":"none"}.

他のヒント

Problem is with your json data structure. It's not consistent.

{"login":{"EMAIL":"none","ID":"none","NOME":"none"}} 

Here [responseObject valueForKeyPath:@"login"] is a single NSDictionary object.

But here,

{"login":[{"EMAIL":"none","ID":"none","NOME":"none"},{"EMAIL":"none","ID":"none","NOME":"none"}]}

Here [responseObject valueForKeyPath:@"login"] is an NSArray. So your fast enumeration works.

Best solution is to ask your webservice developer to send an array all the time, even 'login' has a single object. so it should look like this,

{"login": [{"EMAIL":"none","ID":"none","NOME":"none"}]}  //notice square brackets

Otherwise you have to modify your code to check for an NSDictionary instead of array when there's only one object.

I suspect the issue is that you aren't retaining the AFHTTPRequestOperationManager object.

Assuming this code is in something like viewDidAppear:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:...];

Then manager will be destroyed before it has a chance to complete.

Instead add a property and store the manager object in that:

@interface MyViewController ()
@property (nonatomic) AFHTTPRequestOperationManager *manager;
@end

and use:

self.manager = [AFHTTPRequestOperationManager manager];
[self.manager GET:...];

if you are getting response like that than use below code

NSMutableArray *category = [[NSMutableArray alloc]init];
category = [responseObject objectForKey:@"login"];

 for(int i = 0; i < [category count]; i++)
 {
    NSDictionary *dic = [category objectAtIndex:i]; 
    nome  = [dic objectForKey:@"nome"];
    email = [dic objectForKey:@"email"];
 }
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top