iphone网络请求与响应,并对响应的xml进行处理
来源:互联网 发布:avsow最新域名 编辑:程序博客网 时间:2024/05/17 08:55
网络请示与响应,并对
@interface
NSMutableData *responseData;
NSXMLParser *coutryParse;
NSMutableArray *allCoutry;
RootViewController *rootViewController;
@property (nonatomic, retain) IBOutlet RootViewController *rootViewController;
@property (nonatomic) NSMutableArray *allCoutry;
- (void) getCoutry;
- (void) getCoutryFromWebService:(NSString*) URLStr;
- (void) parseCoutry : (NSData *) CoutryXMLData;
@implementation
#define Coutry_URL @"http://www.accuweather.com/includes/ajax-functions/favoriteCountries.asp?region=ASI"
#define ErrorGettingCoutry @"Error Getting Coutry"
#define GettingCoutry @"Getting Coutry ..."
- (void) getCoutry
{
[self getCoutryFromWebService:Coutry_URL];
}
- (void) getCoutryFromWebService:(NSString*) URLStr
{
[rootViewController setTitle:@"Getting Coutry ..."];
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:TRUE];
NSURL *currentURL = [NSURL URLWithString:URLStr];
NSURLRequest *theRequest = [NSURLRequest requestWithURL:currentURL
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:10.0];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (theConnection) {
responseData = [[NSMutableData data] retain];
}
else
{
[rootViewController setTitle:ErrorGettingCoutry];
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:FALSE];
}
}
- (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
[rootViewController setTitle:ErrorGettingCoutry];
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:FALSE];
[connection release];
[responseData release];
}
- (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSHTTPURLResponse *HTTPResponse = (NSHTTPURLResponse*)response;
NSInteger statusCode = [HTTPResponse statusCode];
if (statusCode == 404 || statusCode == 500)
{
[rootViewController setTitle: ErrorGettingCoutry];
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:FALSE];
[connection cancel];
}
else {
[responseData setLength:0];
}
}
-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[responseData appendData:data];
}
- (void) connectionDidFinishLoading:(NSURLConnection *)connection
{
[self parseCoutry:responseData];
[connection release];
[responseData release];
}
- (void) parseCoutry : (NSData *) CoutryXMLData
{
if (coutryParse) {
[coutryParse release];
}
coutryParse = [[NSXMLParser alloc] initWithData:CoutryXMLData ];
coutryParse.delegate = self;
[coutryParse setShouldResolveExternalEntities:NO];
[coutryParse parse];
}
- (void)parser:(NSXMLParser *)parser
didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName
attributes:(NSDictionary *)attributeDict
{
if ([elementName isEqualToString:@"countries"]) {
if(allCoutry)
[allCoutry removeAllObjects];
else {
allCoutry = [[NSMutableArray alloc] init];
}
}
if ([elementName isEqualToString:@"country"]) {
//attributeDict当前节点属性
[allCoutry addObject:attributeDict];
}
}
//得到值
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if ([elementName isEqualToString:@"country"]) {
[rootViewController reloadData];
}
}
- iphone网络请求与响应,并对响应的xml进行处理
- Tornado对Web请求与响应的处理机制
- httpclient发送post xml请求参数据,对响应的xml做处理
- 【SoapUI】获取请求响应json中键值对的值,并进行断言
- 浏览器与IIS的请求处理响应
- responseXML 属性 如果来自服务器的响应是 XML,而且需要作为 XML 对象进行解析,请使用 responseXML 属性: 请求 books.xml 文件,并解析响应:
- 利用servlet对客户端增删改的请求进行响应
- JAVA: 发送请求并解析返回的XML响应
- Android网络拦截器,支持Debug模式,查看请求与响应的具体信息并修改
- httpclient的post请求并处理json格式响应数据
- 4.Tornado对Web请求与响应的处理机制 (副标题:作为Web Server的功能)
- 服务器端我们通过一个 Servlet 来处理来自客户端的请求,并对客户端作出响应。其核心处理代码
- Yii请求处理与响应处理
- 【网络通信:Volley】请求的发送与响应之String
- 【网络通信:Volley】请求的发送与响应之Image
- 【网络通信:Volley】请求的发送与响应之JSON
- nginx响应与处理请求的方法详解
- Ajax 的请求与响应
- C# webbrowser根据id、src或者alt属性轻松读取验证码
- 用C#制作艺术字
- automake编译时对./configure命令的操作(二)【转自:http://tieba.baidu.com/f?kz=817154941】
- 001--IOC和DI
- 那么好吃吗?
- iphone网络请求与响应,并对响应的xml进行处理
- 发现一个新的方法contains(child:DisplayObject)
- Linux文件夹大小查看办法
- js获取事件源对象
- 积分啊
- MS SQL 索引(三)
- 弹出窗口页面
- 027_《Delphi Direct X 图形与游戏程序设计》
- C#实现网页表单自动提交(126邮箱登录例程)