如何从服务器
问题描述:
如何通过从服务器获取数据如何从服务器
我是从服务器,但无法设置这些值获取XML数据textfield.canü请纠正我,如果有设置textfiled值获得的数据集textfiled值用下面的代码的任何事情
Customerdetails.h(模型)
@interface Customerdetails : NSObject
@property(nonatomic,strong)NSString*fst;
@property(nonatomic,strong)NSString*lst;
@property(nonatomic,strong)NSString*street;
@property(nonatomic,strong)NSString*city;
@end
detailviewcontroller.m
#import "DetailViewController.h"
#import "Customerdetails.h"
@interface DetailViewController()<NSXMLParserDelegate>
@property(nonatomic,strong)NSXMLParser*xmlparse;
@property(nonatomic,strong)NSMutableString*tempstr;
@property(nonatomic,strong)NSMutableString*foundvalue;
@property Customerdetails*csd;
@end
@implementation DetailViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
_csd=[[Customerdetails alloc]init];
NSMutableURLRequest*req=[[NSMutableURLRequest alloc]init];
_tempstr=[[NSMutableString alloc]init];
NSURL*url=[NSURL URLWithString:@"http://www.thomas-bayer.com/sqlrest/CUSTOMER/4"];
[req setURL:url];
[[[NSURLSession sharedSession]dataTaskWithRequest:req completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
// SMXMLDocument *document = [SMXMLDocument documentWithData:data error:&error];
NSString*str=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
_xmlparse=[[NSXMLParser alloc]initWithData:data];
_xmlparse.delegate=self;
[_xmlparse parse];
}] resume];
dispatch_async(dispatch_get_main_queue(), ^{
self.first.text=[self.csd fst];
self.last.text=[self.csd lst];
self.street.text=[self.csd street];
self.city.text=[self.csd city];
});
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(nullable NSString *)namespaceURI qualifiedName:(nullable NSString *)qName attributes:(NSDictionary<NSString *, NSString *> *)attributeDict;{
_tempstr=[[NSMutableString alloc]initWithString:elementName];
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(nullable NSString *)namespaceURI qualifiedName:(nullable NSString *)qName;{
// sent when an end tag is encountered. The various parameters are supplied as above.
if([self.tempstr isEqualToString:@"FIRSTNAME"]){
[self.csd setFst:_foundvalue];
self.foundvalue=nil;
self.tempstr=nil;
}
if([self.tempstr isEqualToString:@"LASTNAME"]){
[self.csd setLst:_foundvalue];
self.foundvalue=nil;
self.tempstr=nil;
}
if([self.tempstr isEqualToString:@"STREET"]){
[self.csd setStreet:_foundvalue];
self.foundvalue=nil;
self.tempstr=nil;
}
if([self.tempstr isEqualToString:@"CITY"]){
[self.csd setCity:_foundvalue];
self.foundvalue=nil;
self.tempstr=nil;
}
if([elementName isEqualToString:@"CUSTOMER"]){
NSLog(@"%@",self.csd);
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
if([self.tempstr isEqualToString:@"FIRSTNAME"]){
self.foundvalue=[[NSMutableString alloc]initWithString:string];
}
if([self.tempstr isEqualToString:@"LASTNAME"]){
self.foundvalue=[[NSMutableString alloc]initWithString:string];
}
if([self.tempstr isEqualToString:@"STREET"]){
self.foundvalue=[[NSMutableString alloc]initWithString:string];
}
if([self.tempstr isEqualToString:@"CITY"]){
self.foundvalue=[[NSMutableString alloc]initWithString:string];
}
}
@end
答
您正在设置对象self.csd设置之前的标签文本。只有在解析完成后,该对象才会被设置。
[[[NSURLSession sharedSession]dataTaskWithRequest:req completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
// SMXMLDocument *document = [SMXMLDocument documentWithData:data error:&error];
NSString*str=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
_xmlparse=[[NSXMLParser alloc]initWithData:data];
_xmlparse.delegate=self;
[_xmlparse parse];
}] resume];
//This part will get executed even before the parsing is done.
// So self.csd is nil and so the label are getting empty string.
dispatch_async(dispatch_get_main_queue(), ^{
self.first.text=[self.csd fst];
self.last.text=[self.csd lst];
self.street.text=[self.csd street];
self.city.text=[self.csd city];
});
现在,我们将尽快设置标签,文本作为XML解析,但要注意使用这个,因为这因为它是在后台线程和UI可以了一段时间保持不变尚需时日。
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(nullable NSString *)namespaceURI qualifiedName:(nullable NSString *)qName;{
// sent when an end tag is encountered. The various parameters are supplied as above.
if([self.tempstr isEqualToString:@"FIRSTNAME"]){
[self.csd setFst:_foundvalue];
//We are doing the UI stuff in main thread.
dispatch_async(dispatch_get_main_queue(), ^{
self.first.text = [self.csd fst];
});
self.foundvalue=nil;
self.tempstr=nil;
}
...
现在做这件事,你有两个self.csd准备好其他处理,并且UI已更新。
答
你的代码从你的XML安装值完成处理器内属于你的数据的任务:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
_csd=[[Customerdetails alloc]init];
NSMutableURLRequest*req=[[NSMutableURLRequest alloc]init];
_tempstr=[[NSMutableString alloc]init];
NSURL*url=[NSURL URLWithString:@"http://www.thomas-bayer.com/sqlrest/CUSTOMER/4"];
[req setURL:url];
[[[NSURLSession sharedSession]dataTaskWithRequest:req completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
// SMXMLDocument *document = [SMXMLDocument documentWithData:data error:&error];
NSString*str=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
_xmlparse=[[NSXMLParser alloc]initWithData:data];
_xmlparse.delegate=self;
[_xmlparse parse];
dispatch_async(dispatch_get_main_queue(), ^{
self.first.text=[self.csd fst];
self.last.text=[self.csd lst];
self.street.text=[self.csd street];
self.city.text=[self.csd city];
});
}] resume];
}
可能有其他问题与您的代码,但肯定需要改变。
当前代码的工作方式将数据安装到文本字段中的代码将在网络响应到达之前运行。
+0
@Duncan ......谢谢 – Nishanth
你得到了什么错误? –
什么都不显示在文本字段中...我从服务器获取4个值的名字,姓氏,街道和城市,并尝试显示在文本字段中 – Nishanth
您应该尝试在NSXMLParser的委托方法中设置label.text。 –