Post Method:
.h file:
.h file:
@property(nonatomic,assign) NSMutableData *receivedData;
.m file:
receivedData = [NSMutableData data];
NSError *error;
NSMutableString *post = [NSMutableString stringWithFormat:@"foodid=%@",food_id];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/DeleteFood.php",kBaseURL]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:10.0];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSURLResponse *requestResponse;
NSData *requestHandler = [NSURLConnection sendSynchronousRequest:request returningResponse:&requestResponse error:&error];
NSString *requestReply = [[NSString alloc] initWithBytes:[requestHandler bytes] length:[requestHandler length] encoding:NSASCIIStringEncoding];
//retrieve response:
if(!error)
{ //log response
NSLog(@"Response from server = %@", requestReply);
[self foodretrieve];
}else{
[foodTable reloadData];
}
// (OR) retrieve json format:
[receivedData appendBytes:[requestReply UTF8String]
length:[requestReply lengthOfBytesUsingEncoding:NSUTF8StringEncoding]];
id jsonObjects = [NSJSONSerialization JSONObjectWithData:receivedData options:NSJSONReadingMutableContainers error:nil];
Get Method:
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/DeleteFood.php?foodid=%@",kBaseURL,food_id]];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSURLResponse *response;
NSError *error;
//send it synchronous
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
if(!error)
{ //log response
NSLog(@"Response from server = %@", responseString);
[self foodretrieve];
}else{
[foodTable reloadData];
}
No comments:
Post a Comment