NSURL not working
I wanted to ask if anyone could help he get this code to work. Nothing is showing up in my MySQL database. Thanks, enbr.
NSString *urlstr = [[NSString a开发者_运维问答lloc] initWithFormat:@"http://mysite.come/myapp/submitrating.php?name=%@&comment=%@&rating=%@",
[selectedItem objectForKey:@"name"], comment.text, selectedRating];
NSString *urlstrEncoded = [urlstr stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [[NSURL alloc] initWithString:urlstrEncoded];
[urlstr release];
[url release];
You need to do much more than that! At minimum you need the following:
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:theURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10.0];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
along with implementing the following methods:
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
You're not doing anything with the URL object except creating it. Perhaps you want to try an NSURLConnection
?
I got it to work with this code.
NSString *urlstr = [NSString stringWithFormat:@"http://mysite.com/myapp/submitrating.php?name=%@&comment=%@&rating=%@", [selectedItem objectForKey:@"name"], comment.text, selectedRating];
NSString *urlEncoded = [urlstr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [[NSURL alloc] initWithString:urlEncoded];
NSString *ans = [NSString stringWithContentsOfURL:url encoding:NSASCIIStringEncoding error:nil];
精彩评论