如何使用用户名和密码将JSON对象发送到iphone中的REST登录webservice?

时间:2012-05-03 12:19:27

标签: iphone json web-services rest

我必须使用JSON对象发送用户名和密码才能登录REST Web服务。我在android中用以下代码完成了。

HttpClient client = new DefaultHttpClient();
                    String responseBody;
                    JSONObject jObj = new JSONObject();

                    try
                    {
                        HttpPost post = new HttpPost("http://localhost:8080/MTA/login");
                        jObj.put("email", "user@email.com");
                        jObj.put("password", "password12345");

                        StringEntity se = new StringEntity(jObj.toString());  

                        post.setEntity(se);
                        post.setHeader(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
                        post.setHeader("Content-type", "application/json");
                        System.out.println("webservice request executing");

                        ResponseHandler responseHandler = new BasicResponseHandler();
                        responseBody = client.execute(post, responseHandler);

                        System.out.println("Response : " + responseBody);
                        /* 
                         * You can work here on your responseBody
                         * if it's a simple String or XML/JSON response
                         */

                    }
                    catch(Exception e)
                    {
                        System.out.println("Exception : " + e);
                    }

我必须在iOS中做同样的事情。我怎么能在iOS上做?

2 个答案:

答案 0 :(得分:2)

我正在使用Ge.tt API,为了登录,我正在这样做:

NSString *email = @"Some email";
NSString *password = @"Some password"
NSString *apikey = @"some api key";
NSString *loginURL = @"http://open.ge.tt/1/users/login";

NSURL *url = [NSURL URLWithString:loginURL];

NSString *JSONString = [NSString stringWithFormat:@"{\"apikey\":\"%@\",\"email\":\"%@\",\"password\":\"%@\"}", apikey, email, password];

NSData *JSONBody = [JSONString dataUsingEncoding:NSUTF8StringEncoding];

NSMutableURLRequest *loginRequest = [[NSMutableURLRequest alloc] initWithURL:url];
loginRequest.HTTPMethod = @"POST";
loginRequest.HTTPBody = JSONBody;

NSOperationQueue *queue = [NSOperationQueue new];

[NSURLConnection sendAsynchronousRequest:loginRequest 
                                   queue:queue 
                       completionHandler:^(NSURLResponse *response, NSData *data, NSError *error){

                                        // Manage the response here.

                                        }];

答案 1 :(得分:1)

将此代码用于您的网络服务,如果它有效,请通知我们,或者您有任何问题请问我。

-(IBAction)Submit
 {


if(([user.text length]==0) || ([password.text length]==0) || ([eMail.text length]==0) || ([contactNo.text length]==0))
{
    UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Alert !!!" message:@"Fill All the Text For Registration!!" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil, nil];
    [alert show];
    [alert release]; 

}

else 
{
    if([self validateEmailWithString:eMail.text])
    {
        NSString *regestrationString=[[NSString alloc]initWithFormat:@"http://www.somename.com/foldername/login.php?user_name=%@&user_password=%@&user_emailid=%@&user_contactno=%d",user.text,password.text,eMail.text,contactNo.text];
        NSURLRequest *regestrationRequest=[[NSURLRequest alloc]initWithURL:[NSURL URLWithString:regestrationString]];
        NSURLResponse *regestrationResponce;
        NSError *error;

        NSMutableData *regestrationData=[[NSMutableData alloc]initWithData:[NSURLConnection sendSynchronousRequest:regestrationRequest returningResponse:&regestrationResponce error:&error]];
        NSString *dataString=[[NSString alloc]initWithData:regestrationData encoding:NSUTF8StringEncoding];
        NSLog(@"%@",dataString);
        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Thank You!!!" message:@"Your Registration is completed!!" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
        [alert show];
        [alert release];        
        [registrationView setHidden:YES];
    }
    else
    {
        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:nil message:@"Invalid Email" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
        [alert show];
        [alert release]; 
    }
}



 }

 -(IBAction)login
 {
NSString *regestrationString=[[NSString alloc]initWithFormat:@"http://www.somename.com/foldername/login_table.php?user_name=%@&user_password=%@",userName.text,passWord.text];
NSURLRequest *regestrationRequest=[[NSURLRequest alloc]initWithURL:[NSURL URLWithString:regestrationString]];
NSURLResponse *regestrationResponce;
NSError *error;

NSMutableData *regestrationData=[[NSMutableData alloc]initWithData:[NSURLConnection sendSynchronousRequest:regestrationRequest returningResponse:&regestrationResponce error:&error]];
NSString *dataString=[[NSString alloc]initWithData:regestrationData encoding:NSUTF8StringEncoding];
NSLog(@" %@",dataString);

regestrationArray=[[NSArray alloc]init];
regestrationArray=[dataString JSONValue];
if(regestrationArray == NULL)
{
    UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Alert !!!" message:@"Invalid Login" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil, nil];
    [alert show];
    [alert release];        
}

userNameField =(NSString *)[NSString stringWithFormat:@"%@",[[regestrationArray objectAtIndex:0]valueForKey:@"user_name"]] ;



userName.text=@"";
passWord.text=@"";


[signInView setHidden:YES];
}

login.php有你的数据库插入查询,login_table.php有数据库选择查询。