문제

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 연결만 생성합니다. 프레임워크에서 대리자 콜백을 호출한 후에(HTTP 응답이 수신된 후) 데이터는 receivedData에 기록되고 사용 가능해집니다.자세한 정보와 샘플 코드는 다음에서 얻을 수 있습니다. Apple의 문서

다른 팁

답은 내가 마지막으로 당신을 위해 대답 한 것과 동일합니다. iPhone에서 URL에서 데이터를 어떻게받을 수 있습니까?. 자세한 설명을했습니다. 읽었습니까?

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top