how to create simple Http request via AFNetworking

江枫思渺然 提交于 2019-12-04 02:56:33

I have finally found the solution as follows -

 AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];


  NSDictionary *parameters = @{@"UserId": @"24",@"Name":@"Robin"};

  NSLog(@"%@",parameters);
  parameters = nil;

    // if you want to sent parameters you can use above code 

    manager.requestSerializer = [AFJSONRequestSerializer serializer];

    [manager POST:@"http://maps.google.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=false" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject)
    {

        NSLog(@"JSON: %@", responseObject);


   }failure:^(AFHTTPRequestOperation *operation, NSError *error)
   {
        NSLog(@"Error: %@", error);
   }];

For text/Html + if it does not provide correct JSON String you can remove it from string and convert it to array or dictionary.

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

        // if you want to sent parameters you can use above code
        manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
     // header("Content-Type: application/json");
    //    manager.requestSerializer = [AFJSONRequestSerializer serializer];

        manager.responseSerializer = [AFHTTPResponseSerializer serializer];


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

            NSLog(@"responseObject %@",responseObject);

            NSString *jsonString =  [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];

            NSString *newJsonString = [jsonString stringByReplacingOccurrencesOfString:@"\\'" withString:@""];

/*
NSRange range = [jsonString rangeOfString:@"}" options:NSBackwardsSearch];
jsonString = [jsonString substringToIndex:range.location + 1];
*/
            NSData *data = [newJsonString dataUsingEncoding:NSUTF8StringEncoding];

            NSError *error;
            NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];

            NSLog(@"array %@",array);


            if (!array) {
                NSLog(@"Parsing JSON failed: %@", error);
            }

            /*
             NSData *newJSONData = [newJsonString dataUsingEncoding:NSUTF8StringEncoding];
             NSDictionary* json = [NSJSONSerialization
             JSONObjectWithData:newJSONData
             options:NSJSONReadingMutableContainers
             error:&error];
             NSLog(@"json %@",json);
            */

            NSLog(@"responseObject = %@", [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]);


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

            NSLog(@"%@",[error description]);

        }];

In some cases you need to change response dictionary/array - but sometimes all fragments of an object are not mutable.
In order to do that do as follows.

For Dictionary

 NSError *error;

                NSData *dataFromDict = [NSJSONSerialization dataWithJSONObject:responce options:NSJSONWritingPrettyPrinted error:&error];

                responseDictionary = [[NSMutableDictionary alloc]init];

                responseDictionary = [NSJSONSerialization JSONObjectWithData:dataFromDict options:NSJSONReadingMutableContainers error:&error];

For Array

NSError *error;

                NSData *dataFromDict = [NSJSONSerialization dataWithJSONObject:responce options:NSJSONWritingPrettyPrinted error:&error];

                responseArray = [[NSMutableDictionary alloc]init];

                responseArray = [NSJSONSerialization JSONObjectWithData:dataFromDict options:NSJSONReadingMutableContainers error:&error];
Codo

You seem to have an ASP.NET Web API service on the server side. It returns XML by default.

You have two options:

  1. Change the configuration of the web service as explained in How do I get ASP.NET Web API to return JSON instead of XML using Chrome?

  2. Send the HTTP header Accept: application/json along with your request.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!