【问题标题】:Why am I getting Empty Response from this Request iOS JSON为什么我从这个请求 iOS JSON 得到空响应
【发布时间】:2014-10-09 08:25:44
【问题描述】:

目标 C 代码

@try {
        NSURL *postURL = [NSURL URLWithString: @"http://yyy.com/api/Customer/"];
        NSDictionary *myDic = [[NSDictionary alloc] initWithObjectsAndKeys:
                               self.firstNameTextField.text, CustFirstName,
                               self.lastNameTextField.text, CustLastName,
                               nil];

        NSError *error;

        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:myDic options:NSJSONWritingPrettyPrinted error:&error];

        if (!jsonData) {
            NSLog(@"Got an error: %@", error);
        } else {

            // Get our Access Token back
            NSString *savedToken = [[NSUserDefaults standardUserDefaults]
                                    stringForKey:@"savedToken"];
            NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
            [request setURL:postURL];
            [request setHTTPMethod: @"POST"];
            [request setValue: @"application/json" forHTTPHeaderField: @"Accept"];
            [request setValue: @"application/json; charset=utf-8" forHTTPHeaderField: @"content-type"];
            [request setValue:savedToken forHTTPHeaderField:@"Authorization"];
            [request setHTTPBody:jsonData];

            NSHTTPURLResponse *response = nil;

            NSLog(@"Response code: %ld", (long)[response statusCode]);

            //NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
            if ([response statusCode] >= 200 && [response statusCode] < 300)
            {
                NSLog(@"Saving Success!");
                [self performSegueWithIdentifier:@"edit_add_customer" sender:self];
            } else {
                NSLog(@"Saving Fail!");
                [self alertStatus:@"Please try again" :@"Saving Fail" :0];
            }


        }
}
@catch (NSException *exception) {
    NSLog(@"Exception: %@", exception);
    [self alertStatus:@"Error Retrieving Customer Data." :@"Error!" :0];
}

无论如何,我的 JSON 字符串是正确的,因为我放置在我的 Rest Client 中。它能够保存。 我尝试记录我的响应状态代码,它显示为“0”。有人可以帮我解决这个问题吗?

【问题讨论】:

  • 你是否错过了连接:NSURLConnection *connection= [[NSURLConnection alloc] initWithRequest:request delegate:self]; ?

标签: ios objective-c iphone json rest


【解决方案1】:

在实际请求之前,您会尝试获取 statusCode

  NSLog(@"Response code: %ld", (long)[response statusCode]);
  NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

你必须在请求后检查 statusCode

  NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
  NSLog(@"Response code: %ld", (long)[response statusCode]);

【讨论】:

  • 您好,很抱歉回来晚了。是的,我猜你的代码正在使它工作,但我现在收到 500 的响应而不是空响应。在 Rest Client 工作时,我仍在尝试思考我的代码有什么问题。
  • HTTP 错误 500“内部服务器错误”。服务器无法做某事。尝试在您的模拟器中“重置内容和设置”。可能你会得到和手机一样的结果。
  • 谢谢你太棒了。哇,这行得通。那很奇怪,现在我可以成功发布数据了。为什么会这样?
  • 请您的客户检查服务器端的日志。也许您需要更改请求中的某些 setValues 或者它们有一些例外。
  • 谢谢。会通知他们。 :)
猜你喜欢
  • 1970-01-01
  • 2018-08-08
  • 1970-01-01
  • 1970-01-01
  • 2015-12-03
  • 1970-01-01
  • 2013-10-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多