【问题标题】:How to connect (GET request) ios app to django rest framework如何将(GET请求)ios应用程序连接到django rest框架
【发布时间】:2014-08-13 19:35:37
【问题描述】:

我进入了我的应用程序开发的最后阶段,这是我以前从未做过的事情。 我的朋友使用 django rest 框架为我的应用程序开发了发送和接收数据的 API。 我需要对我的应用进行身份验证才能连接到它、发送一些数据和接收数据。

到目前为止我发现的是:

NSURL *url = [NSURL URLWithString:@"http://localhost:8080/my/path/to/api/login/"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    NSString *authStr = [NSString stringWithFormat:@"%@:%@", @"myUsername", @"myPassword"];
    NSData *authData = [authStr dataUsingEncoding:NSASCIIStringEncoding];
    NSString *authValue = [NSString stringWithFormat:@"Basic %@", authData];
    [request setValue:authValue forHTTPHeaderField:@"Authorization"];
    [request setHTTPMethod:@"GET"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];

//--------------------------------------------- -------------------------------------------------- -------------------------------------------------//

    //EDIT: Added this based on answer form @Quver.
    NSURLResponse *response1;
    NSError *responseError;

NSData *result = [NSURLConnection sendSynchronousRequest:request returningResponse:&response1 error:&responseError];

if (result.length > 0 && responseError == nil)
{
    NSDictionary *greeting = [NSJSONSerialization JSONObjectWithData:result
                                                             options:0
                                                               error:NULL];
    NSLog(@"Got response form server: %@", greeting);

}

这等于输出如下:

//--------------------------------------------- -------------------------------------------------- ------------------------------------------------------//

我想这是创建请求的方式。接下来我该怎么办?我怎么知道我已经连接了? 那么,如果我已经连接,我如何在那里获取数据? (我有一个 url,它给我 json 作为输出 - 这就是我想要得到的)。假设网址为http://localhost:8080/url/that/gives/json/

感谢您的帮助。希望这是该问题的足够信息。我会添加任何其他需要的内容。

【问题讨论】:

  • 你有没有研究过AFNetworking?它使所有这些 URL 请求变得更加容易,并且是一个非常常用的 Pod。

标签: ios django xcode django-rest-framework


【解决方案1】:
NSURLResponse *response;
NSError *responseError;

NSData *result = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&responseError];

添加此项以获得响应。你已经准备好了请求,现在是时候用 NSURLConnection 发送它了。我同步请求是异步的,因为使用 GCD 进行整个方法请求 + sqlite 更新。

【讨论】:

  • 嗨。谢谢你。这帮助我得到了以下形式的响应:
  • 因为这是 NSData 如果你想要字符串使用NSString *stringResults = [[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding]; NSLog(@"stringResults dump = %@", stringResults);
【解决方案2】:

经过几天的搜索,我找到了一种方法。 首先,我们需要一个用于身份验证的令牌。我现在通过终端生成它:

curl -X POST -d "grant_type=password&username=<your username>&password=<your password>" http://<client secret>:<client id>@url/to/token/page

然后,在您要连接的视图控制器中:

//always put </> at the end of link
    NSURL *aUrl = [NSURL URLWithString: @"http://where/you/trying/to/conect"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:aUrl
                                                           cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                       timeoutInterval:30.0];

    [request addValue:[NSString stringWithFormat:@"<type> <your token>"] forHTTPHeaderField:@"Authorization"];

    [request setHTTPMethod:@"GET"];

    NSError *error = nil;
    self.response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error: &error];

这将返回页面应该返回的任何 json。这可能不是最安全的方式,但这正是我现在需要的。稍后我将介绍更安全的解决方案,例如 OAuth 2。

希望这对某人有所帮助。

【讨论】:

    猜你喜欢
    • 2016-07-20
    • 1970-01-01
    • 1970-01-01
    • 2022-11-07
    • 1970-01-01
    • 2022-10-18
    • 2014-08-07
    • 1970-01-01
    • 2021-05-12
    相关资源
    最近更新 更多