【发布时间】:2016-06-03 18:01:04
【问题描述】:
我厌倦了这样做..但我无法得到回应..
NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:@"access_name", @"about us", nil];
NSDictionary *finalDict = [[NSDictionary alloc] initWithObjectsAndKeys:dict, @"cmscontent", nil];
NSString * post =[NSString stringWithFormat:@"%@",finalDict];
NSData *postdata= [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength=[NSString stringWithFormat:@"%lu",(unsigned long)[postdata length]];
NSMutableURLRequest *request= [[NSMutableURLRequest alloc]init];
NSString *str=@"http://my url...";
[request setURL:[NSURL URLWithString:str]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postdata];
NSError *error;
NSURLResponse *response;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *returnstring=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
NSMutableDictionary *dict12=[returnstring JSONValue];
NSLog(@"%@",dict12);
我的 json 字典参数是..
{
"cmscontent":
{
"access_name": "about us"
}
}
所以请告诉我如何才能得到回应? 提前致谢。
【问题讨论】:
-
网址是什么?你想在 JSON 字符串中传递字典吗?您是否尝试过将 Dictionary 转换为 JSON 字符串
-
在此代码中将所有字典作为参数传递..
-
应该如何使用
NSString *str=@"http://my url..."; -
我认为你应该使用
application/json作为Content-Type。
标签: ios objective-c