Pregunta

He creado una subclase de AFHTTPClient y estoy tratando de enviar algunos parámetros JSON a un servidor.

Sin embargo, el servidor está respondiendo con un tipo de contenido esperado

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

Según Preguntas frecuentes de FEQ

Si está utilizando AFHTTPCLIENTE, configure la propiedad ParameTERENCODING en AFJSONPARAMETERENCODING.Cualquier método en ese cliente HTTP con un argumento de parámetros ahora codificará el objeto pasado en una cadena JSON y configurar el cuerpo HTTP y el encabezado de tipo de contenido adecuadamente.

He hecho eso aquí, pero el servidor parece no reconocer los encabezados de contenido.¿Alguien sabe de una solución potencial?

Aquí está el método:

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

¿Fue útil?

Solución

El problema fue con el formato de URL.No noté un detalle de implementación de la API que hizo que el envío de parámetros de consulta necesaria y también especificara la salida JSON en el URI.

No hubo problemas con respecto a AFnetworking.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top