使用 ASIHTTPRequest 使用 NSDictionary 来 POST 嵌套参数

发布于 2024-12-13 18:43:38 字数 1094 浏览 0 评论 0原文

尝试将嵌套参数的信息发布到 Rails 应用程序,但遇到了一些麻烦。

#pragma mark - Begin Network Operations
- (void)beginNetworkOperation {
    NSURL *requestURL = [NSURL URLWithString:[self retrieveURL]];
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:requestURL];

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_4_0
    [request setShouldContinueWhenAppEntersBackground:YES];
#endif

    [request setRequestMethod:@"PUT"];

    [request addRequestHeader:@"Content-Type" value:@"application/json"];

    [request addPostValue:strClientId forKey:@"client_id"];
    [request addPostValue:strAccessToken forKey:@"access_token"];

    NSDictionary *assetDictionary = [NSDictionary dictionaryWithObject:self.tags forKey:@"tags"];
    [request addPostValue:assetDictionary forKey:@"asset"];

    [request setDelegate:self];
    [request setDidFinishSelector:@selector(requestFinished:)];
    [request setDidFailSelector:@selector(requestFailed:)];
    [request startSynchronous];
}

self.tags 只是一个带有逗号分隔值的 NSString,但是一旦到达 Rails 服务器,标签参数就无法读取(params[:asset][:tags])。

Trying to post information for nested parameters to a rails app and having some trouble.

#pragma mark - Begin Network Operations
- (void)beginNetworkOperation {
    NSURL *requestURL = [NSURL URLWithString:[self retrieveURL]];
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:requestURL];

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_4_0
    [request setShouldContinueWhenAppEntersBackground:YES];
#endif

    [request setRequestMethod:@"PUT"];

    [request addRequestHeader:@"Content-Type" value:@"application/json"];

    [request addPostValue:strClientId forKey:@"client_id"];
    [request addPostValue:strAccessToken forKey:@"access_token"];

    NSDictionary *assetDictionary = [NSDictionary dictionaryWithObject:self.tags forKey:@"tags"];
    [request addPostValue:assetDictionary forKey:@"asset"];

    [request setDelegate:self];
    [request setDidFinishSelector:@selector(requestFinished:)];
    [request setDidFailSelector:@selector(requestFailed:)];
    [request startSynchronous];
}

self.tags is just a NSString with comma separated values, however once arriving at the rails server the tags parameter cannot be read (params[:asset][:tags]).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一场春暖 2024-12-20 18:43:38

尝试将字典作为 JSON 字符串而不是字典对象传递。

您可以使用 iOS5 JSON 库来做到这一点,或者使用这个库来获得更高的兼容性:

https://github.com/stig /json-framework

我所做的是使用appendPostData,因为我非常确定设置标头(addRequestHeader)和使用addPostValue 不是兼容的函数。
这是我的代码示例:

ASIFormDataRequest *request;
[request addRequestHeader:@"Content-Type" value:@"application/json"];
[request appendPostData:[[SBJsonWriter new] dataWithObject:myDictionaryToPassAsAnArgument]];

当您使用appendPostData时,您不能使用任何addPostValue。你必须把所有的东西都放进字典里。

Try to pass you dictionary as a JSON string and not a dictionary object.

You can do that using iOS5 JSON library, or this one for more compatibility:

https://github.com/stig/json-framework

What I do is use appendPostData because I am pretty sure that setting the header (addRequestHeader) and using addPostValue are not compatible functions.
Here is an example of my code:

ASIFormDataRequest *request;
[request addRequestHeader:@"Content-Type" value:@"application/json"];
[request appendPostData:[[SBJsonWriter new] dataWithObject:myDictionaryToPassAsAnArgument]];

When you use appendPostData, you can't use any addPostValue. You have to put everything in the dictionary.

云胡 2024-12-20 18:43:38

以下是 iOS 上 JSONKit 的工作代码片段。

[request addRequestHeader:@"Content-Type" value:@"application/json"];

NSMutableDictionary *requestDict = [[NSMutableDictionary alloc] init];
[requestDict setObject:@"iSteve" forKey:@"UserId"];
[requestDict setObject:@"1" forKey:@"CompanyCode"];
[requestDict setObject:@"IN" forKey:@"LineOfBusiness"];
[requestDict setObject:@"M" forKey:@"LineOfBusinessClassification"];
[requestDict setObject:pricingVariablesListString forKey:@"CarQuoteString"];
[request appendPostData:[requestDict JSONData]];

Here is snippet of working code with JSONKit on iOS.

[request addRequestHeader:@"Content-Type" value:@"application/json"];

NSMutableDictionary *requestDict = [[NSMutableDictionary alloc] init];
[requestDict setObject:@"iSteve" forKey:@"UserId"];
[requestDict setObject:@"1" forKey:@"CompanyCode"];
[requestDict setObject:@"IN" forKey:@"LineOfBusiness"];
[requestDict setObject:@"M" forKey:@"LineOfBusinessClassification"];
[requestDict setObject:pricingVariablesListString forKey:@"CarQuoteString"];
[request appendPostData:[requestDict JSONData]];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文