Use of the afnetworking framework

Source: Internet
Author: User


#import "ViewController.h"
#import "AFNetworking.h"

@interface Viewcontroller ()

@end

@implementation Viewcontroller

-(void) Viewdidload {
[Super Viewdidload];

}

-(void) Touchesbegan: (Nsset *) touches withevent: (uievent *) event{


[Self sendget];
[Self sendpost];
[Self upLoad];
[Self downLoad];


The default is asynchronous request!!
}

/**
* GET Request
*/
-(void) sendget{

Afhttprequestoperationmanager * Mamaner=[afhttprequestoperationmanager manager];//single case

Sets the type of data returned by parsing (the default is parsing JSON) (can be set, there are three kinds)
Mamaner.responseserializer=[afhttpresponseserializer serializer];//No matter what data is returned, unified parsing into binary data
Mamaner.responseserializer = [Afxmlparserresponseserializer serializer];//returns the XML, using this
Mamaner.responseserializer = [Afjsonresponseserializer serializer];//default

Get request two ways of writing
(1) The wording of a
NSString * [email protected] "http://192.168.2.162/logo.php?userName=jereh&pwd=123";
[Mamaner get:url parameters:nil success:^ (afhttprequestoperation *operation, id responseobject) {

NSLog (@ "%@", responseobject);

} failure:^ (Afhttprequestoperation *operation, Nserror *error) {
NSLog (@ "%@", error);
}];

(2) notation two, similar to the writing of post
NSString * [email protected] "http://192.168.2.162/logo.php";
nsdictionary * [Email protected]{@ "UserName": @ "Jereh" @ "pwd": @ "123"};
[Mamaner get:url parameters:dic success:^ (afhttprequestoperation *operation, id responseobject) {
//
NSLog (@ "%@", responseobject);
//
} failure:^ (Afhttprequestoperation *operation, Nserror *error) {
NSLog (@ "%@", error);
//    }];

}

/**
* POST Request
*/
-(void) sendpost{

Afhttprequestoperationmanager * Mamaner=[afhttprequestoperationmanager Manager];

Mamaner.responseserializer=[afhttpresponseserializer Serializer];



NSString * [email protected] "http://192.168.2.162/loginPost";
nsdictionary * [Email protected]{@ "UserName": @ "Jereh" @ "pwd": @ "123"};

[Mamaner post:url parameters:dic success:^ (afhttprequestoperation *operation, id responseobject) {

NSLog (@ "%@", responseobject);


} failure:^ (Afhttprequestoperation *operation, Nserror *error) {
NSLog (@ "%@", error);
}];


}




/**
* POST request (upload, use post)
*/
-(void) upload{

Afhttprequestoperationmanager * Mamaner=[afhttprequestoperationmanager Manager];

Mamaner.responseserializer=[afhttpresponseserializer Serializer];

NSString * [email protected] "http://192.168.2.162/upload.php";


[Mamaner post:url parameters:nil constructingbodywithblock:^ (id<afmultipartformdata> formData) {

Nsurl * Url=[[nsbundle Mainbundle] urlforresource:@ "Exclusive_title_icon.png" withextension:nil];
[FormData appendpartwithfileurl:url name:@ "file" filename:@ "Jereh.png" mimetype:@ "Image/png" error:nil];


} success:^ (afhttprequestoperation *operation, id responseobject) {

NSString * str=[[nsstring alloc] Initwithdata:responseobject encoding:nsutf8stringencoding];
NSLog (@ "%@", str);

} failure:^ (Afhttprequestoperation *operation, Nserror *error) {
NSLog (@ "%@", error);
}];


}




/**
* POST request (download, GET request)
*/
-(void) download{

(0) Create Manager object
Nsurlsessionconfiguration * Config=[nsurlsessionconfiguration defaultsessionconfiguration];
Afurlsessionmanager * Manager=[[afurlsessionmanager alloc] initwithsessionconfiguration:config];


(1) Monitor download progress
[Manager setdownloadtaskdidwritedatablock:^ (Nsurlsession *session, Nsurlsessiondownloadtask *downloadTask, int64_t Byteswritten, int64_t Totalbyteswritten, int64_t totalbytesexpectedtowrite) {
Note that the current thread is a child thread and needs to return the main thread to refresh the data
CGFloat progress=totalbyteswritten*1.0/totalbytesexpectedtowrite;//writes a total of more than
Dispatch_sync (Dispatch_get_main_queue (), ^{
self.progress.progress=progress;
});
}];

(2) Request
Nsurlrequest * request=[nsurlrequest requestwithurl:[nsurl urlwithstring:@ "Http://192.168.2.162/test.rar"];
Note that the method below has a return value, and the block has a return value
Nsurlsessiondownloadtask *task= [Manager downloadtaskwithrequest:request Progress:nil Destination:^nsurl * (NSURL * TargetPath, Nsurlresponse *response) {

NSString * Cache=[nssearchpathfordirectoriesindomains (nscachesdirectory, Nsuserdomainmask, YES) lastObject];
Cache =[cache stringbyappendingpathcomponent:@ "Jereh.rar"];

Nsurl * Url=[nsurl Fileurlwithpath:cache];

return URL;


} completionhandler:^ (Nsurlresponse *response, Nsurl *filepath, Nserror *error) {

if (Error) {
NSLog (@ "Download Failed");
}else{
NSLog (@ "Download complete");
}

}];

(3) Start task (note to write this sentence)
[Task resume];

}


@end

Use of the afnetworking framework

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.