[英]Objective-C How to send data to Http Post request with nested dictionary in header
我學習Objective-C已有幾個月了。 那時,我已經可以執行基本的發帖請求,但是今天我看到了包含嵌套字典的發帖請求的標題。 我不確定如何將數據傳遞到這樣的API。
我的問題是如何將數據發布到此API。 如果您能指向相關閱讀材料和教程,例如復雜的API請求或給出示例,將不勝感激。
API標頭如下所示:
{ "header": { "id": "2312b24c-5b83-46f0-8670-5edc2b1d1672", "parentId": "3gyy-w325e-bebd-ddsfsdf", "countryCode": "IRELAND", "timestamp": "2017-10-13T08:39:14.638Z" }, "myinfo": { "User": "intro", "contactPerson": "Newbee", "contact": "00000000", "notes": "hey guys i am new" } }
這是我的代碼,我需要多個詞典的幫助。
__block NSMutableDictionary *resultsDictionary;
SMAMobileAPI *api = [SMAMobileAPI sharedSMAMobileAPI];
NSMutableDictionary *bodyParams = [[NSMutableDictionary alloc]init];
NSMutableDictionary *header = [[NSMutableDictionary alloc]init];
[header setValue:@"2312b23c-5b83-46f0-8670-5edc2b1d1672" forKey:@"id"];
[header setValue:@"2e3a015e-bebd-ddsfsdf" forKey:@"parentId"];
[header setValue:@“IRELAND" forKey:@"countryCode"];
[header setValue:@"2017-10-13T08:39:14.638Z" forKey:@"timeStamp"];
NSMutableDictionary *myinfo = [[NSMutableDictionary alloc]init];
[Case setValue:@“intro" forKey:@“User"];
[Case setValue:@“newbie" forKey:@"contactPerson"];
[Case setValue:@"+254 724474140" forKey:@"contactPersonNo"];
[Case setValue:@"iOS case" forKey:@"notes"];
[bodyParams setValue:header forKey:@"header"];
[bodyParams setValue:myinfo forKey:@“myinfo"];
if ([NSJSONSerialization isValidJSONObject:bodyParams]) {//validate it
NSError* error = nil;
NSData* jsonData = [NSJSONSerialization dataWithJSONObject:bodyParams options:NSJSONWritingPrettyPrinted error: &error];
NSString *jsonString = [[NSString alloc] initWithData:jsonData
encoding:NSUTF8StringEncoding];
NSURL* url = [NSURL URLWithString:@“my URL"];
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0];
[request setHTTPMethod:@"POST"];//use POST
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%lu",(unsigned long)[jsonData length]] forHTTPHeaderField:@"Content-length"];
[request setHTTPBody:jsonData];//set data
__block NSError *error1 = nil;
//use async way to connect network
[NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse* response,NSData* data,NSError* error)
{
if ([data length]>0 && error == nil) {
resultsDictionary = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error1];
NSLog(@"resultsDictionary is %@",resultsDictionary);
[self hideLoading];
} else if ([data length]==0 && error ==nil) {
NSLog(@" download data is null");
[self hideLoading];
} else if( error!=nil) {
NSLog(@" error is %@",error);
[self hideLoading];
}
}];
}
}
似乎將NSDictionary轉換為JSON是您的實際問題。 請參閱文檔https://developer.apple.com/documentation/foundation/jsonserialization
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:yourDictionary
options:NSJSONWritingPrettyPrinted error:&error];
NSString *jsonString = [[NSString alloc] initWithData:jsonData
encoding:NSUTF8StringEncoding];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.