IOS AFN Pass parameters to the server via body

Source: Internet
Author: User

    1. Tell the truth I am disgusted with this kind of wonderful writing, because of the project requirements of the wonderful and the staff to leave the big pit is no language, do not want the next or this is the pit.
    2. Import the AFN file first or pod.
Nsdictionary *parameters; Parameters= @{@"type":@"GetPath",@"Path": _path}; //set the parameters according to your server's format settingsNSString *temp = [NSString stringWithFormat:@"{\"Type\": \ "getpath\", \ "path\": \ "%@\"}", _path];NSData*data =[temp datausingencoding:nsutf8stringencoding]; NSString*requesturl = [NSString stringWithFormat:@"%@%@"The URL of the weburl_prefix,weburl_getdir];//request I copied it directly from the code so this should know how to get it afurlsessionmanager*manager =[[Afurlsessionmanager alloc] Initwithsessionconfiguration:[nsurlsessionconfiguration  Defaultsessionconfiguration]]; If you do not need to pass through the body then the parameters into the parameters inside Nsmutableurlrequest*request = [[Afhttprequestserializer serializer] Requestwithmethod:@"POST"Urlstring:requesturl Parameters:nil Error:nil]; NSLog (@"requesturl:%@", Requesturl); Request.timeoutinterval=Ten; [Request SetValue:@"Application/json"Forhttpheaderfield:@"Content-type"]; //set the body here to put the parameters into the 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,IDResponseobject,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 Pass parameters to the server via body

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.