[英]AFNetworking - How to make POST request
編輯07/14
正如比爾·伯吉斯在評論中提到的那樣,這個問題與AFNetworking
version 1.3
有關。 這里的新人可能已經過時了。
我對iPhone開發很新,我使用AFNetworking作為我的服務庫。
我正在查詢的API是RESTful的,我需要發出POST請求。 為此,我嘗試使用以下代碼:
NSDictionary *parameters = [NSDictionary dictionaryWithObjectsAndKeys:@"my_username", @"username", @"my_password", @"password", nil];
NSURL *url = [NSURL URLWithString:@"http://localhost:8080/login"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
NSLog(@"Pass Response = %@", JSON);
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
NSLog(@"Failed Response : %@", JSON);
}];
[operation start];
此代碼有兩個主要問題:
AFJSONRequestOperation
似乎發出了GET
請求,而不是POST
請求。 我也嘗試過這段代碼:
NSDictionary *parameters = [NSDictionary dictionaryWithObjectsAndKeys:@"my_username", @"username", @"my_password", @"password", nil];
NSURL *url = [NSURL URLWithString:@"http://localhost:8080"];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
[httpClient postPath:@"/login" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Succes : %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Failure : %@", error);
}];
有沒有更好的方法來制作我想要的東西來完成它?
謝謝您的幫助 !
您可以覆蓋與AFNetworking
一起使用的請求的默認行為,以便作為POST進行處理。
NSURLRequest *request = [client requestWithMethod:@"POST" path:path parameters:nil];
這假設您已覆蓋默認的AFNetworking
設置以使用自定義客戶端。 如果你不是,我會建議你這樣做。 只需創建一個自定義類來為您處理網絡客戶端。
MyAPIClient.h
#import <Foundation/Foundation.h>
#import "AFHTTPClient.h"
@interface MyAPIClient : AFHTTPClient
+(MyAPIClient *)sharedClient;
@end
MyAPIClient.m
@implementation MyAPIClient
+(MyAPIClient *)sharedClient {
static MyAPIClient *_sharedClient = nil;
static dispatch_once_t oncePredicate;
dispatch_once(&oncePredicate, ^{
_sharedClient = [[self alloc] initWithBaseURL:[NSURL URLWithString:webAddress]];
});
return _sharedClient;
}
-(id)initWithBaseURL:(NSURL *)url {
self = [super initWithBaseURL:url];
if (!self) {
return nil;
}
[self registerHTTPOperationClass:[AFJSONRequestOperation class]];
[self setDefaultHeader:@"Accept" value:@"application/json"];
self.parameterEncoding = AFJSONParameterEncoding;
return self;
}
然后,您應該可以在操作隊列上啟動網絡調用,沒有任何問題。
MyAPIClient *client = [MyAPIClient sharedClient];
[[AFNetworkActivityIndicatorManager sharedManager] setEnabled:YES];
[[AFNetworkActivityIndicatorManager sharedManager] incrementActivityCount];
NSString *path = [NSString stringWithFormat:@"myapipath/?value=%@", value];
NSURLRequest *request = [client requestWithMethod:@"POST" path:path parameters:nil];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
// code for successful return goes here
[[AFNetworkActivityIndicatorManager sharedManager] decrementActivityCount];
// do something with return data
}failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
// code for failed request goes here
[[AFNetworkActivityIndicatorManager sharedManager] decrementActivityCount];
// do something on failure
}];
[operation start];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.