problem in posting data in json from iPhone to MySQL/PHP
I'm a newbie to iPhone Development. Following is the to post data in json from iPhone to MySQL. Data seems to be posted from iPhone but problems occur while posting.
**- (IBAction) btnPostDataPressed : (id) sender {
NSURL *objUrl = [NSURL URLWithString : @"http://localhost/test_questionnaire/update.php"] ;
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL : objUrl] ;
[theRequest setHTTPMethod : @"POST"];
[theRequest setValue : @"application/json"
forHTTPHeaderField : @"Content-Type"];
[theRequest setTimeoutInterval:10];
//NSData *myData = [[NSString alloc] initWithString:@"nasir here"];
//[NSString alloc] initWithData:myData encoding:NSUTF8StringEncoding];
NSString *stringData = [[NSString alloc] initWithString:@"nasir here"];
NSDictionary *jsonDictionary = [NSDictionary dictionaryWithObject:stringData forKey:@"code"];
NSString *jsonString = [jsonDictionary JSONRepresentation];
NSString *strHttpBody = [NS开发者_开发技巧String stringWithFormat:@"%@", jsonString];
[theRequest setHTTPBody:[strHttpBody dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
textView.text = @"data successfully posted!!!";
}**
Also, I've tried with the lines commented out above but still problems remain.
Hoping for favorable response. Thanks.
You cant't post against localhost
as in that case it's your phone.
Use the IP of the server your PHP/MySQL script is running on, if you don't have local name resolution available
精彩评论