-1

I'm trying to do a POST with action parameter called "#load".

To my webservice is a key need to show results: action:#load

How I can do this using AFNetworking framework? Here a sample of my wrong code:

First the constant declaration:

const NSString *BASE_URL = @"http://www.mywebservice.com/request.php";

Now the code:

-(void)requisitarEventos{
  [smartEventos removeAllObjects];

  NSString* url = [BASE_URL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

  AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
  [SVProgressHUD showWithStatus:@"Loading..." maskType:SVProgressHUDMaskTypeGradient];

  NSDictionary *parameters = [NSDictionary dictionaryWithObjectsAndKeys:@"#load",@"action", nil];

  [manager POST:url parameters:parameters
     success:^(AFHTTPRequestOperation *operation, id responseObject) {

         [self carregarEventos:responseObject];
         [SVProgressHUD dismiss];
     }
     failure:^(AFHTTPRequestOperation *operation, NSError *error) {
         NSLog(@"Error: %@", error);
         [SVProgressHUD dismiss];
     }];
 }

2 Answers2

0

I found the answer. The real problem of my code that's my Web Service doesn't give results of Content-Type in "application/json", but in "text/html".

I solve the problem using this code below:

NSMutableSet *contentTypes = [[NSMutableSet alloc] initWithSet:manager.responseSerializer.acceptableContentTypes];
[contentTypes addObject:@"text/html"];
manager.responseSerializer.acceptableContentTypes = contentTypes;

And the full code of my class:

-(void)callEvents{
[smartEvents removeAllObjects];

NSString* url = [NSString stringWithFormat:@"%@myService.php?action=load", BASE_URL];
url = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

[SVProgressHUD showWithStatus:@"Loading..." maskType:SVProgressHUDMaskTypeGradient];

manager.responseSerializer = [AFJSONResponseSerializer serializer];

NSMutableSet *contentTypes = [[NSMutableSet alloc] initWithSet:manager.responseSerializer.acceptableContentTypes];
[contentTypes addObject:@"text/html"];
manager.responseSerializer.acceptableContentTypes = contentTypes;

[manager GET:url parameters:nil
     success:^(AFHTTPRequestOperation *operation, id responseObject) {

         [self carregarEventos:responseObject];
         [SVProgressHUD dismiss];
     }
     failure:^(AFHTTPRequestOperation *operation, NSError *error) {
         NSLog(@"Error: %@", error);
         [SVProgressHUD dismiss];
     }];
}
0

please see this , I make a simple post method with callback ,

POST with URL parameters and JSON body in AFNetworking

Community
  • 1
  • 1