我有一个使用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
}
答案 0 :(得分: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:%@");
}
}