Use of "iOS development-Network" AFN

Source: Internet
Author: User

AFN's

Https://github.com/AFNetworking/AFNetworking

Here are also specific ways to use

Download the unzip and drag the Afnetworking folder into the project.

Introducing Header Files

#import "AFNetworking.h"
GET request
//Create a managerAfhttprequestoperationmanager *mgr = [Afhttprequestoperationmanager manager];//Set the data format returned by the server side, default to JSON parsing Afjsonresponseserializer Afxmlresponseserializer AfresponseserializerMgr.responseserializer = [Afjsonresponseserializer serializer]; setGetRequest the following parameter Nsmutabledictionary *params= [Nsmutabledictionary dictionary];params[@ "username"] =@ "123";params[@ "pwd"] =@ "123";//Set URLNSString *string=@ "Http://localhost:8080/TFServer/login"; [Mgr GET:stringParametersparamssuccess:^ (afhttprequestoperation *operation, id responseobject) {//Get successful after executing this block of codeNSLog (@"---%@", Responseobject); } failure:^ (Afhttprequestoperation *operation, Nserror *error) {//code failed to executeNSLog (@ "Error"); }];
POST request
//Create a managerAfhttprequestoperationmanager *mgr = [Afhttprequestoperationmanager manager];//Set the data format returned by the server side, default to JSON parsing Afjsonresponseserializer Afxmlresponseserializer AfresponseserializerMgr.responseserializer = [Afjsonresponseserializer serializer]; setGetRequest the following parameter Nsmutabledictionary *params= [Nsmutabledictionary dictionary];params[@ "username"] =@ "123";params[@ "pwd"] =@ "123";//Set URLNSString *string=@ "Http://localhost:8080/TFServer/login";//Only here to turn get into post[Mgr POST:stringParametersparamssuccess:^ (afhttprequestoperation *operation, id responseobject) {//Get successful after executing this block of codeNSLog (@"---%@", Responseobject); } failure:^ (Afhttprequestoperation *operation, Nserror *error) {//code failed to executeNSLog (@ "Error"); }];
Uploading files

New methods, used with iOS7

//Get the path to the file NSString*path = [[NSBundleMainbundle] pathforresource:@"1.jpg"OfType:Nil];//Create a request pair likeNsmutableurlrequest *request = [[Afhttprequestserializer serializer] multipartformrequestwithmethod:@"POST"urlstring:@"Http://localhost:8080/TFServer/upload"ParametersNilconstructingbodywithblock:^ (ID<AFMultipartFormData> formData) {[FormData appendpartwithfileurl:[NsurlFileurlwithpath:path] name:@"File"filename:@"Test.jpg"mimetype:@"Image/jpeg"ErrorNil]; } Error:Nil];//Create a Manager objectAfurlsessionmanager *manager = [[Afurlsessionmanager alloc] Initwithsessionconfiguration:[nsurlsessionconfiguration    Defaultsessionconfiguration]]; Nsprogress *progress =Nil;//Create a taskNsurlsessionuploadtask *uploadtask = [Manager uploadtaskwithstreamedrequest:request progress:&progress completionhandler:^ (Nsurlresponse *response,IDResponseobject,Nserror*error) {if(Error) {NSLog(@"Error:%@", error); }Else{NSLog(@"%@ %@", response, Responseobject);    }    }]; [Uploadtask resume];
Upload Method 1

Convert the uploaded file to data and upload it

    //1. Create a managerAfhttprequestoperationmanager *mgr = [Afhttprequestoperationmanager manager];//2. Package parameters (this dictionary can only put non-file parameters)    nsmutabledictionary*params = [nsmutabledictionaryDictionary]; params[@"username"] = @"123"; params[@"Age"] = @ -; params[@"PWD"] = @"456"; params[@"Height"] = @1.55;//2. Send a request    NSString*url = @"Http://localhost:8080/TFServer/upload"; [Mgr Post:url parameters:params constructingbodywithblock:^ (ID<AFMultipartFormData> formData) {UIImage*image = [UIImageimagenamed:@"1.jpg"]; NSData *filedata = uiimagejpegrepresentation (image,1.0); [FormData appendpartwithfiledata:filedata name:@"File"filename:@"Haha.jpg"mimetype:@"Image/jpeg"];//Do not use this method to set file parameters        //[FormData appendpartwithformdata:filedata name:@ "file"];} success:^ (Afhttprequestoperation *operation,IDResponseobject) {NSLog(@"Upload succeeded"); } failure:^ (Afhttprequestoperation *operation,Nserror*error) {NSLog(@"Upload failed"); }];
Upload Method 2

Get the file you want to upload directly with the URL

    //1. Create a managerAfhttprequestoperationmanager *mgr = [Afhttprequestoperationmanager manager];//2. Package parameters (this dictionary can only put non-file parameters)Nsmutabledictionary *params = [Nsmutabledictionary dictionary]; params[@"username"] = @"123"; params[@"Age"] = @ -; params[@"PWD"] = @"456"; params[@"Height"] = @1.55;//2. Send a requestNSString *url = @"Http://localhost:8080/TFServer/upload"; [Mgr Post:url parameters:params constructingbodywithblock:^ (id<afmultipartformdata> formData) {this block is automatically called before the request is sent        //need to add file parameters to Formdata in this block        /** FileURL: URL path of the file to be uploaded name: The server over there receive the file with the parameter name filename: (Tell the server) the file name of the uploaded files MimeType : The file type of the uploaded file */Nsurl *url = [[NSBundle Mainbundle] urlforresource:@"1"withextension:@"JPG"]; NSLog (@"-----%@", URL); [FormData Appendpartwithfileurl:url name:@"File"filename:@"Test.jpg"mimetype:@"Image/jpeg"Error:nil];/** FileData: Specific data for the file to be uploaded name: The server over there receive the file with the parameter name filename: (Tell the server) the file name of the uploaded files MimeType : The file type of the uploaded file */        //UIImage *image = [UIImage imagenamed:@ "minion_01"];        //NSData *filedata = uiimagepngrepresentation (image);        //[FormData appendpartwithfiledata:filedata name:@ "file" filename:@ "Haha.png" mimetype:@ "Image/png"];} success:^ (afhttprequestoperation *operation, id responseobject) {NSLog (@"Upload succeeded"); } failure:^ (Afhttprequestoperation *operation, Nserror *error) {NSLog (@"Upload failed"); }];

Use of "iOS development-Network" AFN

Related Article

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.