我必须在iOS中访问Rest api,我正在使用RestKit。服务器只接受POST请求并以JSON发送响应。只是帮我用RestKit调用登录api。网址如下:
http://mobile.domian.co/apiversion/user/login
和帖子参数:
APP_KEY(字符串),签名(字符串),时间戳(日期时间),USER_ID(字符串),密码(字符串)。
可用的回复表示
{
"status": "0",
"result": {
"error_code": "1",
"error_msg": "Login failed. Wrong username or password"
}
{ “状态”:“1”, “结果”:{ “数据”:{ “avatar”:“”, “email”:“sd.p@clozette.co”, “user_name”:“sd”, “时事通讯”:“1”, “bio_data”:“关于我自己”, “mth_dob”:“1”, “yr_dob”:“1981”, “故乡”:“”, “dob_priv”:“1”, “hometown_priv”:“1”, “bio_data_priv”:“1”, “block_comment_notification”:” 0” , “block_liked_notification”:” 0” , }
请帮帮我。提前谢谢。
答案 0 :(得分:0)
这是答案(您应该将日期时间转换为字符串): -
NSString *post =[NSString stringWithFormat:@"app_key=%@&signature=%@×tamp=%@&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]];