如何获得服务器响应以及服务器响应是否为“成功”在使用目标c通过JSON显示警报之后 [英] how to get server response and if server response is "Success" after show alert by JSON using objective c

查看:96
本文介绍了如何获得服务器响应以及服务器响应是否为“成功”在使用目标c通过JSON显示警报之后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

NSString *data = [NSString stringWithFormat:@"username=%@&password=%@",usertxt.text,pwdtxt.text,[NSNumber numberWithBool:YES]];
    NSData *postData = [data dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
    NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

//预先发送URL请求以发送数据。

// preaparing URL request to send data.

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;
connection=[[NSURLConnection alloc] initWithRequest:request delegate:self];
NSString *url = [NSString stringWithFormat:URL_PATH];
[request setURL:[NSURL URLWithString:url]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Accept"];
[request setHTTPBody:postData];
[request setTimeoutInterval:7.0];

NSURLResponse *_response;
NSError *error;
NSData *urlData = [NSURLConnection sendSynchronousRequest:request returningResponse:&_response error:&error];
NSString *str=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];

//打印服务器响应

NSLog(@"Login response:%@",str);

//将JSON字符串解析为NSDictionary

//Parse JSON string into an NSDictionary

 NSDictionary* json = [NSJSONSerialization JSONObjectWithData:urlData
                                                     options:kNilOptions
                                                      error:&error]; 


推荐答案

试试这个

Don'需要使用JSON只需尝试此代码即可从服务器获取响应

Try This
Don't need to use JSON simply try this code to get response from server

NSString *string= [[NSString alloc]initWithFormat:@"url"];
        NSLog(@"%@",string);
        NSURL *url = [NSURL URLWithString:string];
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
        [request setHTTPMethod:@"POST"];

        NSURLResponse *response;
        NSError *err;
        NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
        NSLog(@"responseData: %@", responseData);
        NSString *str = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
        NSLog(@"responseData: %@", str);
        NSString *str1 = @"1";
        if ([str isEqualToString:str1 ])
        {

            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Successfully" message:@"" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
            [alert show];
        }
        else
        {
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Try Again" message:@"" delegate:self cancelButtonTitle:@"Try Later" otherButtonTitles:@"Call", nil];
            alert.tag = 1;
            [alert show];
        }

如果您的回复字符串是成功而不是把它放在 @1的位置

if your response string is success than put this in the place of @"1"

这篇关于如何获得服务器响应以及服务器响应是否为“成功”在使用目标c通过JSON显示警报之后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆