使用 POST 方法下载 XML 文件并将其存储在文档目录中
Download XML File with POST Method and Store it in Document's Directory
我使用 post 方法成功地从服务器获得了回复,但是,我在下载 xml 文件数据时遇到问题。
这是我的 Post 方法(我在 Whosebug 中搜索过)
//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 = data
// 来自 dataTaskRequest
然后这是我将xml数据保存到文档目录
的代码
// 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 解析数据时,xml数据无法正常工作。我尝试使用 xml 文件并将其放入我的项目中,它解析 xml 但我下载的 xml 数据未被解析(未调用解析委托)。我认为我下载文件的方式是错误的。有人可以帮助我吗?
我在我的项目中发现了问题。毕竟我的代码是正确的!我没有看到我遵循的代码使用了 NSCachesDirectory
并且当我调用 xmlData 时我使用了 NSDocumentDirectory
这就是它没有被解析的原因。
我使用 post 方法成功地从服务器获得了回复,但是,我在下载 xml 文件数据时遇到问题。
这是我的 Post 方法(我在 Whosebug 中搜索过)
//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 = data
// 来自 dataTaskRequest
然后这是我将xml数据保存到文档目录
的代码// 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 解析数据时,xml数据无法正常工作。我尝试使用 xml 文件并将其放入我的项目中,它解析 xml 但我下载的 xml 数据未被解析(未调用解析委托)。我认为我下载文件的方式是错误的。有人可以帮助我吗?
我在我的项目中发现了问题。毕竟我的代码是正确的!我没有看到我遵循的代码使用了 NSCachesDirectory
并且当我调用 xmlData 时我使用了 NSDocumentDirectory
这就是它没有被解析的原因。