在Objective C中将数据发布到Wufoo表单

时间:2013-02-14 12:13:31

标签: iphone ios objective-c json wufoo

我正在尝试将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;
    }
)

班级档案:

ViewController.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]];
}

DiaryForm.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;
}

@end

1 个答案:

答案 0 :(得分:1)

通过更改WufooAPIClient.m类initWithBaseURL:方法中的一行来解决此问题。

此:

self.parameterEncoding = AFJSONParameterEncoding;

致:

 self.parameterEncoding = AFFormURLParameterEncoding;

Wufoo通过发送HTTP POST数据来工作,然后向您发回json或xml响应。我所做的是将发送的数据设置为JSON数据,而不是原本应该使用的Form Data。

希望能帮助任何人尝试做同样的事情:)