2

接続が成功したかどうかを検証しようとしていますが、一貫性のない結果が得られています。偽の URL を使用して同期リクエストを実行しようとすると、次のようになります。

NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

if (responseData)   
    {  
        did_send = TRUE;  
    }   
    else   
    {  
        did_send = FALSE;
    }

しばらくハングし、最終的に戻ります:

 did_send = FALSE;

しかし、偽の URL を使用して非同期リクエストを行うと、次のようになります。

NSURLConnection *conn=[[NSURLConnection alloc] initWithRequest:request delegate:self ];
if (conn)   
    {  
        did_send = TRUE;  
    }   
    else   
    {  
        did_send = FALSE;
    }

私は得る:

did_send = TRUE;

毎回。非同期リクエストでは変更できないデフォルトのタイムアウト期間でリクエストがタイムアウトする間、タイムアウトを設定でき、60 秒間ハングする必要がないため、非同期リクエストを機能させる必要があります。何か案は?

4

2 に答える 2

7

nsurlconnection クラスのデリゲート メソッドconnection:didFailWithError:を使用してみてください: これにより、一貫した結果が得られるはずです。

-(void)getLocationsFromWebService {
    NSLog(@"in getLocationsFromWebService");


    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:TRUE];


    NSURLRequest *theRequest = [NSURLRequest requestWithURL:self.locationsURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:100.0];

    NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
    self.requestType = @"GET";
    if (theConnection) {
        _responseData = [[NSMutableData data] retain ];
    } else {

        [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:FALSE];
    }
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {

    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:FALSE]; 
    NSLog(@"in mapviewcontroller");
    NSLog(@"Error connecting - %@",[error localizedDescription]);
    [connection release];
    [_responseData release];
}

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {

    NSHTTPURLResponse *HTTPResponse = (NSHTTPURLResponse *)response;
    NSInteger statusCode = [HTTPResponse statusCode];

    if (404 == statusCode || 500 == statusCode) {
        //[self.controller setTitle:@"Error Getting Parking Spot ....."];
        [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:FALSE];

        [connection cancel];
        NSLog(@"Server Error - %@", [NSHTTPURLResponse localizedStringForStatusCode:statusCode]);
    } else {
        if ([self.requestType isEqualToString:@"GET"])
            [_responseData setLength:0];
    }
}

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    if ([self.requestType isEqualToString:@"GET"])
        [_responseData appendData:data];
}

-(void) connectionDidFinishLoading:(NSURLConnection *)connection {

    if ([self.requestType isEqualToString:@"GET"]) {
        [self parseLocations:_responseData];
        [_responseData release];
    }
    [connection release];

}
于 2010-08-16T18:55:08.873 に答える
1

インスタンス化するだけでは何も起こりNSURLConnectionません。そのクラスのインスタンスを作成しますが、データ転送は開始しません。オブジェクトは通常、要求を満たすことができない場合でも nil ではありません。

むしろ、デリゲート オブジェクト (selfコード内) を実装する必要がありますNSURLConnectionDelegate(これは、「受信データ」や「エラー状態」などのコールバックを処理します)。次に、NSURLConnectionインスタンスに-startメッセージを送信する必要があります。これにより、実行中のスレッドの実行ループでスケジュールされます。 .

プロトコルの詳細NSURLConnectionDelegate(コールバックで何をすべきかに関する情報を含む) については、Apple の「URL Loading System Programming Guide 」を参照してください。

于 2010-08-16T21:02:38.527 に答える