【问题标题】:How to call a JSON Method in iOS如何在 iOS 中调用 JSON 方法
【发布时间】:2011-06-19 17:22:16
【问题描述】:

当我通过 curl 调用时,有一个 JSON 调用,如下所示:

卷曲 -H “内容类型:应用程序/json”-H “接受:应用程序/json”-d "{\"checkin\":{\"message\":\"这是一个 测试\”}}” http://gentle-rain-302.heroku.com/checkins.json

我得到这个结果:

{"checkin":{"created_at":"2011-01-29T13:52:49Z","id":3,"message":"this 是一个 测试","updated_at":"2011-01-29T13:52:49Z"}}

但是当我像下面这样调用我的 iPhone 应用程序时:

- (void)doCheckIn:(NSString *)Str
{
    NSString *path = [[NSBundle mainBundle] bundlePath];
    NSString *finalPath = [path stringByAppendingPathComponent:@"inStore-settings.plist"];
    NSDictionary *plistDictionary = [[NSDictionary dictionaryWithContentsOfFile:finalPath] retain];

    NSString *appURL = [plistDictionary objectForKey:@"apiurl"];
    appURL = [appURL stringByAppendingString:@"checkins.json"];
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:appURL]];
    [request setPostValue:@"{\"checkin\":{\"message\":\"test\"}}" forKey:@"message"];
    [request startSynchronous];
    NSError *error = [request error];
    if (!error) {
        NSString *response = [request responseString];
        NSLog(response);
    }
}

我得到一个结果:

“您想要的更改被拒绝 (422)"

非常感谢任何帮助。

【问题讨论】:

  • appURL到底是什么值?另外,您确定您正确使用了 setPostValue: 方法吗?对我来说这似乎有点不对劲,因为您希望整个帖子正文为 {\"checkin\":{\"message\":\"test\"}} 并且看起来您可能将其设置为 message 的值。
  • @raidfive:appURL 有“gentle-rain-302.heroku.com/checkins.json”,我什至试过:“[request setPostValue:@"This is a test message" forKey:@"message"];"没有任何效果!

标签: json ios asihttprequest


【解决方案1】:

这段代码有效:

- (void)doCheckIn:(NSString *)Str
{
    NSString *path = [[NSBundle mainBundle] bundlePath];
    NSString *finalPath = [path stringByAppendingPathComponent:@"inStore-settings.plist"];
    NSDictionary *plistDictionary = [[NSDictionary dictionaryWithContentsOfFile:finalPath] retain];

    NSString *appURL = [plistDictionary objectForKey:@"apiurl"];
    appURL = [appURL stringByAppendingString:@"checkins.json"];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:appURL]];
    [request appendPostData:[@"{\"checkin\":{\"message\":\"test by Zuzar\"}}" dataUsingEncoding:NSUTF8StringEncoding]];
    [request addRequestHeader:@"Content-Type" value:@"application/json"];
    [request addRequestHeader:@"Accept" value:@"application/json"];
    [request setRequestMethod:@"POST"];
    [request startSynchronous];
    NSError *error = [request error];
    if (!error) {
        NSString *response = [request responseString];
        NSLog(@"%@", response);
    }

}

【讨论】:

  • 我认为您的意思是 piece 而不是 peace
【解决方案2】:

查看您的 curl 命令,我认为您不想进行 http 多部分发布,ASIFormDataRequest 专门设置为处理。您只想将请求的帖子正文设置为您的 json 字符串。尝试使用普通的ASIHTTPRequest 类并通过appendPostData: 等方法将数据添加到帖子正文。这应该会为您构建适当的 HTTP 请求。

【讨论】:

【解决方案3】:

您是否尝试在您的 ASIFormDataRequest 实例中设置 AcceptContent-Type 标头?

【讨论】:

  • 如果对你有用,记得点击我的答案旁边的√。
猜你喜欢
  • 2015-08-15
  • 2011-10-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-13
  • 1970-01-01
  • 2011-03-17
  • 2011-11-19
相关资源
最近更新 更多