【问题标题】:HTTP request GET response showing NULL valueHTTP 请求 GET 响应显示 NULL 值
【发布时间】:2014-11-25 08:27:47
【问题描述】:

我想使用简单的 Objective C 代码方法创建 HTTP 请求 postget 响应。下面的代码我可以成功发布。但我没有收到响应数据。仅响应打印 null 值。请帮帮我,我需要获取响应数据。

在我的代码下面

NSString *post = [NSString stringWithFormat:@"name=%@",name];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]init];
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:URLPATH]]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:postData];
NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:self];

if(conn) {
      NSLog(@"Connection Successful");
} else {
      NSLog(@"Connection could not be made");
}



// Create GET method
NSError *err;
NSURLResponse *responser;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&responser error:&err];

// JSON Formatter
NSError *error;
NSDictionary *jsonsDictionary = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
NSDictionary *respon = [jsonsDictionary objectForKey:@"response"];
NSLog(@"UPDATE RESPONSE : %@", respon);

【问题讨论】:

  • 响应不是有效的 JSON。从 responseData 创建 NSString 并打印出来。同时打印 err 和 error 变量。
  • 能否请您发布一些代码?我找不到你。
  • NSLog(@"%@", err);和 NSLog(@"%@", 错误);
  • 仅打印(空)@AbhiBeckert
  • 不如去谷歌搜索一下如何将 NSData 转换为 NSString?无论如何,这可能是不可能的,因为数据可能不是有效的字符串。这就是我建议改用 HexFiend 的原因。

标签: ios objective-c http nsurlconnection


【解决方案1】:

您的数据帖子是 NSString *post = [NSString stringWithFormat:@"name=%@",name]; 。因此,在这种情况下,您需要将请求标头 "Content-Type" 设置为 "application/x-www-form-urlencoded" 或使用以下代码将字符串转换为数据并设置 Content-Type 标头:

NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
 ...
[request setValue:@"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"];

【讨论】:

  • 可能不需要请求标头。这取决于服务器的期望,有些会忽略 Content-Type 标头。他正在从服务器获得响应,需要让代码处理它。
  • 我认为在这种情况下不需要。因为它内容正文数据格式信息
  • 尝试使用我发布的这段代码。使用 NSUTF8StringEncoding 而不是 NSASCIIStringEncoding,并为请求头字段“Content-Type”设置“application/x-www-form-urlencoded; charset=utf-8”。 @马诺
【解决方案2】:

很简单:

//-- Convert string into URL
    NSString *jsonUrlString = [NSString stringWithFormat:@"demo.com/your_server_db_name/service/link"];
    NSURL *url = [NSURL URLWithString:[jsonUrlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

    //-- Send request to server
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:url];
    //-- Send username & password for url authorization
    [request setValue: jsonUrlString forHTTPHeaderField:@"Content-Length"];
    [request setHTTPMethod:@"POST"]; //-- Request method GET/POST

    //-- Receive response from server
    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

    //-- JSON Parsing with response data
    NSDictionary *result = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:nil];
    NSLog(@"Result = %@",result);

示例:https://github.com/svmrajesh/Json-Sample

【讨论】:

  • 当这个网站上已经有数百个如何将 JSON 转换为 Objective-C 集合的示例时,为什么还要发布这个?
  • 什么是 authValue?
  • 不要使用这段代码,我可以看到其中有一堆错误。您的代码没有任何错误,您只需要找出服务器响应的数据。它返回一个无效的响应。
猜你喜欢
  • 2021-08-28
  • 2014-09-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-14
  • 1970-01-01
  • 1970-01-01
  • 2016-12-01
相关资源
最近更新 更多