search

Home  >  Q&A  >  body text

ios - 在AFNetworking上怎么用AFJSONRequestOperation来请求数据

ringa_leeringa_lee2771 days ago605

reply all(2)I'll reply

  • 伊谢尔伦

    伊谢尔伦2017-04-17 16:54:51

    I am new to iOS development, so I understand the meaning of the question very well.
    I haven’t really used it during practiceAFJSONRequestOperation. I have always used the author’s demo below to request data

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:@"http://example.com/resources.json" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"JSON: %@", responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
    }];

    reply
    0
  • 阿神

    阿神2017-04-17 16:54:51

    There is no problem in getting the operation instance from the manager. You can command+mouse and click in to see what configuration the manager has made for the operation. In the end, he added the operation to the operationQueue to start the task, instead of using start directly

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    AFHTTPRequestOperation *operation = [manager HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) {
                NSInteger statusCode = operation.response.statusCode;
                //...
            } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                NSInteger statusCode = operation.response.statusCode;
                //...
            }];
    
    [manager.operationQueue addOperation:operation];

    reply
    0
  • Cancelreply