我嘗試將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
我編輯的問題,並添加圖像不是零。 –