Domanda

Ho creato una sottoclasse di AFHTTPClient e sto cercando di inviare alcuni parametri JSON a un server.

Tuttavia il server risponde con un tipo di contenuto previsto

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

Secondo FAQ afnetworking

.

Se si utilizza AfhttpClClient, impostare la proprietà di parametrizzazione su AFJSONPameterCodifica.Qualsiasi metodo su quel client HTTP con un argomento Parametri ora codifica l'oggetto superato in una stringa JSON e impostare il corpo HTTP e l'intestazione del tipo di contenuto in modo appropriato.

L'ho fatto qui ma il server sembra non riconoscere le intestazioni dei contenuti.Qualcuno sa di una soluzione potenziale?

Ecco il metodo:

- (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");
}
.

È stato utile?

Soluzione

Il problema era con la formattazione dell'URL.Non ho notato un dettaglio di implementazione API che ha reso necessario l'invio di parametri di query e anche specificando la produzione JSON nell'URI.

Non c'erano problemi per quanto riguarda l'afnetworking.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top