我正在尝试将Wufoo API集成到 iPhone 应用程序中,并且在最后阶段遇到了困难。我已使用AFNetworking成功连接到 Wufoo 表单,并创建了 AFHTTPClient 的子类以容纳所需的额外标头,例如 HTTP 授权。我还将参数编码设置为 AFJSONParameterEncoding。
当我使用上述方法发出请求时,它成功连接到服务器并发布数据,但又出现与空白字段有关的错误。我已经在 NSDictionary 中添加了要在请求中传递的字段/键对,但它们必须采用错误的格式或其他东西,因为它们没有在另一端得到处理。对不起,我知道这是一个冗长的问题,但任何帮助将不胜感激:)。我已经附加了我在控制台中得到的响应,以及相关的类文件/方法。
回复:
2013-02-14 12:11:43.053 <AppName>[14750:c07] Success?: 0
Error: Errors have been <b>highlighted</b> below.
Fields: (
{
ErrorText = "This field is required. Please enter a value.";
ID = Field1;
},
{
ErrorText = "This field is required. Please enter a value.";
ID = Field3;
},
{
ErrorText = "This field is required. Please enter a value.";
ID = Field222;
},
{
ErrorText = "This field is required. Please enter a value.";
ID = Field11;
},
{
ErrorText = "This field is required. Please enter a value.";
ID = Field12;
},
{
ErrorText = "This field is required. Please enter a value.";
ID = Field220;
}
)
类文件:
视图控制器.m
-(NSDictionary *)getParameters {
NSDictionary *d = [NSDictionary dictionaryWithObjectsAndKeys:
emailAddress, @"Field1", //problem
firstName, @"Field3", //problem
lastName, @"Field4",
activityArranged, @"Field10",
evidenceDescription, @"Field222", // problem
startDate, @"Field11", //Problem
endDate, @"Field224",
@"1", @"Field12", //Problem
benefitExplanation, @"Field113",
activityCategory, @"Field116",
webAddress, @"Field219",
@"I Agree", @"Field220", //Problem
nil];
return d;
}
-(void)submitForm {
DiaryForm *df = [[DiaryForm alloc] init];
[df submitForm:self params:[self getParameters]];
}
日记形式.m
-(void)submitForm:(id)sender params:(NSDictionary *)params {
WufooAPIClient *client = [WufooAPIClient sharedClient];
[[AFNetworkActivityIndicatorManager sharedManager] setEnabled:YES];
[[AFNetworkActivityIndicatorManager sharedManager] incrementActivityCount];
NSURLRequest *req = [client requestWithMethod:@"POST" path:@"entries.json" parameters:params];
AFJSONRequestOperation *op = [AFJSONRequestOperation JSONRequestOperationWithRequest:req success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
[[AFNetworkActivityIndicatorManager sharedManager] decrementActivityCount];
NSLog(@"Success?: %@\nError: %@\nFields: %@",[JSON objectForKey:@"Success"], [JSON objectForKey:@"ErrorText"], [JSON objectForKey:@"FieldErrors"]);
}failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
[[AFNetworkActivityIndicatorManager sharedManager] decrementActivityCount];
NSLog(@"[Error]: (%@ %@) %@", [request HTTPMethod], [[request URL] relativePath], error);
}];
[op start];
}
WufooAPIClient.m
#import "WufooAPIClient.h"
#import "AFNetworking.h"
@implementation WufooAPIClient
+(WufooAPIClient *)sharedClient {
static WufooAPIClient *_sharedClient = nil;
static dispatch_once_t oncePredicate;
dispatch_once(&oncePredicate, ^{
NSString *kProtocol = @"https";
NSString *kSub = @"<removed>";
NSString *kHost = @"wufoo.com";
NSString *kHash = @"<removed>";
NSString *sURL = [NSString stringWithFormat:@"%@://%@.%@/api/v3/forms/%@/", kProtocol, kSub, kHost, kHash];
NSLog(sURL);
NSURL *url = [NSURL URLWithString:sURL];
_sharedClient = [[self alloc] initWithBaseURL:url];
});
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 setAuthorizationHeaderWithUsername:@"<removed>" password:@"<removed>"];
self.parameterEncoding = AFJSONParameterEncoding;
return self;
}
@结尾