Question

J'ai créé une sous-classe de AFHTTPClient et essaye d'envoyer des paramètres JSON à un serveur.

Cependant, le serveur répond à un type de contenu attendu

{(
    "text/json",
    "application/json",
    "text/javascript"
)}, got application/xml

Selon FAQ Afnetworking

Si vous utilisez AFHTTPCLIPT, définissez la propriété Paramétecoding sur AFJSONPARAMETERCODINGING.Toute méthode sur ce client HTTP avec un argument de paramètres encodera maintenant l'objet transcuté dans une chaîne JSON et définit correctement l'en-tête HTTP Corps et Type de contenu.

J'ai fait cela ici, mais le serveur semble ne pas reconnaître les en-têtes de contenu.Est-ce que quelqu'un connaît une solution potentielle?

Voici la méthode:

- (void)getCompanyDataWithString:(NSString*)companySearchQuery 
      finish:(LBMarkitAPIRequestCompletionBlock)finishBlock
{
    [self registerHTTPOperationClass:[AFJSONRequestOperation class]];
    [self setParameterEncoding:AFJSONParameterEncoding];

    NSDictionary *params = [NSDictionary dictionaryWithObject:
        companySearchQuery forKey:@"input"];
    NSMutableURLRequest *searchQueryRequest = [self requestWithMethod:@"GET"
        path:kMarkitCompanyURL parameters:params];

    AFJSONRequestOperation *searchRequestOperation = [AFJSONRequestOperation 
        JSONRequestOperationWithRequest:searchQueryRequest 
        success:^(NSURLRequest *request, NSHTTPURLResponse *response, id json) 
        {
            NSLog(@"Response: %@", response);
            NSLog(@"JSON: %@",json);
            NSMutableArray *results = [NSMutableArray array];

            NSError *anError = [[NSError alloc] init];
            if ([json objectForKey:@"Message"]) 
            {
                NSString *message = [json objectForKey:@"Message"];
                anError = [[NSError alloc] initWithDomain:message
                                                     code:100 
                                                 userInfo:nil];
            }

            // Need some error handling code here
            for (id item in json) 
            {
                NSString *aName = [item objectForKey:@"Name"];
                NSString *aSymbol = [item objectForKey:@"Symbol"];
                NSString *anExchange = [item objectForKey:@"Exchange"];

                LBCompany *aCompany = [[LBCompany alloc] initWithName:aName 
                    Symbol:aSymbol Exchange:anExchange];
                [results addObject:aCompany];
            }
            // Need to run the passed in block after JSON 
            // Request Operation succeeds

            finishBlock(results,anError);
          }
        failure:^(NSURLRequest *request, NSHTTPURLResponse *response, 
            NSError *error, id JSON)
        {
            NSLog(@"request failed: %@",[error localizedDescription]);
            NSLog(@"Response: %@",response);
            NSLog(@"JSON: %@",JSON);
        }];

    [searchRequestOperation start];
    NSLog(@"JSON operation started");
}

Était-ce utile?

La solution

Le problème était avec le formatage de l'URL.Je n'ai pas remarqué un détail d'implémentation de l'API qui a apporté l'envoi de paramètres de requête nécessaires et spécifiant également la sortie JSON dans l'URI.

Il n'y a pas eu de problèmes concernant l'Afnetworking.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top