我尝试将NSDictionary
转换为JSON
数据并将其发送到PHP
.server,并在setHTTPBody
的“POST”请求中发送。 当我从我的app
寄出时,我从服务器收到一个空,但是当我从PostMan
发送JSON
时,我收到这些对象。如何创建JSON并将其发布到Web服务客观c
我在哪里错了?
- (void)viewDidLoad
{
[super viewDidLoad];
NSError *error = nil;
NSString *url = [NSString stringWithFormat:@"http://myAddress/sql_service.php"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:url]];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
NSArray *arrayOfStrings = @[@"alex",@"dima"];
NSDictionary *dict = @{@"request_type" : @"select_with_params",
@"table" : @"user",
@"where" : @"f_name=? OR f_name=?",
@"values" : arrayOfStrings};
NSData* jsonData1 = [NSJSONSerialization dataWithJSONObject:dict options:kNilOptions error:&error];
[request setHTTPBody:jsonData1];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[connection start];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
if (data)
{
[receivedData appendData:data];
}
else
{
}
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"didFailWithError");
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSError * error = nil;
NSMutableDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:receivedData options:0 error:&error];
NSLog(@"connectionDidFinishLoading");
}
这是我需要发布的json。
{
request_type: "select_with_params",
table: "user",
where: "f_name=? OR f_name=?",
values: ["dima", "alex"]
}
在didReceiveData的数据是:
您是否已经验证ŧ帽子'jsonData1'不是'nil'? – Avi
究竟是什么错误?在'NSURLConnectionDelegate'的方法中,你是否收到任何数据?如果是你的回答是顶层的NSArray而不是NSDictionary? – Larme
我编辑的问题,并添加图像不是零。 –