Basic use of iOS development afnetworking

Source: Internet
Author: User

    • First download afnetworking and then, in the class you need to use, import the following 2 header files:

#import "AFNetworking.h"//used primarily for network request methods #import "Uikit+afnetworking.h"//inside there is a way to load images asynchronously

    • Get request network Data mode:

-(void) obtaindata{    //  starter system steam wheel     [UIApplication  sharedapplication].networkactivityindicatorvisible = yes;             //in front of the server to write the domain name, the following stitching on the need to submit parameters, if the parameters are key=1     nsstring  *domainstr = @ "http://192.168.1.69/xffcol/index.php/Api/key/1/";     afhttprequestoperationmanager *manager = [afhttprequestoperationmanager manager];     manager.responseSerializer = [AFHTTPResponseSerializer serializer];         //is submitted in the form of get, only the above request address to get parameters can be     [manager  get:domainstr parameters:nil success:^ (afhttprequestoperation *operation, id  Responseobject)  {        //  hidden system steam Wheel          [uiapplication sharedapplication].networkactivityindicatorvisible = no;                 //json Analysis          nsdictionary *resultdic = [nsjsonserialization jsonobjectwithdata:responseobject  options:nsjsonreadingmutableleaves error:nil];             nslog (@ "---gets to the JSON-formatted dictionary--%@", Resultdic);             } failure:^ (Afhttprequestoperation *operation, nserror *error)  {               //  Parse failed hidden system steam wheel (can print error.userinfo view error message)         [UIApplication  sharedapplication].networkactivityindicatorvisible = no;             }];}

    • Post request network data mode:

-(void) obtaindata{    //  starter system steam wheel     [UIApplication  sharedapplication].networkactivityindicatorvisible = yes;             //server to the domain name       nsstring *domainstr =  @ "http://192.168.1.69/xffcol/index.php/Api/";         // If the parameter that needs to be submitted to the server is key=1,class_id=100    //create a mutable dictionary     nsmutabledictionary  *parametersdic = [nsmutabledictionary dictionary];    // Add the required parameters to the dictionary     [parametersdic setobject:@ "1"  forkey:@ "key"];     [parametersdic setobject:@ " forkey:@" class_id "];         AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager  Manager];    manager.responseserializer = [afhttpresponseserializer serializer];        // Submitted as a post, the parameters of the post is the domain name above, the parameters parameter is a dictionary type, the above dictionary as its parameter     [manager post: domainstr parameters:parametersdic success:^ (afhttprequestoperation *operation, id  Responseobject)  {        //  hidden system steam Wheel          [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;                 //json Analysis          NSDictionary *resultDic = [NSJSONSerialization  jsonobjectwithdata:responseobject options:nsjsonreadingmutableleaves error:nil];             nslog (@ "---gets to the JSON-formatted dictionary--%@", Resultdic);             } failure:^ (afhttprequestoperation *operation, nserror * Error)  {               //   Parse failed hide system steam wheel (can print error.userinfo view error message)         [uiapplication  sharedApplication].networkActivityIndicatorVisible = NO;             }];}

    • Implement uploading multiple images:

-(void) uploadpictures{        //domain name     nsstring  *domainstr = @ "http://192.168.1.69/xffcol/index.php/Api/";         AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager  manager];    manager.responseserializer = [afhttpresponseserializer  serializer];        //If you need to upload additional parameters, refer to the POST request above, create a mutable dictionary, and deposit the contents of the parameters that need to be submitted. Submit as parameter of parameters     [manager POST:domainStr parameters:nil  constructingbodywithblock:^ (Id<afmultipartformdata> formdata)      {      //_imagearray is the image array, my _imagearray is stored in the image of the data, the following can be directly removed to use, if the memory is an image, Convert image to data as follows: Nsdata *imagedata = uiimagejpegrepresentation (image, 0.7);          if  (_imagearray.count > 0 ) {              for (int i = 0;i < _imagearray.count;i ++) {                  NSData *data=_imageArray[i];                  //the name of the parameter being uploaded                  nsstring  *name = [nsstring stringwithformat:@ "%d",i];                  //uploaded by filename                  nsstring *filename = [ nsstring stringwithformat:@ "%@.jpg",name];                  [formData appendPartWithFileData:data                                               name: name                                           fileName:fileName                                           mimetype:@ "Image/jpeg"];              }          }                        }success:^ (Afhttprequestoperation *operation, id responseobject)       {         //shut down system steam wheel           [uiapplication sharedapplication].networkactivityindicatorvisible  = NO;                   //json Analysis          NSDictionary *  Resultdic = [nsjsonserialization jsonobjectwithdata:responseobject options: nsjsonreadingmutableleaves error:nil];                       nslog (@ "---resultdic--%@", resultDic);                   }           failure:^ (Afhttprequestoperation *operation, nserror *error)  {               //  parsing Failure                [uiapplication sharedapplication]. networkactivityindicatorvisible = no;                    }];}

    • How to set up a network picture:

[Leftview setimagewithurl:[nsurl urlwithstring:@ "Http://pic25.nipic.com/20121205/5955207_182136452000_2.jpg"] placeholderimage:@ "Here is the default image displayed when the network image is not loaded yet";


Basic use of iOS development afnetworking

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.