Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在iOS中调用JSON方法_Json_Ios_Asihttprequest - Fatal编程技术网

如何在iOS中调用JSON方法

如何在iOS中调用JSON方法,json,ios,asihttprequest,Json,Ios,Asihttprequest,当我通过下面的curl调用时,有一个JSON调用: - (void)doCheckIn:(NSString *)Str { NSString *path = [[NSBundle mainBundle] bundlePath]; NSString *finalPath = [path stringByAppendingPathComponent:@"inStore-settings.plist"]; NSDictionary *plistDictionary = [[NS

当我通过下面的curl调用时,有一个JSON调用:

- (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);
    }
}
旋度-H “内容类型:应用程序/json”-H “接受:应用程序/json”-d “{\”签入\“:{\”消息\“:\”这是一个 测试\“}”

我得到这个结果:

{“checkin”:{“created_at”:“2011-01-29T13:52:49Z”,“id”:3,“message”:“this” 是一个 测试,“更新时间”:“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)


非常感谢您的帮助。

您是否尝试在
ASIFormDataRequest
实例中设置
Accept
Content-Type
标题?

查看您的curl命令,我认为您不想执行http multipart post,
ASIFormDataRequest
是专门设置来处理的。您只需要将请求的post正文设置为json字符串。尝试使用普通的
ASIHTTPRequest
类,并通过
appendPostData:
等方法将数据添加到帖子正文中。这将为您构建适当的HTTP请求。

这段代码有效:

- (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);
    }

}

记得点击√ 在我的答案旁边,如果它适合你的话。appURL的最终价值是什么?另外,您确定正确使用了setPostValue:方法吗?这对我来说似乎有点不对劲,因为你希望整个帖子正文是
{“checkin\”:{“message\”:“test\”}
,看起来你可能会将其设置为
message
@raidfive:appURL的值为“”,我甚至尝试过:“[request setPostValue:@”这是一条测试消息“forKey:@“message”];”没有任何效果!老实说,我以前从未弄乱过这个图书馆。我刚从他们的网站上查看了南方,我想你的意思是
piece
,而不是
peace