IOS AFN 通過body傳遞參數給伺服器

來源:互聯網
上載者:User

標籤:handle   conf   class   pos   afn   setvalue   using   伺服器   error:   

  1. 講真我對於這種奇葩寫法是深惡痛絕,由於奇葩的項目要求和人員的跳槽留下的大坑簡直是無語,不希望下一個還是這樣被坑。
  2. 先將AFN檔案匯入 或者pod啊
    NSDictionary *parameters;    parameters = @{@"type":@"getpath",@"path":_path};    //  設定參數  根據你們伺服器的格式設定     NSString *temp = [NSString stringWithFormat:@"{\"type\":\"getpath\",\"path\":\"%@\"}",_path];    NSData *data = [temp dataUsingEncoding:NSUTF8StringEncoding];        NSString *requestUrl = [NSString stringWithFormat:@"%@%@",WEBURL_PREFIX,WEBURL_GETDIR];//請求的URL 我是直接從代碼上copy的 so 這個應該知道怎麼弄吧    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];  //如果你不需要將通過body傳 那就參數放入parameters裡面    NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] requestWithMethod:@"POST" URLString:requestUrl parameters:nil error:nil];    NSLog(@"requestURL:%@",requestUrl);    request.timeoutInterval= 10;    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];        // 設定body 在這裡將參數放入到body    [request setHTTPBody:data];        AFHTTPResponseSerializer *responseSerializer = [AFHTTPResponseSerializer serializer];    responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",                                                 @"text/html",                                                 @"text/json",                                                 @"text/javascript",                                                 @"text/plain",                                                 nil];    manager.responseSerializer = responseSerializer;    [[manager dataTaskWithRequest:request uploadProgress:nil downloadProgress:nil completionHandler:^(NSURLResponse *response,id responseObject,NSError *error){        if(responseObject!=nil){                    NSString *result = [[NSString alloc] initWithData:responseObject  encoding:NSUTF8StringEncoding];            NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];            NSString *type = [jsonDict objectForKey:@"type"];            NSString *path = [jsonDict objectForKey:@"path"];                    if ([type isEqualToString:@"getpath"]) {                            if ([path isEqualToString:_path]) {                                    _diskList = [jsonDict objectForKey:@"disk"];                    _folderList = [jsonDict objectForKey:@"folder"];                    _documentList = [jsonDict objectForKey:@"document"];                    NSLog(@"%@",@"path is correct");                }else{                    NSLog(@"%@",@"path is not correct");                }            }else{                NSLog(@"%@",@"type is not getpath");            }            [self.tableView reloadData];            NSLog(@"%@",result);        }    }]resume];

IOS AFN 通過body傳遞參數給伺服器

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.