質問

Apple Document を使用してアプリを作成しています。サーバーへの接続には成功しましたが、サーバーから 0 バイトを受け取りました (応答データなし)。私は次の手順を実行します。

  1. ビューベースのアプリを作成し、プロパティ「receivedData」を追加します。

    ViewController.h 内:

    @property (nonatomic, retain) NSMutableData *receivedData;

    ViewController.m 内:

    @synthesize receivedData; 
  2. ViewController.m のアクション「ViewDidLoad」に次を追加します。

    receivedData = [NSMutableData alloc];
  3. ビューにボタンを追加し、そのボタンに対するアクションを追加します。

    // create the request
    NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://..."]
                        cachePolicy:NSURLRequestUseProtocolCachePolicy
                        timeoutInterval:60.0];
    // create the connection with the request
    // and start loading the data
    NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
    if (theConnection) {
    // Create the NSMutableData that will hold
    // the received data
    // receivedData is declared as a method instance elsewhere
    receivedData=[[NSMutableData data] retain];
    } else {
    // inform the user that the download could not be made
    }

これらのコードをデバッグすると、receivedData が 0 バイトを返すことがわかりました。何が問題なのか何か考えはありますか?私のコードを簡単に修正していただければ幸いです。

役に立ちましたか?

解決

(HTTP応答が受信された後)デリゲートのコールバックは、フレームワークによって呼び出された後のデータのみをreceivedData中に書かれており、利用できるようになります -

あなたのコードは、HTTP接続を作成します。あなたは、Appleのドキュメント

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top