【问题标题】:NSURLConnection ResponseNSURLConnection 响应
【发布时间】:2012-06-17 03:16:42
【问题描述】:

您好,我有这个代码。

NSString *urlToAuthPage = [[NSString alloc] initWithFormat:@"&name=%@&street=%@&city=%@&state=%@&zip=%@&lat=%@&lon=%@&hash=%@", name, street, city, state, zip, str1, str2, hash];

        NSData *postData = [urlToAuthPage dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:NO];
        NSString *postLength = [NSString stringWithFormat:@"%d",[urlToAuthPage length]];
        NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
        [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://oo.mu/partyapp/post-party.php"]]];
        [request setHTTPMethod:@"POST"];
        [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
        [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
        [request setHTTPBody:postData];        


        NSString *infoString = [NSString stringWithFormat:@"http://oo.mu/partyapp/post-party.php?name=%@&street=%@&city=%@&state=%@&zip=%@&lat=%@&lon=%@&hash=%@", name, street, city, state, zip, str1, str2, hash];
        NSURL *infoUrl = [NSURL URLWithString:infoString];
        NSData *infoData = [NSData dataWithContentsOfURL:infoUrl];
        NSError *error;
        responseDict = [NSJSONSerialization JSONObjectWithData:infoData options:0 error:&error];
        NSLog(@"%@", responseDict);

您可能注意到我有一些我知道的不需要的代码,但我对其他内容却得到了错误的响应。如何清理一些代码并从带有 urlToAuthPage 的请求中获取响应?

【问题讨论】:

  • 只试试这个 NSString *infoString = [NSString stringWithFormat:@"oo.mu/partyapp/…", name, street, city, state, zip, str1, str2, hash]; NSURL *infoUrl = [NSURL URLWithString:infoString]; NSData *infoData = [NSData dataWithContentsOfURL:infoUrl]; NSError *错误; responseDict = [NSJSONSerialization JSONObjectWithData:infoData options:0 error:&error]; NSLog(@"%@", responseDict);你在 responseDict 中得到什么

标签: iphone ios5 nsurlconnection


【解决方案1】:

我想我明白你在说什么:你知道如何使用 NSData 方法和 URL 来调用请求,但这不支持你想要提供请求正文的 POST。

另一个问题是,即使你让它工作,它也是同步的。 NSURLConnection 类中有一个很好的解决方案。像在代码的前半部分中那样构建您的请求(在正文中使用发布数据)。然后这样做:

[NSURLConnection sendAsynchronousRequest:request
    queue:[NSOperationQueue mainQueue]
    completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {

    // your data or an error will be ready here
}];

【讨论】:

    【解决方案2】:

    正如上面@danh 强调的那样,您可以使用 NSURLConnection 在同步和异步请求之间进行选择。我想提一下,你可以通过委托而不是块来实现异步,如果这对你来说似乎更平易近人的话。

    基本上,您可以实现 NSURLConnectionDelegate 和 NSURLConnectionDataDelegate 中的方法,然后将自己指定为连接的委托,并在这些协议中定义的回调期间做出适当的响应。

    [NSURLConnection connectionWithRequest:yourNSMutableURLRequest delegate:self];
    

    如果你经常这样做,你可以像我一样做,将所有异步的东西抽象到一个连接管理器中,它可以处理同步和异步请求(对于异步,它存储一个指向相关方的指针和请求完成时执行回调)。这是一个简单的接口,我在所有地方都使用它来处理这两种请求,并且它与进一步的抽象配合得很好(比如制作一个 webRequest 类来封装一个带有正文的 POST 请求)。

    【讨论】:

      猜你喜欢
      • 2012-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多