AFNetworking 프로젝트 사용
3329 단어 ios항목AFNetworking
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 ]];
어떤 좋 은 사용 건의 가 있 으 면 모두 함께 토론 할 수 있다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Swift의 패스트 패스Objective-C를 대체하기 위해 만들어졌지만 Xcode는 Objective-C 런타임 라이브러리를 사용하기 때문에 Swift와 함께 C, C++ 및 Objective-C를 컴파일할 수 있습니다. Xcode는 S...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.