简体   繁体   English

如何在iPhone中将NSURLconnection http响应数据与字符串值进行比较?

[英]how to compare a NSURLconnection http response data with a string value in iphone?

i am new in iphone programming......i want to do an activation module which will send a http request with a PIN numbr and then read the response...if the response is "OK" it opens up with a main menu...........the problem is that i am recieving the response as "OK" but i am unable to compare it with a NSString @"OK"............so how to compare the http response with a string...........please give ur suggestions...thanks. 我是iPhone编程的新手...我想做一个激活模块,它将发送一个PIN码为num的http请求,然后读取响应...如果响应为“ OK”,它将打开一个主窗口菜单...........问题是我收到的响应为“确定”,但我无法将其与NSString @“确定”进行比较........... .so如何将http响应与字符串进行比较...............请给我们建议...谢谢。

Here is my piece of code...... 这是我的代码……

-(IBAction) submitPINAction:(id) sender { printf("inside submit btn"); -(IBAction)SubmitPINAction:(id)发送者{printf(“内部提交btn”); mydata = [NSMutableData alloc]; mydata = [NSMutableData alloc]; NSURLRequest *request = [NSURLRequest requestWithURL: [NSURL URLWithString:@" http://192.168.100.3/WWTF/activationApp.php?PIN=11111 "] NSURLRequest * request = [NSURLRequest requestWithURL:[NSURL URLWithString:@“ http://192.168.100.3/WWTF/activationApp.php?PIN=11111 ”]

                                          cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
NSURLConnection *connection=[[NSURLConnection alloc] initWithRequest:request delegate:self];
if(connection)
{
    mydata = [[NSMutableData data] retain];
}
else
{
    //Handle error : Not received data
    printf("No internet connection");
}

} }

-(void)connection:(NSURLConnection *)theConnection didReceiveData:(NSData *)incrementalData { printf("data received"); -(void)connection:(NSURLConnection *)theConnection didReceiveData:(NSData *)incrementalData {printf(“接收到的数据”); if (mydata==nil) { mydata = [[NSMutableData alloc] initWithCapacity:2048]; 如果(mydata == nil){mydata = [[NSMutableData alloc] initWithCapacity:2048]; } [mydata appendData:incrementalData]; } [mydata appendData:incrementalData]; NSString *temp = [[NSString alloc] initWithData:mydata encoding:NSASCIIStringEncoding]; NSString * temp = [[NSString alloc] initWithData:mydata编码:NSASCIIStringEncoding];

NSString *string1 = @"OK";


NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];

if([string1 isEqualToString:temp]){   // HERE IS THE PROBLEM, THE STRINGS ARE NOT GETTIN COMPARED

    [prefs setBool:TRUE  forKey: @"activationKey"];
    // show the main menu
    mainMenuController *mmC = [[mainMenuController alloc]initWithNibName:@"mainMenu" bundle:[NSBundle mainBundle]];
    self.mmainMenuController = mmC;
    [mmC release];
    [self.view addSubview:[mmainMenuController view]];
}
else{
    printf("in else");
    [prefs setBool:FALSE  forKey: @"activationKey"];
    //show an alert
    UIAlertView *alertActivationFail = [[UIAlertView alloc] initWithTitle:@"Activation Failed!" message:@"PIN is Incorrect" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alertActivationFail show];
    [alertActivationFail release];
}

} }


I would use the following line, to see that the response is actually the OK string, cause it may contain white space or other characters. 我将使用以下行,以查看响应实际上是OK字符串,因为它可能包含空格或其他字符。

NSLog(@"string1 = '%@' temp = '%@'", string1, temp);

put this line before the if statement like this... 将此行放在这样的if语句之前...

NSString *string1 = @"OK";
NSLog(@"string1 = '%@' temp = '%@'", string1, temp);
if([string1 isEqualToString:temp]){   // HERE IS THE PROBLEM, THE STRINGS ARE NOT GETTIN COMPARED

If extra whitespace is the reason why the comparison is failing then you can trim whitespace from you string by using the following method: 如果多余的空格是比较失败的原因,则可以使用以下方法从字符串中修剪空格:

+(NSString *)trim:(NSString *)value {
    return [value stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
} 

Another thing I would make sure is that you've received all the data before you check for the string. 我还要确保的另一件事是,在检查字符串之前,您已经收到了所有数据。 It looks like you do the string checking in didReceiveData , but it may make more sense to do the check in connectionDidFinishLoading after the data is complete. 看起来您在didReceiveData进行了字符串检查,但是在数据完成之后在connectionDidFinishLoading进行检查可能更有意义。

Thank you Korros and danvin for helping me out. 感谢Korros和danvin帮助我。 The problem was indeed with whitespaces. 问题确实与空白有关。 I trimmed the whitespaces as you said and it worked. 我按照您所说的修剪了空白,并且行得通。

and Thanks stackoverflow.com . 并感谢stackoverflow.com。 It is really a great forum.I find helpful ansers for all iPhone related topics, at the same time it also helps other platform as well i believe. 这确实是一个很棒的论坛。我发现所有iPhone相关主题的有用答案,同时也对其他平台有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM