同步一般用于一次性操作,如判断当前网络是否可用等等。多的就不再一一介绍,在实现上面有两点不同:
(1)在用NSURLConnect的时候一个调用同步函数一个调用了异步函数。
(2)异步的需要实现delegate的相关回调函数。
以下是参考代码:
同步方式:
-(void)UpadaPost:(NSString*)strcontextURL:(NSString*)urlstr{
NSLog(urlstr);
NSLog(strcontext);
assert(strcontext!=NULL);
assert(urlstr!=NULL);
NSData*postData=[strcontextdataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString*postLength=[NSStringstringWithFormat:@"%d",[postDatalength]];
NSMutableURLRequest*request=[[[NSMutableURLRequestalloc]init]autorelease];
[requestsetURL:[NSURLURLWithString:urlstr]];
[requestsetHTTPMethod:@"POST"];[request setTimeoutInterval: 20];//setting timeout
[requestsetValue:postLengthforHTTPHeaderField:@"Content-Length"];
[requestsetValue:@"application/x-www-form-urlencoded"forHTTPHeaderField:@"Content-Type"];
[requestsetHTTPBody:postData];
NSURLResponse*respone;
NSError*error;
NSData*myReturn=[NSURLConnection sendSynchronousRequest:requestreturningResponse:&respone
error:error];
NSLog(@"%@",[[NSStringalloc]initWithData:myReturnencoding:NSUTF8StringEncoding]);
}
异步方式:
-(void)UpadaPost:(NSString*)strcontextURL:(NSString*)urlstr{
NSLog(urlstr);
NSLog(strcontext);
assert(strcontext!=NULL);
assert(urlstr!=NULL);
NSData*postData=[strcontextdataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString*postLength=[NSStringstringWithFormat:@"%d",[postDatalength]];
NSMutableURLRequest*request=[[[NSMutableURLRequestalloc]init]autorelease];
[requestsetURL:[NSURLURLWithString:urlstr]];
[requestsetHTTPMethod:@"POST"]; [request setTimeoutInterval: 20];//setting timeout
[requestsetValue:postLengthforHTTPHeaderField:@"Content-Length"];
[requestsetValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[requestsetHTTPBody:postData];
NSURLConnection*conn=[[NSURLConnectionalloc]initWithRequest:request delegate:self];
if(conn)
{
NSLog(@"Connectionsuccess");
[UIApplicationsharedApplication].networkActivityIndicatorVisible=YES;
[connretain];
}
else
{
//informtheuserthatthedownloadcouldnotbemade
}
}
#pargmamark------------------以下为相应的回调函数-------------------------------
//收到响应时,会触发
-(void)connection:(NSURLConnection*)connection didReceiveResponse:(NSURLResponse*)response{
//注意这里将NSURLResponse对象转换成NSHTTPURLResponse对象才能去
NSHTTPURLResponse*httpResponse=(NSHTTPURLResponse*)response;
if([responserespondsToSelector:@selector(allHeaderFields)]){
NSDictionary*dictionary=[httpResponseallHeaderFields];
NSLog([dictionarydescription]);
NSLog(@"%d",[responsestatusCode]);
}
}
//链接错误
-(void)connection:(NSURLConnection*)connectiondidFailWithError:(NSError*)error{
//[selfperformSelectorOnMainThread:@selector(httpConnectEnd)withObject:nil waitUntilDone:NO];
NSLog(@"%@",[errorlocalizedDescription]);
}
//Calledwhenachunkofdatahasbeendownloaded.
//接收数据每收到一次数据,会调用一次
-(void)connection:(NSURLConnection*)connectiondidReceiveData:(NSData*)data{
//Processthedownloadedchunkofdata.
NSLog(@"%d",[datalength]);
//NSLog(@"%@",[[NSStringalloc]initWithData:dataencoding:NSUTF8StringEncoding]);
//[selfperformSelectorOnMainThread:@selector(updateProgress)withObject:nil waitUntilDone:NO];
}
//接收结束
-(void)connectionDidFinishLoading:(NSURLConnection*)connection{
NSLog(@"%@",connection);
//NSLog(@"%lld",received_);
//[selfperformSelectorOnMainThread:@selector(httpConnectEnd)withObject:nil waitUntilDone:NO];
//Settheconditionwhichendstherunloop.
}