【问题标题】:Request from database using AFNetworking使用 AFNetworking 从数据库请求
【发布时间】:2014-02-06 23:19:20
【问题描述】:

我正在学习有关与应用程序进行基本数据库交互的教程,但遇到了一个小问题。它让我使用我在网上阅读的 ASIHTTPRequest API 已经过时,甚至 Xcode 都告诉我不要使用它。所以,我下载并安装了 AFNetworking。唯一的问题,作为一个遵循教程的菜鸟,我不知道现在该做什么。我的项目中有所有 Json、MBProgressHUD 和 AFNetworking 文件,但我无法将教程中的代码更改为 AFNetworking 友好代码。有人可以帮帮我吗?

这是教程中的代码:

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    NSLog(@"Want to redeem: %@", textField.text);

    // Get device unique ID
    UIDevice *device = [UIDevice currentDevice];
    NSString *uniqueIdentifier = [device uniqueIdentifier];

    // Start request
    NSString *code = textField.text;
    NSURL *url = [NSURL URLWithString:@"http://www.wildfables.com/promos/"];
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
    [request setPostValue:@"1" forKey:@"rw_app_id"];
    [request setPostValue:code forKey:@"code"];
    [request setPostValue:uniqueIdentifier forKey:@"device_id"];
    [request setDelegate:self];
    [request startAsynchronous];

    // Hide keyword
    [textField resignFirstResponder];

    // Clear text field
    textView.text = @"";

    return TRUE;
}

【问题讨论】:

    标签: objective-c json ios7 asihttprequest afnetworking-2


    【解决方案1】:

    您可以像这样使用AFNetworking POST

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSDictionary *parameters = @{@"foo": @"bar"};
    [manager POST:@"http://example.com/resources.json" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"JSON: %@", responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
    }];
    

    我还建议使用SVProgressHUD here,因为它更容易实现。

    并且,应用商店将不再接受使用使用 UDID 的应用。 Source

    【讨论】:

    • 如何将以下内容合并到您建议的代码中? [请求 setPostValue:@"1" forKey:@"rw_app_id"]; [请求 setPostValue:code forKey:@"code"]; [请求 setPostValue:uniqueIdentifier forKey:@"device_id"];当然减去 UDID
    • 您的请求只是字典键和值,因此您可以像示例一样使用NSDictionary 文字来完成。 NSDictionary *parameters = @{@"rw_app_id": @"1", @"code": code};
    猜你喜欢
    • 1970-01-01
    • 2010-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多