【问题标题】:How to create and post json to web service Objective c如何创建 json 并将其发布到 Web 服务目标 c
【发布时间】:2016-03-02 10:01:26
【问题描述】:

我尝试将NSDictionary 转换为JSON 数据并将其发送到setHTTPBody 在“POST”请求中的PHP.server。 当我从 app 发送时,我从服务器收到了 null,但是当我从 PostMan 发送 JSON 时,我收到了对象。

我哪里错了?

- (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"]
}

jsonData1 不为零。

didReceiveData 中的数据是:

【问题讨论】:

  • 您确认jsonData1 不是nil
  • 到底出了什么问题?在NSURLConnectionDelegate的方法中,你收到数据了吗?如果您的回复是顶级的NSArray 而不是NSDictionary
  • 我编辑问题并添加不为零的图像。
  • 我再次编辑问题。
  • connection:connectionDidFinishLoading: 被调用了吗?如果是,你能记录下 [[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding], or even each time the delegate method connection:didReceiveData:` 的值被调用了吗?

标签: objective-c nsjsonserialization nsmutableurlrequest


【解决方案1】:

试试 AFNetworking

编辑

    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: [[NSString stringWithFormat:@"%@", jsonData1] dataUsingEncoding:NSUTF8StringEncoding]];

    AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    op.responseSerializer = [AFJSONResponseSerializer serializer];
    [op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject){

if (responseObject)
{
    NSLog(@"Success!");
}} failure:^(AFHTTPRequestOperation *operation, NSError *error)             {
    NSLog(@"Error");
    }];

[op start];

希望对你有帮助

【讨论】:

  • 在我将 AFNetworking 添加到我的项目后,我得到 puu.sh/ntaWx/8d288a851f.png 我需要添加哪个框架?
  • 您是否导入了“AFHTTPRequestOperationManager.h”?
  • 我解决了这个问题,但是在你的代码中有问题 - [request setHTTPBody: [jsonData1 dataUsingEncoding:NSUTF8StringEncoding]];我尝试这样做但仍然无法正常工作 - NSData* jsonData1 = [NSJSONSerialization dataWithJSONObject:dict options:kNilOptions error:&error]; [请求 setHTTPBody:jsonData1];
  • 现在尝试编辑。只需替换 [request setHTTPBody: [jsonData1 dataUsingEncoding:NSUTF8StringEncoding]];进入 [请求 setHTTPBody: [[NSString stringWithFormat:@"%@", jsonData1] dataUsingEncoding:NSUTF8StringEncoding]];
  • 谢谢大家(-:,但它仍然不起作用,我添加了带有错误的代码图像-puu.sh/nwD0r/a62b3c6183.png-在这里您可以在“postMan”中看到它的工作原理- puu.sh/nwD5w/3573303483.png
猜你喜欢
  • 1970-01-01
  • 2019-10-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-13
相关资源
最近更新 更多