AFNetworking 프로젝트 사용

3329 단어 ios항목AFNetworking
1.도입 할 파일:
1.AFNetworking 소스 파일:
2.필요 한 라 이브 러 리:
2.간단 한 사용 절차:
//         
static NSString *const BaseURLString = @"www.xx.com/";

/*****************   *********************/

NSURL *baseURL = [NSURL URLWithString:[NSString stringWithFormat:BaseURLString]];

//               

NSMutableDictionary *parameters = [NSMutableDictionary dictionaryWithCapacity:1];
    
[parameters setObject:    forKey:   ];
    
AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:baseURL];
[client registerHTTPOperationClass:[AFJSONRequestOperation class]];
[client setDefaultHeader:@"Accept" value:@"application/json"];
    
NSString *path = @"          ";//  restful "xx/yy/zz"


/*    。    getPath     ,    postPath        ,
      post   ,get post     。*/
[client postPath:path 
      parameters:parameters
         success:^(AFHTTPRequestOperation *operation, id responseObject) {
         
             //    :responseObject            

         }
         failure:^(AFHTTPRequestOperation *operation, NSError *error) {
             
             //    :error          
             UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"      "
                                                          message:error
                                                         delegate:nil
                                                cancelButtonTitle:@"OK"          
                                                otherButtonTitles:nil];
             [av show];
                                
         }
];




/*****************   (operation   )*********************/



NSURL *baseURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@",BaseURLString,@"          "]];
    
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:baseURL 
                                                            cachePolicy:NSURLRequestReloadIgnoringCacheData 
                                                        timeoutInterval:20.0f];

//     
NSString *postString = @"keywords=  ";
    
NSStringEncoding enc = NSUTF8StringEncoding;
NSData *postData = [postString dataUsingEncoding: enc allowLossyConversion: YES];


//  Method    
[request setHTTPMethod: @"POST"];


//    
[request setHTTPBody:postData];
    
AFJSONRequestOperation *operation 
= [AFJSONRequestOperation JSONRequestOperationWithRequest:request 
                                                  success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) 
{
     
    //    :JSON              
        
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) 
{
    
    //    :error          
    NSLog(@"ERROR: %@", error);
        
}];

    
[operation start];

이 두 가지 방식 은 모두 자신의 네트워크 데이터 인 터 랙 션 클래스 에 쓸 수 있 고 블록 방법 도 패키지 하여 서버 에서 성공 적 으로 얻 은 데 이 터 를 구체 적 인 controller 에 전달 하여 표시 할 수 있 습 니 다.
3.UIImageView+AFNetworking 이라는 것 이 아주 좋 습 니 다.네트워크 그림 을 비동기 로 불 러 올 수 있 습 니 다.구체 적 인 용법 도 간단 하 다.
#import "UIImageView+AFNetworking.h"
[         imageView setImageWithURL:[NSURL URLWithString:             ] placeholderImage:[UIImage imageNamed:placeholder  ]];

어떤 좋 은 사용 건의 가 있 으 면 모두 함께 토론 할 수 있다.

좋은 웹페이지 즐겨찾기