0

asynchornusリクエストを実装しようとしています。私は調査を行いましたが、これは私が得た最高のものですが、コードには解決策が見つからないエラーがたくさんあります

   NSURL *url2 = [NSURL URLWithString:@"www.google.com"];

    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url2];
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];

    [NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
    {
        if ([data length] > 0 && error == nil)
            [delegate receivedData:data];//i get the error here use of undeclared identifier 'delegate' ,when I put self insead I receive the error : no visible @interface for "my class name"  declares the selector "received data"
        else if ([data length] == 0 && error == nil)
            [delegate emptyReply];//same error here 
        else if (error != nil && error.code == ERROR_CODE_TIMEOUT) //the error here is "use of undelcared identifier ERROR_CODE_TIMEOUT
            [delegate timedOut];//error here is save as the first one
        else if (error != nil)
            [delegate downloadError:error];//error here is save as the first one
    }];

NSURLConnectionDelegate.hファイルに追加しました

誰かがエラーを教えてもらえますか?

ありがとう

4

2 に答える 2

1

この方法の経験はあまりありませんが、この例は機能します。これをサーバーに送信して、アプリ内購入の商品情報の取得をテストしました。投稿したように、他のifを追加して、考えられるさまざまな結果をテストすることもできますが、これで開始できます。投稿したスタブにデリゲートへの参照がある理由がわかりません。ブロックメソッドのポイント(またはポイントの1つ)は、デリゲートなしでこれらの種類のことを実行できるようにすることです。

- (void)makeConnection {
NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:kServerPath]
                                          cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
                                      timeoutInterval:5];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:theRequest queue:queue completionHandler:^(NSURLResponse* theResponse, NSData* theData, NSError* error) {
    NSLog(@"%@,  %@  %@",theResponse.suggestedFilename,theResponse.MIMEType,theResponse.textEncodingName);
    if (theData != nil && error == nil) {
        NSArray *productArray = [NSJSONSerialization JSONObjectWithData:theData options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"%@",productArray);
    }else{
        NSLog(@"%@",error.localizedDescription);
    }
}];

}

于 2012-03-29T05:01:00.943 に答える
0

initWithRequest:delegate: を実行し、必要に応じてデリゲート メソッドを実装する方が簡単だと思います。あなたがしたいでしょう

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

- (void)connection:(NSURLConnection *)theConnection didReceiveData:(NSData *)data

少なくとも。一部のコードについては、 http://developer.apple.com/library/ios/#samplecode/SimpleFTPSample/Listings/URLGetController_m.htmlを参照してください。受信データがインクリメンタル ブロックに対して呼び出されることに注意してください。おそらく、プロパティ内の合計データを追跡し、データが到着したときにデータを追加したいと思うでしょう。それ以外に、エラー処理と認証をスローすることもできますが、それで始める必要があります。

于 2012-03-29T03:36:13.220 に答える