views:

453

answers:

2

I'm using Apple Document to create an app.I succeed in connection to the server, but I receive 0 bytes from the server(no response data). I take the following steps:

1) I create a view-based App and add a property 'receivedData':

In ViewController.h:

@property (nonatomic, retain) NSMutableData *receivedData;

In ViewController.m:

@synthesize receivedData; 

2) ViewController.m's action 'ViewDidLoad', I add:

receivedData = [NSMutableData alloc];

3) Add a button in the View and add action for it:

// 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
}

When I debugging these codes, I find that receivedData returns 0 bytes. Any ideas about what goes wrong? A simple modify of my code will be appreciated.

A: 

Your code only creates the HTTP connection - the data will only be written to and available in receivedData after the delegate callbacks have been called by the framework (once the HTTP response is received). You can get more information and sample code from Apple's documentation

Marc Novakowski
Thank you. The method with 'didReceiveData' is added and I successfully receive the data from the server.
iPhoney
A: 

The answer is the same as it was the last time I answered it for you, over at http://stackoverflow.com/questions/593474/how-to-receive-data-from-url-on-iphone/593557#593557. I gave a detailed explanation-- did you read it?

Tom Harrington