使用POST方法下载XML文件并将其存储在文档目录中
问题描述:
但是,使用我的post方法从服务器获得答复方面取得了成功,但是,我在下载xml文件数据时遇到了问题。使用POST方法下载XML文件并将其存储在文档目录中
这是帖子的方法(我已经搜查计算器)
//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
那么这里就是我的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,但我下载的XML数据不会被解析(解析委托没有被调用)。我想我的下载文件的方式是错误的。有人能帮我吗?
答
我在我的项目中发现了这个问题。毕竟我的代码是正确的!我没有看到我遵循的代码使用NSCachesDirectory
,当我调用xmlData时,我使用了NSDocumentDirectory
,这就是为什么它不能被解析。
您是否尝试将数据转换为字符串(使用'NSUTF8StringEncoding')并将其记录到控制台? –
是的,我转换它,并得到正确的数据。这是requestReply字符串。 – theFool
然后,你如何设置你的'NSXMLParser'一定有什么问题。你是否遵循官方文档或一些教程? –