NSURLConnection JSON不发布数据

时间:2013-02-24 15:21:31

标签: php iphone xcode json nsurlconnection

我正在我的iPhone应用程序中创建登录页面,我使用Json调用PHP页面连接到MySql数据库。

我有与PHP页面的连接,但我的程序没有发布任何内容。

- (IBAction)login:(id)sender{


        //Alloc dictionary
        jsonDictionaryLogin = [[NSDictionary alloc] initWithObjectsAndKeys: securityCode, @"hash_app", loginTextField.text, @"app_email", passwordTextField.text, @"app_password", nil];

        NSError *jsonError = nil;
        jsonDataLogin = [NSJSONSerialization dataWithJSONObject:jsonDictionaryLogin
                                                        options:kNilOptions error:&jsonError];

        if(!jsonError) {

            NSString *serJSON = [[NSString alloc] initWithData:jsonDataLogin encoding:NSUTF8StringEncoding];
            NSLog(@"\n\nSerialized JSON: %@", serJSON);

        } else {

            NSLog(@"JSON Encoding Failed: %@", [jsonError localizedDescription]);
        }


        NSURL *url = [NSURL URLWithString:@"http://www.mywebsite.com/mypage.php"];

        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0];



        [request setHTTPMethod:@"POST"];
        [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
        [request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
        [request addValue:[NSString stringWithFormat:@"%d", [jsonDataLogin length]] forHTTPHeaderField:@"content-Length"];

        [request setHTTPBody:jsonDataLogin];
        NSLog(@"jsonData: %@",jsonDataLogin);


        NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:true];

        [connection start];   
    }

之后,在我的委托方法中:

-   (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)theData{

    NSLog(@"String sent from server %@",[[NSString alloc] initWithData:theData encoding:NSUTF8StringEncoding]);

}

我的PHP代码:

<?
    $hash = $_POST['hash_app'];

    echo json_encode(array("TEST" => "swrao")); //RETURN swrao

    echo json_encode($hash); //RETURN null

        if($hash == "string_sended")
    {

         echo json_encode(array("TEST" => "YOU ARE IN")); //NO RETURN

        }


?>

来自NSLog的字符串给出了我在PHP页面中编写的一些消息,但PHP页面没有收到所发送的数据。

有些想法?

0 个答案:

没有答案