0

サービスにリクエストを送信したいのですが、返信でxmlデータを取得しています。

それで、インターネット接続をチェックしてWebサーバーにリクエストを送信する方法と、xml解析の重要なデリゲートメソッドを教えてください。

4

2 に答える 2

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];
于 2011-09-30T11:19:15.827 に答える
0

この質問は、少なくとも最初は自分で試してみるべきだとすでに尋ねています。ここで完全な解決策を探します: インターネット接続を確認する

希望はあなたを助けます!!

于 2011-09-30T11:18:39.197 に答える