2

我必须使用 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 中做什么?

4

2 回答 2

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.

                                        }];
于 2012-05-03T13:31:22.737 回答
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 有数据库选择查询。

于 2012-05-04T04:13:00.853 回答