问题
I am struggling to get one particular string to upload, using php and mysql I have a number of strings from textfields that will successfully go, this particular string is the text from a label in another viewcontroller. I can see the text with NSLog but for some reason it will not go, I'm confident the server side is working properly as I can pass other strings to that particular row in mySQL. To get the text from the other view I am using
- (IBAction)submit:(id)sender;
{
GetLocation * getlocation =[[GetLocation alloc] initWithNibName:Nil bundle:nil];
getlocation.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
getlocation.rwanswer=self.rwanswer.text;
[self presentModalViewController:getlocation animated:YES];
[getlocation release];
}
in the first viewcontroller then in the view that is uploading to the server I synthesize the string.
NSString *rwanswer;
@property (nonatomic, copy) NSString *rwanswer;
I can see the text with NSLog but for some reason I cannot get it to upload.
-(IBAction)UploadData :(id)sender {
NSString *appname = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDisplayName"];
NSString *result =rwanswer;
NSLog(@"result=%@", result);
NSString *comment =comments.text;
NSString *name = username.text;
NSLog(@"name=%@", name);
NSString *Website =[NSString stringWithFormat:@"http://www.ivectorn.com/Set/Submit.php?name=%@&comments=%@&appname=%@&results=%@", name, comment, appname, result];
[BackroundLoader loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:Website]]];
Any help would be great
回答1:
I know this problem. I think the problem is by your Website-String. Try to Log int and you will see, that there are characters inside you cannot put in a URL. The following method will help you if this is your problem:
- (NSString *)stringByAddingPercentEscapesUsingEncoding:(NSStringEncoding)encoding
Instead of
NSString *appname = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDisplayName"];
NSString *result =rwanswer;
NSLog(@"result=%@", result);
NSString *comment =comments.text;
NSString *name = username.text;
To the following:
NSString *appname = [[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDisplayName"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; //Or your String Encoding
NSString *result = [rwanswer stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; //Or your String Encoding;
NSLog(@"result=%@", result);
NSString *comment =[comments.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; //Or your String Encoding;
NSString *name = [username.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; //Or your String Encoding;
I hope this will help you. I struggled with the same problem some time ago.
Sandro Meier
来源:https://stackoverflow.com/questions/5542011/problem-uploading-nsstring-to-server