0

私が持っているクラスで NSURLConnection を作成するときはいつでも、そのクラスによって接続された最初の URL に常に接続します。connNSURLConnection が格納されている ivar があり、接続するメソッドは次のとおりです。

-(void)getMoreProblems
{
    problemsPage++;
    NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:@"http://projecteuler.net/problems;page=%d",problemsPage]];
    NSURLRequest *req=[NSURLRequest requestWithURL:url];
    NSLog(@"%p",conn);
    conn=[[NSURLConnection alloc] initWithRequest:req delegate:self];
    NSLog(@"%p",conn);
}

URLの説明と接続のポインターが異なることを確認しNSLog、UIApplicationにURLをサファリにロードするように指示しました。私が知る限り、正しいページを読み込もうとします。また、POST と GET の両方を試しましたが、違いはありませんでした。何が原因でしょうか?

同様の問題でこれを見ている人のために編集:

NSMutableData私の問題は、各ページが読み込まれた後に接続データを保存したものを再初期化しなかったことです。

4

1 に答える 1

0

これは実際には答えではありませんが、コメントするには長すぎます。あなたが投稿したコードに問題はありません。getMoreProblems のコードを新しいプロジェクトに貼り付け、結果を確認するために必要なデリゲート メソッドを追加しました。結果の文字列を見ると、(getMoreProblems への最初の呼び出しから) 受け取った最初のページで 1 から始まり、getMoreProblems への 2 回目の呼び出しで問題 51 から始まる問題番号が表示されます。getMoreProblems メソッドに追加したのは、最後の if-else 句だけです。ここに私が使用したコードがあります:

@synthesize window = _window,receivedData;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    problemsPage = 0;
    [self getMoreProblems];
}


-(void)getMoreProblems {
    problemsPage++;
    NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:@"http://projecteuler.net/problems;page=%d",problemsPage]];
    NSURLRequest *req=[NSURLRequest requestWithURL:url];
    NSLog(@"%p",conn);
    conn=[[NSURLConnection alloc] initWithRequest:req delegate:self];
    NSLog(@"%p",conn);
    if (conn) {
        self.receivedData = [NSMutableData data];
    } else {
        NSLog(@"The Connection Failed");
    }
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    NSLog(@"%@",response.URL);
    [self.receivedData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    NSLog(@"In connection:didReceiveData:");
    [self.receivedData appendData:data];
}



- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    NSLog(@"Succeeded! Received %lu bytes of data",[receivedData length]);
    NSString *page = [[NSString alloc] initWithData:self.receivedData encoding:NSUTF8StringEncoding];
    NSLog(@"%@",page);
    [self performSelector:@selector(getMoreProblems) withObject:nil afterDelay:5];
}

だから、私はあなたの問題を再現することはできません.あなたが投稿していないコードのどこかにあると思います.

于 2012-07-24T00:15:07.590 に答える