jamesHoward
jamesHoward

Reputation: 107

iOS Get Connection

This is my first time on this site and I am very new to coding so I was wondering if somebody could help me out.

I want to set a get request from my iphone app to my website and get the information echoed back from the website to my phone.

I have gotten this far but do not know where to go from here. Any help would be much appreciated, thanks!

- (void)myData:(id)sender
{
  NSString *DataToBeSent;
  sender = [sender stringByReplacingOccurrencesOfString:@"," withString:@"%20"];
  [receivedData release];
  receivedData = [[NSMutableData alloc] init];
  DataToBeSent = [[NSString alloc] initWithFormat:@"http://194.128.xx.xxx/doCalc/getInfo.php?Data=%@",sender];
  NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:dataToBeSent] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:10];
  [request setHTTPMethod: @"GET"];
  NSError *requestError;
  NSURLResponse *urlResponse = nil;  
  NSData *response1 = [NSURLConnection sendSynchronousRequest:request   returningResponse:&urlResponse error:&requestError];
  [dataToBeSent release];
}

OLD WAY

- (void)myData:(id)sender
{
  NSString *dataToBeSent;
  sender = [sender stringByReplacingOccurrencesOfString:@"," withString:@"%20"];
  [receivedData release];
  receivedData= [[NSMutableData alloc] init];
  dataToBeSent= [[NSString alloc] initWithFormat:@"http://194.128.xx.xxx/doCalc/getInfo.php?Data=%@",sender];
  NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:dataToBeSent]];
  Theconn= [[NSURLConnection alloc]initWithRequest:theRequest delegate:self];
  NSLog (@"test1 %@", theRequest);
  NSLog (@"test2 %@", Theconn);
  [dataToBeSent release];
}

Then the following methods are called and I get my data BUT if I sent another request after my first one but different data on the same connection, it would always give me the same result which shouldn't happen

- (void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
  /* appends the new data to the received data */
  [receivedData appendData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)conn
{
  NSString *stringData= [[NSString alloc] 
  initWithData:receivedData encoding:NSUTF8StringEncoding]; 
  NSLog(@"Got data? %@", stringData);
  [self displayAlertCode:stringData];    
  [stringData release];
  // Do unbelievably cool stuff here //
}

Upvotes: 1

Views: 320

Answers (1)

Joel Kravets
Joel Kravets

Reputation: 2471

Assuming your data loaded properly you can convert the data into a string and do whatever you want with it.

NSData *response1 = [NSURLConnection sendSynchronousRequest:request   returningResponse:&urlResponse error:&requestError];

//Make sure to set the correct encoding
NSString* responseString = [[NSString alloc] initWithData:response1 encoding:NSASCIIStringEncoding];

If your server returns JSON there are 3rd party libraries that can parse the string into collections like NSArray and NSDictionary. If your server returns XML then NSXMLParser could be something you can use.

EDIT

I've changed your code to manage the memory a little differently.

.h

@property (nonatomic,retain) NSMutableData * receivedData;
@property (nonatomic,retain) NSURLConnection * Theconn;

.m

@synthesize receivedData;
@synthesize Theconn;

//A bunch of cool stuff


- (void)myData:(id)sender
{
    //If you already have a connection running stop the existing one
    if(self.Theconn != nil){
        [self.Theconn cancel];
    }

    sender = [sender stringByReplacingOccurrencesOfString:@"," withString:@"%20"];

    //This will release your old receivedData and give you a new one
    self.receivedData = [[[NSMutableData alloc] init] autorelease];


    NSString *dataToBeSent = [NSString stringWithFormat:@"http://194.128.xx.xxx/doCalc/getInfo.php?    Data=%@",sender];

    NSURLRequest *theRequest= [NSURLRequest requestWithURL:[NSURL URLWithString:dataToBeSent]];

    //This will release your old NSURLConnection and give you a new one
    self.Theconn = [NSURLConnection connectionWithRequest:theRequest delegate:self];

    NSLog (@"test1 %@", theRequest);
    NSLog (@"test2 %@", Theconn);

 }

 //...
 //Your delegate methods
 //...

 - (void) dealloc{
    [receivedData release];
    [Theconn release];
    [super dealloc];
 }

Upvotes: 1

Related Questions