【问题标题】:Asking for help in post request with RestKit使用 RestKit 在 post 请求中寻求帮助
【发布时间】:2012-05-01 13:27:23
【问题描述】:

我必须在 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", “家乡”:“”, “dob_priv”:“1”, "hometown_priv": "1", “bio_data_priv”:“1”, “block_comment_notification”:“0”, “block_liked_notification”:“0”, }

请帮助我。提前致谢。

【问题讨论】:

    标签: iphone objective-c ios ipad restkit


    【解决方案1】:

    这是答案(您应该将日期时间转换为字符串):-

            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]];
    

    【讨论】:

    • 感谢您的回复。我使用了该代码但无法正常工作。我应该在那里调用委托方法吗?
    • 它怎么不工作意味着你没有得到回应?请逐个拼写检查参数。参数必须与您在 URL 中调用的 Web 服务相匹配。
    • 嘿 dh14-sl,我不知道那里出了什么问题。如果我做错了什么,至少会有一些消息来自服务器。 NSData 在这里是 0 字节。
    • 它无论如何都可以工作......创建标题字符串时出错。我使用“,”的地方有“&”。谢谢...答案被接受:)
    • 抱歉这个错误,其实,感谢您纠正我的回答!我按照您的建议进行了编辑。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-27
    • 2020-11-21
    • 2018-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-07
    相关资源
    最近更新 更多