本章主要简示了在IOS里面如何发出云请求,以及处理云端返回的数据,包括NSUrl等以及XML解析部分的知识。具体使用是一个查询IP的请求应用。
(用了10几次不让用了)
1.选取查询IP的云请求链接,并在浏览器里面试试效果,了解查询返回结果的格式
2.新建一个工程,拖拉一个UITextField和UIView,分别用作输入IP地址信息和查询结果展示
3.在发出云请求的时候,根据查询的IP构造查询的地址链接,然后使用NSURL生成链接,使用NSURLRequest构造请求,最后使用NSURLConnection发出请求
4.由于NSURLConnection是是一个异步请求,所以处理其返回结果需要使用其代理,首先指定它的代理,然后实现其代理里面的接收数据方法(connection:didReceiveData)和接收数据完成的方法(connectionDidFinishLoading)
5.在接收数据完成里面使用NSXMLParser解析返回的结果(有需要使用其代理),由于该类是使用SAX的方式解析,所以使用感觉比较烦
6.获取的查询格式如下所示:
<ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://WebXml.com.cn/"> <string>112.134.113.124</string> <string>华中科技大学</string> </ArrayOfString>
//
// ViewController.h
// IPInfo
//
// Created by God Lin on 14/12/11.
// Copyright (c) 2014年 arbboter. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
{
IBOutlet UITextField* _textIP;
IBOutlet UITextView* _textResult;
NSString* _stringReciveData;
NSMutableDictionary* _dictIP;
NSMutableArray* _arrayIP;
NSString* _currentContext;
}
@property (nonatomic, retain) UITextField* _textIP;
@property (nonatomic, retain) UITextView* _textResult;
@property (nonatomic, retain) NSString* _stringReciveData;
@property (nonatomic, retain) NSMutableDictionary* _dictIP;
@property (nonatomic, retain) NSMutableArray* _arrayIP;
@property (nonatomic, retain) NSString* _currentContext;
-(IBAction)go;
@end
//
// ViewController.m
// IPInfo
//
// Created by God Lin on 14/12/11.
// Copyright (c) 2014年 arbboter. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize _textIP;
@synthesize _textResult;
@synthesize _stringReciveData;
@synthesize _arrayIP;
@synthesize _dictIP;
@synthesize _currentContext;
-(IBAction)go
{
_stringReciveData = @"";
[self._textIP resignFirstResponder];
// 请求连接
NSURL* url = [NSURL URLWithString:[NSString stringWithFormat:@"http://webservice.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx/getCountryCityByIp?theIpAddress=%@",self._textIP.text]];
NSURLRequest* request = [NSURLRequest requestWithURL:url];
[NSURLConnection connectionWithRequest:request delegate:self];
}
#pragma NSURLConnectionDelegate协议
// 收到数据调用该方法,可能不是一次性收到所有数据,所以..
- (void)connection:(NSURLConnection *)connection
didReceiveData:(NSData *)data
{
NSString* str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
if([self._stringReciveData length])
{
self._stringReciveData = [self._stringReciveData stringByAppendingString:str];
}
else
{
self._stringReciveData = str;
}
NSLog(@"%@",str);
}
// 数据接收完成调用该方法
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSXMLParser* xmlParser = [[NSXMLParser alloc] initWithData:[self._stringReciveData dataUsingEncoding:NSUTF8StringEncoding]];
xmlParser.delegate = self;
[xmlParser parse];
}
#pragma NSXMLParser协议
// 开始解析XML调用该方法
- (void)parserDidStartDocument:(NSXMLParser *)parser
{
self._dictIP = [[NSMutableDictionary alloc] init];
}
// XML解析结束调用该方法
- (void)parserDidEndDocument:(NSXMLParser *)parse
{
NSString* info = nil;
static int n = 0;
for (id ip in self._dictIP)
{
info = [NSString stringWithFormat:
@"%-5d [%@, %@]\n", ++n, ip, [self._dictIP objectForKey:ip]];
self._textResult.text = [self._textResult.text stringByAppendingString:info];
}
[self._dictIP removeAllObjects];
self._textIP.text = @"";
}
// 解析XML遇到开始标签调用该方法
- (void)parser:(NSXMLParser *)parser
didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qualifiedName
attributes:(NSDictionary *)attributeDict
{
if ([elementName isEqualToString:@"ArrayOfString"])
{
self._arrayIP = [[NSMutableArray alloc] init];
}
self._currentContext = @"";
}
// 解析XML遇到结束标签调用该方法
- (void)parser:(NSXMLParser *)parser
didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName
{
if([self._currentContext length] > 0)
{
[self._arrayIP addObject:self._currentContext];
}
if([self._arrayIP count] == 2)
{
[self._dictIP setObject:self._arrayIP[1] forKey:self._arrayIP[0]];
}
}
// 解析XML遇到标签内容时调用该方法
// (有可能一个标签内容不止触发该函数一次)
- (void)parser:(NSXMLParser *)parser
foundCharacters:(NSString *)string
{
// 去掉空格和换行
string = [string stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceCharacterSet]];
string = [string stringByReplacingOccurrencesOfString:@"\n" withString:@""];
if([string length] > 0)
{
self._currentContext = [self._currentContext stringByAppendingString:string];
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self._textResult.editable = NO;
self._textResult.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg.jpg"]];
self._textResult.textColor = [UIColor yellowColor];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
原文:http://blog.csdn.net/arbboter/article/details/41902267