0

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

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

和 Post 参数是:

app_key(string),signature(string),timestamp(dateTime),user_id(string),password(string)。

可用的响应表示

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

{“状态”:“1”,“结果”:{“数据”:{“头像”:“”,“电子邮件”:“sd.p@clozette.co”,“用户名”:“sd”,“通讯“:”1,“bio_data”:“关于我自己”,“mth_dob”:“1”,“yr_dob”:“1981”,“hometown”:“”,“dob_priv”:“1”,“hometown_priv”: “1”,“bio_data_priv”:“1”,“block_comment_notification”:“0”,“block_liked_notification”:“0”,}

请帮我。提前致谢。

4

1 回答 1

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]];
于 2012-05-01T14:05:21.840 回答