• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

iOS : Message = "An error has occurred." in JSON POST

[复制链接]
菜鸟教程小白 发表于 2022-12-12 16:09:29 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

在java中工作,而不是在 objective-c 中。尽管处于“已发布 Json 数据”状态,但数据并未保存在数据库中。 我应该以如下所示的格式发送数据以获取 json 响应

{
                "rojID": "78",
                "Uid": "12",
                "EmailID": "[email protected]",
                "rojectInviterFQAnswers": [{
                    "slno": "1",
                    "Answer": "a1",
                    "order": "1",
                    "flag": "F"
                }, {
                    "slno": "2",
                    "Answer": "a1",
                    "order": "2",
                    "flag": "F"
                }, {
                    "slno": "1",
                    "Answer": "a1",
                    "order": "2",
                    "flag": "Q"
                }
                ]
            };

我正在发送我登录的字典,格式如下所示。

enter image description here

上面的代码和我的日志截图的区别是';'在每个键值对之后,因此我得到了标题中提到的响应。

有什么建议可以纠正代码/逻辑吗?这是我的代码。

 NSError *error = Nil;
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];

    NSDictionary *dictionaryArray1 = [NSDictionary dictionaryWithObjectsAndKeys"1", @"slno", @"a1", @"Answer", @"1", @"order", @"F", @"Flag", nil];

    NSDictionary *dictionaryArray2 = [NSDictionary dictionaryWithObjectsAndKeys"2", @"slno", @"a1", @"Answer", @"1", @"order", @"F", @"Flag", nil];

    NSDictionary *dictionaryArray3 = [NSDictionary dictionaryWithObjectsAndKeys"1", @"slno", @"a1", @"Answer", @"2", @"order", @"Q", @"Flag", nil];

    NSArray *arrayAnswer = [NSArray arrayWithObjects:dictionaryArray1, dictionaryArray2, dictionaryArray3, nil];

    NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys"78", @"rojID", @"12", @"UID", @"[email protected]", @"EmailID", arrayAnswer, @"rojectInviterFQAnswer", nil];

    NSLog(@"Dictionary that is being sent to URL is = %@", dictionary);

    NSURL *url = [NSURL URLWithString"http://cnapi.iconnectgroup.com/api/QRCodeScan/SaveAnswers"];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];

    NSData *requestData = [NSJSONSerialization dataWithJSONObject:dictionary options:kNilOptions error:&error];

    [request setHTTPMethod"OST"];
    [request setValue"application/json" forHTTPHeaderField"Accept"];
    [request setValue"application/json" forHTTPHeaderField"Content-Type"];
    [request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"];
    [request setHTTPBody: requestData];

    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    [connection start];
    [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error){
        if(error || !data)
        {
            NSLog(@"JSON Data not posted!");
            [activity stopAnimating];
            UIAlertView *alertMessage = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Data not saved" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alertMessage show];
        }
        else
        {
            [activity startAnimating];
            NSLog(@"JSON data posted! ");
            NSError *error = Nil;
            NSJSONSerialization *jsonObject = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];

            NSLog(@"Response is %@", jsonObject);
            [activity stopAnimating];
        }

    }];
}



Best Answer-推荐答案


这就是我解决自己问题的方法。

 NSString *str = @"{\"rojID\": \"78\",\"Uid\": \"12\",\"EmailID\": \"[email protected]\",";
    str = [str stringByAppendingString:@"\"rojectInviterFQAnswers\": ["];
    str = [str stringByAppendingString:@"{\"slno\": \"1\",\"Answer\": \"a1\",\"order\": \"1\", \"flag\": \"F\"},"];
    str = [str stringByAppendingString:@"{\"slno\": \"2\",\"Answer\": \"a1\",\"order\": \"1\",\"flag\": \"F\"},"];
    str = [str stringByAppendingString:@"{\"slno\": \"1\",\"Answer\": \"a1\",\"order\": \"2\",\"flag\": \"Q\"}"];
    str = [str stringByAppendingString:@"]}"];
    NSLog(@"String is === %@", str);

 NSURL *url = [NSURL URLWithString:@"http://cnapi.iconnectgroup.com/api/QRCodeScan/SaveAnswers/"];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];

NSData *requestData = [str dataUsingEncoding:NSUTF8StringEncoding];

[request setHTTPMethod:@"OST"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"];
    [request setHTTPBody: requestData];
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    [connection start];
    [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error){
        if(error || !data)
        {
            NSLog(@"JSON Data not posted!");
            [activity stopAnimating];
            UIAlertView *alertMessage = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Data not saved" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alertMessage show];
        }
        else
        {
            [activity startAnimating];
            NSLog(@"JSON data posted! ");
            NSError *error = Nil;
            NSJSONSerialization *jsonObject = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
            NSLog(@"Response is %@", jsonObject);
        }
    }];

关于iOS : Message = "An error has occurred." in JSON POST,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19804440/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap