3

次のコードを使用して、アプリケーションでデータをダウンロードしようとしています

NSURL *url = [NSURL URLWithString:@"my download url string"];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60.f];

NSURLConnection * connection = [NSURLConnection connectionWithRequest:request delegate:self];

[connection start];

しかし、問題は時々次のエラーが発生することです

Error Domain=NSURLErrorDomain Code=-1001 "The request timed out." UserInfo=0x1d5be240 
{NSErrorFailingURLStringKey=http://dr282zn36sxxg.cloudfront.net/datastreams/f-
d%3Afc7f649e1e3ba58452f67e3fa1f66f69a15b96b3ea585c946e4fa854%2BEPUB%2BEPUB.1, 
NSErrorFailingURLKey=http://dr282zn36sxxg.cloudfront.net/datastreams/f-
d%3Afc7f649e1e3ba58452f67e3fa1f66f69a15b96b3ea585c946e4fa854%2BEPUB%2BEPUB.1, 
NSLocalizedDescription=The request timed out., NSUnderlyingError=0x1e1975b0 "The request 
timed out."}

だから、誰でもこれを解決する方法を提案できますか。. .

4

3 に答える 3

1

と呼ばれる GCD 抽象化を使用してみてください[NSURLConnection sendAsynchronousRequest:queue:completionHandler::

:

NSURL *url = [NSURL URLWithString:@"your_URL"];
NSURLRequest *myUrlRequest = [NSURLRequest requestWithURL:url];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest: myUrlRequest queue: queue completionHandler: ^ (NSURLResponse *response, NSData *data, NSError *error)
{

    if ([data length] > 0 && error == nil)
        //doSomething With The data

    else if (error != nil && error.code == ERROR_CODE_TIMEOUT)
        //time out error

    else if (error != nil)
        //download error
}];

違いはありますか?

于 2013-09-02T10:41:08.527 に答える
0

エラー メッセージには、リクエストがタイムアウトしたことが明確に示されています。ネットワーク速度が向上する場所に移動するか、リクエストのタイムアウトを増やしてください。現在は 60 と表示されることがありますが、これでは画像のダウンロードには不十分な場合があります。

于 2013-09-02T10:12:04.047 に答える