Objective c 尝试在iOS应用程序中登录网站,没有JSON响应

Objective c 尝试在iOS应用程序中登录网站,没有JSON响应,objective-c,cocoa-touch,ios5,Objective C,Cocoa Touch,Ios5,我正在尝试登录到一个网站,并使用JSON获得响应,代码如下: @try { if([[txtUsername text] isEqualToString:@""] || [[txtPassword text] isEqualToString:@""] ) { [self alertStatus:@"Please enter both Username and Password" :@"Login Failed!"]; } else { NSStr

我正在尝试登录到一个网站,并使用JSON获得响应,代码如下:

@try {

    if([[txtUsername text] isEqualToString:@""] || [[txtPassword text] isEqualToString:@""] ) {
        [self alertStatus:@"Please enter both Username and Password" :@"Login Failed!"];
    } else {
        NSString *post =[[NSString alloc] initWithFormat:@"username=%@&password=%@",[txtUsername text],[txtPassword text]];
        NSLog(@"PostData: %@",post);

        NSURL *url=[NSURL URLWithString:@"https://yedion.afeka.ac.il/yedion/fireflyweb.aspx?prgname=login"];

        NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

        NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

        NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
        [request setURL:url];
        [request setHTTPMethod:@"POST"];
        [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
        [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
        [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
        [request setHTTPBody:postData];

        [NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]];

        NSError *error = [[NSError alloc] init];
        NSHTTPURLResponse *response = nil;
        NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

        NSLog(@"Response code: %d", [response statusCode]);
        if ([response statusCode] >=200 && [response statusCode] <300)
        {
            NSString *responseData = [[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
            NSLog(@"Response ==> %@", responseData);

            SBJsonParser *jsonParser = [SBJsonParser new];
            NSDictionary *jsonData = (NSDictionary *) [jsonParser objectWithString:responseData error:nil];
            NSLog(@"%@",jsonData);
            NSInteger success = [(NSNumber *) [jsonData objectForKey:@"success"] integerValue];
            NSLog(@"%d",success);
            if(success == 1)
            {
                NSLog(@"Login SUCCESS");
                [self alertStatus:@"Logged in Successfully." :@"Login Success!"];

            } else {

                NSString *error_msg = (NSString *) [jsonData objectForKey:@"error_message"];
                [self alertStatus:error_msg :@"Login Failed!"];
            }

        } else {
            if (error) NSLog(@"Error: %@", error);
            [self alertStatus:@"Connection Failed" :@"Login Failed!"];
        }
    }
}
@catch (NSException * e) {
    NSLog(@"Exception: %@", e);
    [self alertStatus:@"Login Failed." :@"Login Failed!"];
}
@试试看{
if([[txtUsername文本]IseQualtString:@”“][[txtPassword文本]IseQualtString:@”“])){
[自我提醒状态:@“请同时输入用户名和密码”:@“登录失败!”;
}否则{
NSString*post=[[NSString alloc]initWithFormat:@“用户名=%@&密码=%@”,[TXTSERNAME文本],[TXTSPASSWORD文本];
NSLog(@“PostData:%@”,post);
NSURL*url=[NSURL URLWithString:@”https://yedion.afeka.ac.il/yedion/fireflyweb.aspx?prgname=login"];
NSData*postData=[post数据使用编码:NSASCIIStringEncoding allowLossyConversion:是];
NSString*postLength=[NSString stringWithFormat:@“%d”,[postData长度]];
NSMutableURLRequest*请求=[[NSMutableURLRequest alloc]init];
[请求设置url:url];
[请求设置HttpMethod:@“POST”];
[请求设置值:HttpHeaderField的postLength:@“内容长度”];
[请求设置值:@“应用程序/json”用于HttpHeaderField:@“接受”];
[请求设置值:@“应用程序/x-www-form-urlencoded”forHTTPHeaderField:@“内容类型”];
[请求setHTTPBody:postData];
[NSURLRequest setAllowsAnyHttpSCCertificate:YES for主机:[url主机]];
NSError*error=[[NSError alloc]init];
NSHTTPURLResponse*响应=nil;
NSData*urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&响应错误:&错误];
NSLog(@“响应代码:%d”,[Response statusCode]);

如果([response statusCode]>=200&&[response statusCode]代码对我来说似乎没问题,但请检查web服务,并检查您是否为json提供了正确的关键字如果提供给objectForKey的密钥和您在web服务中的密钥不同,您将永远不会得到json响应。

使用get方法并重试

[ request setHTTPMethod:@"GET" ];

这与Xcode有什么关系?因为我正在为iOS应用程序在Xcode中创建一个登录屏幕。不。这与Xcode无关。Xcode只是一个IDE,它与一般的iOS编程问题无关。请阅读它的标记wiki以了解更多信息。你也在使用Mac来这样做,这不是使用标记的好理由
OSX