0

我正在做一个项目,我使用 NSURLConnection 连接到网页,以便能够监视返回的状态代码(200 OK / 404 ERROR)。如果我收到 404 作为状态码,如果我收到 200 状态码,我想将用户发送到顶部 url www.domain.com,我想将页面加载到 web 视图中。我已经通过创建一个新请求看到了这个问题的几个实现,但我觉得这是不必要的,因为您已经在第一个请求中收到了 html,所以我只想将该 HTML 加载到 webView 中。

所以我尝试使用 [webView loadHTMLFromString: baseURL:] 但它并不总是有效,我注意到当我在 connectionDidFinnishLoading 中打印带有 html 的 NSString 时,它有时为空,当我通过在中打印 html 来监控这些情况时didReceiveData 最后一个数据包的随机数为NULL(2-10之间不同)。始终没有加载相同的网页。如果我使用 [webView loadRequest:myRequest] 将它们加载到我的 webView 中,它总是可以工作的。我的实现看起来像这样,也许你们中的某个人可以看到我做错了什么。

我通过单击按钮创建了我的第一个请求。

-(IBAction)buttonClick:(id)sender
{
    NSURL *url = [NSURL URLWithString:@"http://www.domain.com/page2/apa.html"];
    NSURLRequest *theRequest = [NSURLRequest requestWithURL:url]
    NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

    if( theConnection )
    {
        webData = [[NSMutableData data] retain];
    }
    else
    {

    }
}

然后我通过将请求转换为 NSHTTPURLResponse 来监视 didReceiveResponse 方法中的响应代码,以便能够访问状态代码,然后根据状态代码设置一个 Bool 。

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{     
    NSHTTPURLResponse *ne = (NSHTTPURLResponse *)response;
    if ([ne statusCode] == 200) {
        ok = TRUE;
    }

    [webData setLength: 0];
}

然后我检查 connectionDidFinnishLoading 中的布尔值。如果我记录 html NSString 我会得到网页的来源,所以我知道它不是一个空字符串。

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{

    NSString *html = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];

    NSURL *url = [NSURL URLWithString:@"http://www.domain.com/"];
    if (ok) {
        [webView loadHTMLString:html baseURL:url];
        ok = FALSE;
    }
    else {
        // Create a new request to www.domain.com
    }

}

webData 是一个实例变量,我像这样将它加载到 didReceiveData 中。

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [webData appendData:data];
}
4

2 回答 2

0

您正在混合执行连接的两种方式 - 同步和异步。如果异步在同步之前完成,则重新初始化webData为空字符串。因此,即使同步填充webData它也会被清除。

// you create asynchronous connection which starts downloading in background immediately
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
// you create another synchronous connection which will block until the URL is downloaded
[NSURLConnection sendSynchronousRequest:theRequest returningResponse: &responseHeader error: nil];

// assuming your theRequest is valid, theConnection is always non-nil, so this is always TRUE
if (theConnection) {
     // you reset content of webData, so if your synchronous connection has downloaded something, you throw it away here
     webData = [[NSMutableData data] retain];
} else ...

尝试删除不必要的/错误的调用sendSynchronousRequest:returningResponse:error:,它可能会起作用。

无论如何-您在哪里填充webData数据?您确实实施connection:didReceiveData:并附加了进来的内容webData,对吗?如果你没有 - 好吧,这就是你看到的问题。

于 2010-10-27T20:56:13.973 回答
0

我假设这webData是你班级的一个实例变量。我认为您的问题是您没有设置webDataNSURLConnection. 尝试这样的事情:

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [webData appendData:data];
}

如果这不起作用,请尝试在行NSLog()后添加一个NSString *html = [[NSString alloc]...,如下所示:

NSLog(@"HTML: %@", html);

这将告诉您是否正在获取 HTML,以及问题出在您的NSURLConnection代码还是您的UIWebView IBOutlet.

于 2010-10-27T20:58:29.977 回答