Simple use of afnetworking in iOS

Source: Internet
Author: User

GET:

?
12345678910111213141516171819     // 1.获得请求管理者    AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];        // 2.封装请求参数    NSMutableDictionary *params = [NSMutableDictionary dictionary];    params[@"userId"] = [appDefault objectForKey:@"userId"];    params[@"IMSI"] = _IMSI.text;    params[@"nickName"] = _nickName.text;    params[@"phoneNumber"] = _phoneNumber.text;        NSString *url = [ServiceInfo stringByAppendingString:@"http://103.238.80.57:8080/Vietnam/Application/bindingDevice"];        // 3.发送GET请求    [mgr GET:url parameters:params    success:^(AFHTTPRequestOperation *operation, id responseObject) {         NSLog(@"%@",responseObject);    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {         NSLog(@"%@",error);    }];

POST:

?
12345678910111213141516171819 // 1.获得请求管理者    AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];        // 2.封装请求参数    NSMutableDictionary *params = [NSMutableDictionary dictionary];    params[@"userId"] = [appDefault objectForKey:@"userId"];    params[@"IMSI"] = _IMSI.text;    params[@"nickName"] = _nickName.text;    params[@"phoneNumber"] = _phoneNumber.text;        NSString *url = [ServiceInfo stringByAppendingString:@"http://103.238.80.57:8080/Vietnam/Application/bindingDevice"];        // 3.发送GET请求    [mgr POST:url parameters:params    success:^(AFHTTPRequestOperation *operation, id responseObject) {         NSLog(@"%@",responseObject);    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {         NSLog(@"%@",error);    }];

It is important to note that the afnetworking default is to use the JSON format to send and respond to the returned data, if the returned data is not JSON, you need to set up the request manager, the following see the code:

?
1234567891011121314151617181920212223242526 // 1.获得请求管理者    AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];        // 因为传递过去和接收回来的数据都不是json类型的,所以在这里要设置为AFHTTPRequestSerializer和AFHTTPResponseSerializer    mgr.requestSerializer = [AFHTTPRequestSerializer serializer];// 请求    mgr.responseSerializer = [AFHTTPResponseSerializer serializer];// 响应        // 2.封装请求参数    NSMutableDictionary *params = [NSMutableDictionary dictionary];    params[@"userId"] = [appDefault objectForKey:@"userId"];    params[@"IMSI"] = _IMSI.text;    params[@"nickName"] = _nickName.text;    params[@"phoneNumber"] = _phoneNumber.text;        NSString *url = [ServiceInfo stringByAppendingString:@"http://103.238.80.57:8080/Vietnam/Application/bindingDevice"];        // 3.发送GET请求    [mgr GET:url parameters:params     success:^(AFHTTPRequestOperation *operation, id responseObject) {            // 转码,如果返回的数据不是JSON,这里必须要转码            NSString *string = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];            NSLog(@"%@",string);    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {            NSLog(@"%@",error);    }];

Simple use of afnetworking in iOS

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.