POST 메서드를 사용하여 서버에서 응답하는 데 성공했지만 xml 파일 데이터를 다운로드하는 데 문제가 있습니다. 여기 POST 메서드를 사용하여 XML 파일 다운로드 및 문서 디렉토리에 저장
내가 // 여기에 그런 다음 dataTaskRequest에서 xmlData = data
이 (I에 유래에서 검색 한 것을) 내 포스트 방법
//We begin by creating our POST's body as an NSString, and converting it to NSData.
NSString *post = [NSString stringWithFormat:@"device_unique_key=%@&provision_type=c", deviceUniqueKey];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
//Next up, we read the postData's length, so we can pass it along in the request.
NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];
//Now that we have what we'd like to post, we can create an NSMutableURLRequest, and include our postData.
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"http://192.168.1.166/autoprovision/sip_setup/downloadSipSetup"]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:postData];
//And finally, we can send our request, and read the reply by creating a new NSURLSession:
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
[[session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSString *requestReply = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
xmlData = data; //This is the data that I try to donwload as xml file.
NSLog(@"requestReply: %@", requestReply);
}] resume];
이다 문서의 디렉토리에 XMLDATA를 저장하는 내 코드
// Display the network activity indicator
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
// Perform the request on a new thread so we don't block the UI
dispatch_queue_t downloadQueue = dispatch_queue_create("Download queue", NULL);
dispatch_async(downloadQueue, ^{
NSError* err = nil;
NSHTTPURLResponse* rsp = nil;
// Perform the request synchronously on this thread
NSData *rspData = [NSURLConnection sendSynchronousRequest:request returningResponse:&rsp error:&err];
// Once a response is received, handle it on the main thread in case we do any UI updates
dispatch_async(dispatch_get_main_queue(), ^{
// Hide the network activity indicator
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
if (rspData == nil || (err != nil && [err code] != noErr)) {
// If there was a no data received, or an error...
} else {
// Cache the file in the cache directory
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
path = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"init.xml"];
[[NSFileManager defaultManager] removeItemAtPath:path error:nil];
[xmlData writeToFile:path atomically:YES];
// Do whatever else you want with the data...
[self loadDataFromXML];
}
});
});
문제는 NSXMLParser를 사용하여 구문 분석 할 때 xmlData가 작동하지 않는 것입니다. xml 파일을 사용하여 내 프로젝트 안에 넣고 XML을 구문 분석하지만 다운로드 한 xmlData는 파싱되지 않습니다 (구문 분석 대리자는 호출되지 않습니다). 내 파일을 다운로드하는 내 방식이 잘못되었다고 생각합니다. 누군가 나를 도울 수 있습니까?
'NSUTF8StringEncoding'을 사용하여 데이터를 문자열로 변환하고 콘솔에 기록하려고 했습니까? –
예 변환하여 올바른 데이터를 가져 왔습니다. requestReply 문자열입니다. – theFool
그러면 NSXMLParser 설정 방법에 문제가있을 것입니다. 공식 문서 또는 튜토리얼을 따르고 있습니까? –