0

我有一个使用 NSURLconnection 的应用程序,当使用 3G 并出现错误“网络连接丢失”时,它似乎始终无法与 Web 服务通信。但是,该应用程序在 wifi 下运行得很好。

关于可能是什么问题的任何想法?我需要对 NSURLconnection 做任何特别的事情来处理 3G 吗?

我使用的一个 NSURL 代码示例。

        NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:req delegate:self];
    if (conn) {
        XMLData = [NSMutableData data];     
    }

委托方法

 -(void) connection:(NSURLConnection *) connection 
 didReceiveResponse:(NSURLResponse *) response {
    [XMLData setLength: 0];
 }

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

-(void) connection:(NSURLConnection *) connection 
   didFailWithError:(NSError *) error {
     self.errorLabel.text = [error localizedDescription];

 }

 -(void) connectionDidFinishLoading:(NSURLConnection *) connection {
     NSLog(@"DONE. Received Bytes: %d", [XMLData length]);
     NSString *theXML = [[NSString alloc] 
                    initWithBytes: [XMLData mutableBytes] 
                    length:[XMLData length] 
                    encoding:NSUTF8StringEncoding];

   //i do some xml parsing on the data returned
   }
4

1 回答 1

1

我会开始将 NSLog 放在委托方法中。从 didReceiveData 开始。

 -(void) connection:(NSURLConnection *) connection didReceiveData:(NSData *) receiveddata          { 
            if (receiveddata != nil){ 
            [XMLData appendData:receiveddata];
             NSLog(@"didReceiveData :receiveddata is:%@", receiveddata);

            }
    else{ 
        NSLog(@"NO Data:%@");
        }

    }
于 2012-06-01T19:28:00.303 回答