0

URL に接続してその URL のコンテンツ (この場合は単純な xml ドキュメント) を取得する非常に単純なアプリを作成しようとしています。私の問題は、リクエストが送信されないように見えることです。

メインファイルから実行する新しいプロジェクト (Foundation ツール) を作成しました。NSURLConnection のデリゲートを自分自身に設定し、必要なデリゲート メソッドを実装しました。問題は、これらのメソッドが呼び出されないことです。URL を偽の URL に変更しても問題ありません (以下の例のように)。アプリケーションは、応答を送信または待機せずに起動を終了するように思えます。アプリケーションがリクエストのスレッドを作成し、レスポンスを待たずに終了する可能性がありますか?? 「作成された接続」のような NSLOG ステートメントを取得するため、クラスが実行されていることはわかっていますが、デリゲート メソッド内から NSLOG ステートメントはありません。

私は何が欠けていますか?

#import "NetworkController.h"
@implementation NetworkController

- (id)init
{
    if(self = [super init])
    {

    }   
    return self;
}
- (void)dealloc
{
    NSLog(@"Calling dealloc");
    [super dealloc];
}
- (void) performConnection
{
    NSMutableData *receivedData;
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:
                                       @"http://www.test.php?some variables"]];

    NSMutableURLRequest *connectionRequest = [NSMutableURLRequest requestWithURL:url
                                                       cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60.0];
    [url release];
    NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:connectionRequest delegate:self startImmediately:YES];

    if (!theConnection)
    {
        NSLog(@"Failed to submit request");
    }
    if(theConnection)
    {
        receivedData=[[NSMutableData data] retain];
        NSLog(@"Created connection.");

        NSLog(@"--------- Request submitted ---------");
        NSLog(@"receivedData: %@", receivedData);
    }

    [connectionRequest release];
}

#pragma mark NetworkController Delegate
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    NSLog(@"Received response: %@", response);
    NSLog(@"Received response, connection retain count is %d",[connection retainCount]);
}

#pragma mark NetworkController Delegate
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    NSLog(@"Connection received data, retain count: %d", [connection retainCount]);
}

#pragma mark NetworkController Delegate
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
    NSLog(@"finished connection retain count:  %d", [connection retainCount]);}

#pragma mark NetworkController Delegate
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    NSLog(@"Error receiving response: %@", error);
    [connection release];
}

@end

#import <Foundation/Foundation.h>
#import "NetworkController.h"

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];


    NetworkController *n = [[NetworkController alloc] init];
    n.performConnection;
    [n release];

    // insert code here...
    NSLog(@"Hello, World!");
    [pool drain];
    return 0;
}
4

1 に答える 1

2

まず、メッセージの構文は次のとおりです。

[n performConnection];

あなたが持っているのは、プロパティアクセス構文です。また、メソッドはreturningとして入力されvoidているため、そのレベルでも機能することが保証されているとは思いません。

とにかく、主な問題は、非同期NSURLConnection APIを使用しているが(良い)、同期動作を期待している(悪い)ことです。すべてのNSURLConnectionメソッドはすぐに戻り、バックグラウンドで機能します。必要に応じてデリゲートメッセージをネットワークコントローラーに送信します。ただし、すべての設定が完了したら、ネットワークコントローラーを解放します(接続がリークするため、releaseあなたのdeallocメソッドではありませんでした)、そして終了します。

ネットワークコントローラーを解放する前に実行ループを実行して、接続に要求を送信し、応答を読み取り、ネットワークコントローラーに委任メッセージを送信する時間を与える必要があります。また、ネットワークコントローラーの方法で接続する必要がありますcancelreleasedealloc

また、デリゲートメソッドからアクセスできる必要があるため、そのNSMutableData変数をインスタンス変数にする必要があります。メソッドでもそれを解放することを忘れないでくださいdealloc(そして、接続が終了したことを示す両方のデリゲートメソッドでreleaseそれを設定してください)。nil

于 2009-05-10T13:52:03.310 に答える