使用AFNetWorking请求的一段通用代码,直接替换网址参数即可

<span style="font-size:18px;">// Request: My API (1) (http://m.app.shouyou.com/indexImage/list.json)
    
    NSURL* URL = [NSURL URLWithString:@"http://m.app.shouyou.com/indexImage/list.json"];
    NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:URL];
    request.HTTPMethod = @"POST";
    request.timeoutInterval = 30;
    
    // Request Operation
    
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    operation.responseSerializer = [AFJSONResponseSerializer serializer];
    // Body
    
    NSDictionary *parameters =@{@"input": @{@"appId":@9,@"deviceType":@2}};
    request.HTTPBody = [NSJSONSerialization dataWithJSONObject:parameters options:NSJSONWritingPrettyPrinted error:nil];
    
    // Progress & Completion blocks

    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"Success: Status Code %ld", (long)operation.response.statusCode);
        NSLog(@"%@", responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error.localizedDescription);
    }];
    
    // Connection
    
    [operation start];</span>

你可能感兴趣的:(使用AFNetWorking请求的一段通用代码,直接替换网址参数即可)