JSON中的POST用户名和密码

时间:2013-02-01 10:16:58

标签: iphone ios json web-services url

我有一个网络服务,我正在处理一个网址,例如

//myurl/index.jsp?user_name=bob&user_pwd=new

如您所见,用户名已设为“bob”,密码为“new”。输入时,网站会提供此json文件,

[{"success":"1"}]

如何在xcode上实现它,当用户输入"bob"作为用户名,"new"作为密码时,它应该指向下一个控制器。我怎样才能做到这一点?

我跟着这个tutorial,虽然它不完全一样,你是怎么做到的。感谢。

5 个答案:

答案 0 :(得分:2)

使用导航控制器并将视图控制器设置为rootViewController。然后,在用户输入凭证后,将新视图控制器推送到导航堆栈。

答案 1 :(得分:0)

从json响应中获取成功的价值。如果它等于1则推送到下一个控制器,否则什么都不做。

答案 2 :(得分:0)

在以下代码中,您可以通过GETPOST方法传递数据...使用任何一个( 如您所愿

-(void) sendRequest
{
  //////////////////////////// GET METHOD  /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

   NSString *strURL=[NSString stringWithFormat:@"http://myurl/index.jsp?user_name=bob&user_pwd=new"];
    self.request=[NSURLRequest requestWithURL:[NSURL URLWithString:strURL]];
    self.nsCon=[[NSURLConnection alloc] initWithRequest:request delegate:self];

 //////////////////////////// POST METHOD  /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    NSString *postString = [NSString stringWithFormat:@"&user_name=bob&user_pwd=new"];
    NSString *url = [NSString stringWithFormat:@"http://myurl/index.jsp/"];

    self.request =[NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
    [self.request setHTTPMethod:@"POST"];

    [self.request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];

 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    if(self.nsCon)
    {
        self.receivedData=[[NSMutableData alloc] init];
    }
    else
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Error",@"") message:NSLocalizedString(@"Not Connected !!",@"") delegate:nil cancelButtonTitle:NSLocalizedString(@"OK",@"") otherButtonTitles:nil];
        [alert show];
        [alert release];
    }
}

#pragma mark -
#pragma mark - Connection Delegate Methods

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    [self.responseData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [self.responseData appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    [GeneralClass stopHUD];
    NSLog(@"Connection failed.");
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Error",@"") message:NSLocalizedString(@"Connection failed!",@"") delegate:nil cancelButtonTitle:NSLocalizedString(@"OK",@"") otherButtonTitles:nil];
    [alert show]; alert = nil;
}

 - (void)connectionDidFinishLoading:(NSURLConnection *)connection
    {
        NSString *responseString = [[NSString alloc] initWithData:self.responseData encoding:NSUTF8StringEncoding];
        NSMutableDictionary *receivedData = [responseString JSONValue];

        if([[receivedData objectForKey:@"success"]  isEqualToString:@"1"])
        {
            MainDetailViewController *mdController = [[MainDetailViewController alloc] init];
            [self.navigationController pushViewController:mdController animated:YES];
        }
        else
        {
            NSLog(@"%@",receivedData);
        }

    }

答案 3 :(得分:0)

发布登录数据

    NSString *soapMsg = [NSString stringWithFormat:@"&data={\"LoginUser\":[{\"UserName\":\"%@\",\"Password\":\"%@\"}]}",firstname.text, password.text];

    NSHTTPURLResponse *response;
    NSData *myRequestData = [ NSData dataWithBytes: [ soapMsg UTF8String ] length: [ soapMsg length ] ];
    NSString *postLength = [NSString stringWithFormat:@"%d", [myRequestData length]];
    NSMutableURLRequest *request = [ [ NSMutableURLRequest alloc ] initWithURL: [ NSURL URLWithString:@"http://myurl/index.jsp"]];

    [request setHTTPMethod: @"POST" ];
    [request setHTTPBody: myRequestData ];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody: myRequestData];

    NSURLConnection *myConnection=[[NSURLConnection alloc] initWithRequest:request delegate:self];

接收并有效的json数据

 - (void)connectionDidFinishLoading:(NSURLConnection *)connection {
        NSString *responseString = [[NSString alloc] initWithData:WebXmlData encoding:NSUTF8StringEncoding];
        NSDictionary *results = [responseString JSONValue];
        BOOL success = [[results objectForKey:@"success"] boolValue];

        if (success) {
             ViewController *viewController =[[ViewController alloc]initWithNibName:@"ViewController" bundle:nil];
             [self.navigationController pushViewController:viewController animated:YES];    
        }


    }

答案 4 :(得分:0)

不是您正在寻找的答案,但这更适合您应该使用的建议。 我使用github link进行与网络相关的活动。 他们有很好的文档,非常容易使用(使用块)

NSDictionary *paramsDictionary = [NSDictionary dictionaryWithObjectsAndKeys:@"user",@"client_id",@"password",@"new", nil];

    [[YourAFHttpClientExtenstion sharedInstance] postPath:LOGIN_PATH parameters:paramsDictionary success:^(AFHTTPRequestOperation *operation, id responseObject) {
        //handle success

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
       // handle error.
    }];

YourAFHttpClientExtenstion正在扩展AFHttpClient并添加了一个共享实例的召集方法并实现了initWithBaseUr: