NSURLConnection数据在异步连接中中断(NSURLConnection Data broken in asynchronous connection)

编程入门 行业动态 更新时间:2024-10-27 15:30:22
NSURLConnection数据在异步连接中中断(NSURLConnection Data broken in asynchronous connection)

我有一个NSURLConnection接收来自指向我服务器上的PHP脚本的URL的数据输出。

大多数情况下,一切正常,数据以完整的形式检索。

但是,有时我会收到NULL或损坏(即下半部分)的数据:

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data

发生这种情况时,如果我重新加载连接,它将始终返回请求的相同null或断块数据。

编辑*:我已经意识到,当我收到我认为是零数据时,我实际上收到了数据但是从这些数据创建的NSString是零。 我仍然不明白为什么。 我的php编码输出总是UTF-8,所以我不认为这是编码的问题,而且它大部分时间用于此。

我已经检查了PHP脚本与相同的请求,以验证它是在服务器端或PHP脚本不是问题,并确认它不是。

我的代码如下:

-(void)setUpConnectionAndMakeRequest { NSString *URLpath = @"http://www.example.com/myphp.php"; NSURL *myURL = [[NSURL alloc] initWithString:URLpath]; NSMutableURLRequest *myURLRequest = [NSMutableURLRequest requestWithURL:myURL cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60]; [myURL release]; [myURLRequest setHTTPMethod:@"POST"]; //I added this because I thought it may be a problem relating to cache but it isn't NSURLCache *cache = [NSURLCache sharedURLCache]; [cache removeAllCachedResponses]; NSString *httpBodystr = [NSString stringWithString:@"command=runscript"]; [myURLRequest setHTTPBody:[httpBodystr dataUsingEncoding:NSUTF8StringEncoding]]; [mailData setData:nil]; //mailData is a NSMutableData object which accumulates the data retrieved by the request [NSURLConnection connectionWithRequest:myURLRequest delegate:self]; } - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { NSString *untrimmedDataSTR = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; //Created so I can see the data (always text) in NSLog NSLog(@"Live Data: %@", untrimmedDataSTR); //This is where I see that the data is broken or null when it shouldn't be [mailData appendData:data]; //Append accumulated data to NSMutableData object used later in my app. [untrimmedDataSTR release]; }

任何帮助将非常感激。

I have a NSURLConnection that receives data output from a url pointing to a php script on my server.

Most of the time everything works fine and the data is retrieved in its complete form.

However, sometimes I receive NULL or broken (i.e. the bottom half) of data at:

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data

When this happens, if I reload the connection it will always return the same null or broken block of data for the request.

EDIT*: I've realized that when I receive what I thought was nil data, I actually received data but the NSString created from this data is nil. I still don't understand why though. My php encoding output is always UTF-8 so I don't think it is an issue of encoding and besides it works most of the time with this.

I have checked the php script with that same request to verify that it is not a problem on the server side or with the php script and confirmed that it is NOT.

My code is Below:

-(void)setUpConnectionAndMakeRequest { NSString *URLpath = @"http://www.example.com/myphp.php"; NSURL *myURL = [[NSURL alloc] initWithString:URLpath]; NSMutableURLRequest *myURLRequest = [NSMutableURLRequest requestWithURL:myURL cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60]; [myURL release]; [myURLRequest setHTTPMethod:@"POST"]; //I added this because I thought it may be a problem relating to cache but it isn't NSURLCache *cache = [NSURLCache sharedURLCache]; [cache removeAllCachedResponses]; NSString *httpBodystr = [NSString stringWithString:@"command=runscript"]; [myURLRequest setHTTPBody:[httpBodystr dataUsingEncoding:NSUTF8StringEncoding]]; [mailData setData:nil]; //mailData is a NSMutableData object which accumulates the data retrieved by the request [NSURLConnection connectionWithRequest:myURLRequest delegate:self]; } - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { NSString *untrimmedDataSTR = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; //Created so I can see the data (always text) in NSLog NSLog(@"Live Data: %@", untrimmedDataSTR); //This is where I see that the data is broken or null when it shouldn't be [mailData appendData:data]; //Append accumulated data to NSMutableData object used later in my app. [untrimmedDataSTR release]; }

Any help would be much appreciated.

最满意答案

根据NSString引用 , -initWithData:encoding:如果“由于某种原因初始化失败(例如,如果数据不代表编码的有效数据),则返回nil”。

这几乎可以肯定意味着来自服务器的响应实际上不是UTF-8编码数据。

尝试转换为NSString 之前 ,检查的方法是NSLog数据:

NSLog(@"Raw Data: %@", data);

(NSData上的-description方法将返回内容的十六进制表示;这将记录的内容)。

According to the NSString reference, -initWithData:encoding: returns nil if "the initialization fails for some reason (for example if data does not represent valid data for encoding)."

That almost certainly means that the response from the server is not, in fact, UTF-8 encoded data.

The way to check would be to NSLog the data before trying to convert to an NSString:

NSLog(@"Raw Data: %@", data);

(The -description method on NSData will return a hexadecimal representation of the contents; that's what will get logged).

更多推荐

本文发布于:2023-08-04 17:03:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1417912.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据   Data   NSURLConnection   connection   asynchronous

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!