【问题标题】:NSString to NSURL error?NSString 到 NSURL 错误?
【发布时间】:2014-06-05 19:24:30
【问题描述】:

我之前将NSStrings 变成了NSURL,它总是只是[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]

现在,我发生了一件非常奇怪的事情。我有 not 为空的数据(来自 NSURLSessionDataTask)。就像我打印出来一样,我会得到字节的数据。

然后,当我尝试将其转换为 NSString 并将其注销时,它显示为 null。真的很奇怪。

这是一个sn-p:

NSMutableURLRequest *getProgressReportRequest = [[NSMutableURLRequest alloc] initWithURL:curURL];
[getProgressReportRequest setHTTPMethod:@"GET"];
NSLog(@"The request is %@", [getProgressReportRequest URL]);

NSURLSessionDataTask *getProgressReportTask = [defaultSession dataTaskWithRequest:getProgressReportRequest completionHandler:^(NSData *getProgressReportData, NSURLResponse *getProgressReportResponse, NSError *getProgressReportError) {
    if ([getProgressReportData length] > 0 && getProgressReportError == nil) {
         NSLog(@"some html is %@", [[NSString alloc] initWithData:getProgressReportData encoding:NSUTF8StringEncoding]);
    } else NSLog(@"Some error with the getting progress report %@", getProgressReportError);
}];
[getProgressReportTask resume];

【问题讨论】:

  • 您在控制台中得到了什么,NSLog 消息?
  • 2014-06-05 15:36:47.767 Zangle[1613:389139] some html is (null) 2014-06-05 15:36:48.308 Zangle[1613:389139] some html is (null) 2014-06-05 15:36:48.847 Zangle[1613:389139] some html is (null) 2014-06-05 15:36:49.351 Zangle[1613:389139] some html is (null) 2014-06-05 15:36:52.629 Zangle[1613:389139] some html is (null) 2014-06-05 15:36:57.723 Zangle[1613:389139] some html is (null)
  • 如果你可以发送网址,我会为你测试它&你确定http方法应该是GET而不是POST。

标签: ios objective-c nsstring nsdata


【解决方案1】:

在尝试将其转换为字符串之前记录数据。然后尝试将其转换为字符串并记录下来。

由于您获得非零长度数据,我猜您使用错误的编码将其转换回字符串。或者可能根本不是字符串数据,而是二进制数据。

【讨论】:

    【解决方案2】:

    -[NSString initWithData:encoding:] 如果数据不代表指定编码的有效字符串,则返回 nil。因此,您收到的数据似乎不是有效的 UTF-8。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-05
      • 2011-07-15
      • 2016-11-15
      • 1970-01-01
      • 2016-04-07
      相关资源
      最近更新 更多