1

以下是我的 PHP 代码,它向 web 服务请求发送响应:

 $query = "select name,email,password from User where email='".$email."' AND password='".$password."'";
 $result = mysql_query($query);
 $num_rows = mysql_num_rows($result);


 if($num_rows==1)
 {
     sendResponse(200, json_encode("match"));
 }

 if($num_rows==0)
 {
     sendResponse(204, json_encode("not match"));
 }

以下是我的 iOS 代码:

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{   
    NSString *status = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(@" Returned Json data : %@",status);


    if ([status isEqualToString:@"match"])
    {
        AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];

        [appDelegate OpenTabController];
    }

    //Boolean flag = [status isEqualToString:@"not match"];

    //NSLog(@"flag : %d",flag);

    if ([status isEqualToString:@"not match"])
    {
        NSLog(@"not match");


        UIAlertView *alertView = [[UIAlertView alloc]
                                      initWithTitle:@"Invalid email or password"
                                      message:@""
                                      delegate:nil
                                      cancelButtonTitle:@"OK"
                                      otherButtonTitles:nil];
        [alertView show];

    }
}

线[status isEqualToString:@"not match"]不工作。

4

5 回答 5

2

首先,您要将字符串转换为 json 响应。在您的 PHP 代码中,使用关联数组创建一个键值对,然后在 iOS 中使用 JSON 解析器将数据转换为 JSON 格式。在转换后的字典上使用 objectForKey 方法

PHP 代码

if($num_rows==1)
 {
$status = "match";
$status_array = array("status"=>"$status");
     sendResponse(200, json_encode($status_array));
 }

 if($num_rows==0)
 {
$status = "not match";
$status_array = array("status"=>"$status");
     sendResponse(204, json_encode($status_array));
 }

iOS 代码使用下面的过程来解析 json 并根据需要获取相应的值。 http://www.raywenderlich.com/5492/working-with-json-in-ios-5

于 2013-06-06T06:59:34.863 回答
1

伙计们,我找到了解决方案:

php代码:

if($num_rows==1)
{
    $status = array(
    "status" => "match",
    );

    sendResponse(200, json_encode($status));
}

if($num_rows==0)
{
    $status = array(
    "status" => "not match",
    );

   sendResponse(204, json_encode($status));
}

iOS 代码:

NSDictionary *result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error: nil];

NSString *status = [result objectForKey:@"status"];
NSLog(@"Returned Json data : %@",status);
于 2013-06-06T07:06:35.047 回答
0

First You need to include the SBJson library to your project. Then import the json.h

#import "JSON.h"

Find the below code:

NSString *status = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSDictionary *dic = [status JSONValue];

This dictionary is your result.With this dictionary, you can check the value for your key.

于 2013-06-06T06:57:47.783 回答
0

PHP json_encode escaped space char. And it should make from 'not match' a json, like {'not match'}

于 2013-06-06T06:58:06.227 回答
0

您可以尝试调试并检查其中的值status是什么。我认为您的代码中的字符串比较没有任何问题。

于 2013-06-06T06:54:48.523 回答