2

REST Webサービスにログインするには、ユーザー名とパスワードを送信するためにJSONオブジェクトを使用する必要があります。私は次のコードでアンドロイドでやりました。

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 に答える