0
A
回答
0
這個問題已經問過你應該自己先好好嘗試ATLEAST, 這裏ü去了完整的解決方案: to check internet connection
希望將幫助ü!
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. 幫助HTML解析並將請求發送到Web服務器
- 2. 解析來自Web服務請求的XML結果Java
- 3. 發送多個Ajax請求web服務
- 4. 確保向Web服務發送請求
- 5. 如何將XML請求發送到Web服務
- 6. REST web服務 - 使用JSP從XML發送post請求
- 7. 如何在發送到Web服務之前清理xml請求
- 8. HTTP請求和解析XML
- 9. 從global.asax發送Web請求(Application_Start()方法)
- 10. 發送xml文件到web服務使用sendfile方法和php
- 11. 當前(.NET 4)將XML請求發送到Web服務的方式?
- 12. 發送XML到web服務
- 13. 無法將Web請求發送到Tomcat上的CAS服務器
- 14. Android:發送服務請求
- 15. 無法發送請求MQSeries服務
- 16. 解析未知的XML Web服務的最佳方法
- 17. getNode:withName:肥皂的方法解析一些大的XML Web服務
- 18. 解析XML請求
- 19. 如何解析從表單到REST Java/Jersey Web服務的發佈請求
- 20. 無法發送WCF服務請求作爲SOAP + XML
- 21. AJAX表單請求未發送到Express web服務器
- 22. Android的Web服務的XML解析
- 23. 向節點服務器發送第三方服務的請求
- 24. MVC解析XML的Web服務
- 25. .net到Coldfusion的Web服務XML解析
- 26. REST Web服務中的Spring XML解析
- 27. Ksoap2-Android的解析器爲我的Web服務請求一個XML節點
- 28. 設置XML Web服務請求
- 29. 獲取xml web服務請求
- 30. iPhone XML SOAP請求或.NET Web服務