2012-05-01 39 views
0

后请求帮助我必须在iOS中访问Rest api,我正在使用RestKit。服务器只接受POST请求并以JSON发送响应。只需帮助我使用RestKit调用登录API。网址是这样的:要求与RestKit

http://mobile.domian.co/apiversion/user/login

和邮政parametersare:

APP_KEY(串),签名(串),时间戳(日期时间),USER_ID(串),密码(字符串)。

可用响应申述

{ 
"status": "0", 
"result": { 
    "error_code": "1", 
    "error_msg": "Login failed. Wrong username or password" 
} 

{ “状态”: “1”, “结果”:{ “数据”:{ “阿凡达”: “”, “电子邮件” :“[email protected]”, “user_name”:“sd”, “newsletter”:“1”, “bio_data”:“关于我自己”, “mth_dob”:“1”, “ yr_dob“:”1981“, ”hometow N“: ”“, ”dob_priv“: ”1“, ”hometown_priv“: ”1“, ”bio_data_priv“: ”1“, ‘block_comment_notification’:” 0” , ‘block_liked_notification’:” 0” , }

请帮帮我。提前致谢。

回答

0

下面是答案(你应该将时间值转换为字符串): -

 NSString *post =[NSString stringWithFormat:@"app_key=%@&signature=%@&timestamp=%@&user_id=%@&password=%@",appKEY,Signature,Datetime,UID,Password]; 

    NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES]; 

    NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]]; 

    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease]; 
    [request setURL:[NSURL URLWithString:@"Your URL"]]; 
    [request setHTTPMethod:@"POST"]; 
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
    [request setValue:@"application/x-www-form-urlencoded charset=utf-8" forHTTPHeaderField:@"Content-Type"]; 
    [request setHTTPBody:postData]; 

    NSError *error; 
    NSURLResponse *response; 
    NSData *returnData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; 
    if (error) 
{ 
    NSLog(@"Error is %@",[error localizedDescription]); 
} 

    returnString=[[NSString alloc]initWithData:returnData encoding:NSUTF8StringEncoding]; 
    if(!returnString) 
    { 
     UIAlertView *erroralert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"There is an error getting response" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil]; 
     [erroralert show]; 
     [erroralert release]; 
    } 
    else 
    { 
     NSLog(@"%@",returnString); 
    } 
    SBJSON *json = [[SBJSON new] autorelease]; 
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] initWithDictionary:[json objectWithString:returnString error:nil]]; 
+0

感谢您的答复。我使用了该代码但不工作。 我应该在那里调用委托方法吗? – makboney

+0

它如何不工作意味着你没有得到回应?拼写检查参数符咒。参数必须与您在URL中调用的Web服务匹配。 – Dhawal

+0

嗨dh14-sl,我不知道那里发生了什么错误。如果我做了什么错误,至少会有一些消息来自服务器。 NSData在这里是0字节。 – makboney