【问题标题】:How can i pass dictionary parameter to url using post method如何使用 post 方法将字典参数传递给 url
【发布时间】: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


【解决方案1】:

更改 forHTTPHeaderField 内容类型值

[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

我希望这会解决.. 谢谢

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-29
    • 1970-01-01
    • 2014-04-08
    • 2022-12-09
    • 1970-01-01
    相关资源
    最近更新 更多