【问题标题】:Read NSURLresponse阅读 NSURL 响应
【发布时间】:2011-11-07 13:05:38
【问题描述】:

我正在向这个地址发送一个对象:https://sandbox.itunes.apple.com/verifyReceipt

NSUrlconnection 我正在尝试使用此委托方法阅读它:

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 像这样:

NSlog(@"%@",response); 我得到这个代码:

<NSHTTPURLResponse: 0x7d2c6c0> 我需要以某种方式得到一个字符串。 怎么读?

【问题讨论】:

    标签: ios objective-c xcode cocoa-touch nsurlconnection


    【解决方案1】:

    我为另一个问题写了这个答案,但我认为它会对你有所帮助。特别看一下方法

    -(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data

    -(void) connectionDidFinishLoading:(NSURLConnection *)connection


    -(void) requestPage
    {
        NSString *urlString = @"http://the.page.you.want.com";
        NSURL *url = [NSURL URLWithString:urlString];
    
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLCacheStorageAllowed timeoutInterval:20.0f];
    
    
        responseData = [[NSMutableData alloc] init];
        connection = [[NSURLConnection connectionWithRequest:request delegate:self] retain];
        delegate = target;
    }
    
    
    -(void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
    {   
        if ([response isKindOfClass:[NSHTTPURLResponse class]])
        {
            NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*) response; 
            //If you need the response, you can use it here
        }
    }
    
    -(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
    {
        [responseData appendData:data];
    }
    
    -(void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
    {
        [responseData release];
        [connection release];
    }
    
    -(void) connectionDidFinishLoading:(NSURLConnection *)connection
    {
        if (connection == adCheckConnection)
        {
            NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
    
            //You've got all the data now
            //Do something with your response string
    
    
            [responseString release];
        }
    
        [responseData release];
        [connection release];
    }
    

    【讨论】:

    • 但是如何处理多个请求??例如,当连接正在进行并且在 connectiondidfinishloading 之前有另一个请求到来时,那么 responseData 可能会被释放,对吗??
    • @raghul 是的,这是可能的。为了避免这种情况,我可能会将它包装在一个类“ConnectionHandler”中,这样​​每个连接都会有它自己的responseData 实例
    【解决方案2】:
    NSHTTPURLResponse * httpResponse = (NSHTTPURLResponse *) response;
    int errorCode = httpResponse.statusCode;
    NSString *fileMIMEType = [[httpResponse MIMEType] lowercaseString];
    

    欲了解更多信息,请查看 iOS 文档:NSHTTPURLResponse

    请耐心等待:并非所有连接都返回NSHTTPURLResponse

    【讨论】:

    • 我得到了 200 个,这还可以……但是我如何读取苹果服务器的响应?
    • 200 表示您的连接已正确建立,您可以开始接收数据了
    【解决方案3】:

    如果您希望连接正在接收一些您可以使用的数据。

    - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
    

    然后您可以简单地将数据转换为NSString

    【讨论】:

      【解决方案4】:

      您可以创建一个子类并覆盖- (NSString*) description 方法。

      【讨论】:

        猜你喜欢
        • 2017-09-11
        • 1970-01-01
        • 2021-11-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-09-19
        • 2020-02-12
        • 2019-09-05
        相关资源
        最近更新 更多