내 서버의 PHP 스크립트를 가리키는 URL에서 데이터 출력을받는 NSURLConnection이 있습니다.NSURLConnection 비동기 연결에서 데이터가 깨졌습니다.
대부분의 경우 모든 것이 잘 작동하고 데이터는 완전한 형태로 검색됩니다. 이 경우 내가 연결을 다시로드하면 항상 같은 null 또는 깨진 블록 의 반환됩니다,
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
:
그러나, 가끔에서 데이터의 NULL 또는 파손 (즉, 하단)를 수신 요청에 대한 데이터
EDIT * : 나는 데이터가 0이라고 생각하면 실제로 데이터 을 받았지만이 데이터에서 생성 된 NSString은 0이라는 사실을 깨달았습니다. 나는 아직도 왜 이해하지 못한다. 내 PHP 인코딩 출력은 항상 UTF-8이므로 인코딩 문제는 아니라고 생각하며 대부분의 경우이 작업을 수행합니다.
동일한 요청으로 PHP 스크립트를 검사하여 서버 측 또는 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];
}
어떤 도움이 많이 주시면 감사하겠습니다.
이전 연결이 완료되기 전에 setUpConnectionAndMakeRequest가 두 번로드되지 않았습니까? –
나는이 문제를 정확히 가지고 있으며 인코딩을 ASCII로 변경하여 해결했습니다. 입력 내용이 UTF-8인지 확인 하시겠습니까? –
예, 인코딩을 NSASCIIStringEncoding으로 변경하면이 문제가 해결됩니다. 감사합니다. . – Zigglzworth