2

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

{
                "ProjID": "78",
                "Uid": "12",
                "EmailID": "ratnam_nv@yahoo.com",
                "ProjectInviterFQAnswers": [{
                    "slno": "1",
                    "Answer": "a1",
                    "order": "1",
                    "flag": "F"
                }, {
                    "slno": "2",
                    "Answer": "a1",
                    "order": "2",
                    "flag": "F"
                }, {
                    "slno": "1",
                    "Answer": "a1",
                    "order": "2",
                    "flag": "Q"
                }
                ]
            };

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

在此处输入图像描述

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

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

 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", @"ProjID", @"12", @"UID", @"ratnam_nv@yahoo.com", @"EmailID", arrayAnswer, @"ProjectInviterFQAnswer", 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:@"POST"];
    [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];
        }

    }];
}
4

2 回答 2

1

所以这就是我如何解决我自己的问题。

 NSString *str = @"{\"ProjID\": \"78\",\"Uid\": \"12\",\"EmailID\": \"ratnam_nv@yahoo.com\",";
    str = [str stringByAppendingString:@"\"ProjectInviterFQAnswers\": ["];
    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:@"POST"];
    [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);
        }
    }];
于 2013-11-08T10:55:53.700 回答
0

就我而言,我遇到了同样的错误,问题是我向服务发送了错误的数据。基本上,错误是由以下行设置错误的正文引起的:

[request setHTTPBody: requestData];

之间,sendSynchronousRequest 在 iOS9 中已被弃用。采用

...
NSURLSessionDataTask * dataTask = [[NSURLSession sharedSession] dataTaskWithRequest:request
                            completionHandler:^(NSData *data,
                                    NSURLResponse *response,
                                    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];
    }
}] resume];
于 2015-11-22T16:41:50.683 回答