我有一个NSURLConnection,它从指向服务器上php脚本的url接收数据输出。
但是,有时我会在以下位置接收空的或损坏的(即下半部分)数据:
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
当这种情况发生时,如果我重新加载连接,它将始终返回相同的空块或断块
请求的数据。
编辑*:
我意识到当我收到我认为是零的数据时,我实际上收到了数据
但是从这个数据创建的NSString是nil。我还是不明白为什么。我的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];
}
任何帮助都将不胜感激。