我想向服务发送请求,作为回复,我收到了xml数据。
所以,请告诉我如何通过互联网连接检查向网络服务器发送请求,并让我知道xml解析的重要委托方法。
答案 0 :(得分:1)
首先您还需要获取Reachability.h和Reachability.m文件
* 您可以从此处获取可达性文件: - *
http://developer.apple.com/library/ios/samplecode/Reachability/Reachability.zip
还需要在您要检查互联网连接的文件中导入.h文件
在.h文件中定义以下变量
NSMutableData *urlData;
NSMutableString *currentElementValue;
同时定义NSMutableString
以下是urlConnection的代码
reachability = [Reachability reachabilityForInternetConnection];
[reachability startNotifier];
NetworkStatus remoteHostStatus = [reachability currentReachabilityStatus];
if(remoteHostStatus == NotReachable)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert Title"
message:@"Internet connection not currently available."
delegate:nil
cancelButtonTitle:nil
otherButtonTitles:@"Ok", nil];
[alert show];
[alert release];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
return;
}
NSString *post = [NSString stringWithFormat:@""];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding
allowLossyConversion:NO];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"Your Information Which You Want To Pass"]]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
urlData=[[NSMutableData alloc] init];
[[[NSURLConnection alloc] initWithRequest:request delegate:self] autorelease];
用于XML解析重要方法
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[urlData setLength:0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[urlData appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
xmlParser = [[NSXMLParser alloc] initWithData:urlData];
[xmlParser setDelegate:self];
[xmlParser parse];
[urlData release];
}
#pragma mark -
#pragma mark XML Parsing Delegate Methods
- (void)parserDidStartDocument:(NSXMLParser *)parser
{
}
-(void)parserDidEndDocument:(NSXMLParser *)parser
{
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName
attributes:(NSDictionary *)attributeDict
{
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
NSCharacterSet *charsToTrim = [NSCharacterSet whitespaceAndNewlineCharacterSet];
string = [string stringByTrimmingCharactersInSet:charsToTrim];
if(!currentElementValue)
currentElementValue = [[NSMutableString alloc] initWithString:string];
else
[currentElementValue appendString:string];
//NSLog(@"Current Element Value :- '%@'",currentElementValue);
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
[currentElementValue release];
currentElementValue = nil;
}
您也可以通过下线
中止解析[xmlParser abortParsing];
答案 1 :(得分:0)
这个问题已经问过你应该先自己尝试一下, 在这里你去寻求完整的解决方案: to check internet connection
希望能帮到你!!