先看同步,默认服务器接受json数据,返回json数据
将下面的代码加入到某个类中,然后调用httpPost方法就可以了,这个是异步的
//同步post
-(NSString *)postSyn:(NSString *)urlStr jsonData:(NSString *)jsonData{
NSLog(@"post_begin");
NSData* postData = [jsonData dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];//数据转码;
NSString *length = [NSString stringWithFormat:@"%d", [postData length]];
NSMutableURLRequest* request = [[NSMutableURLRequest alloc]init];
[request setURL:[NSURL URLWithString:urlStr]]; //设置地址
[request setHTTPMethod:@"POST"]; //设置发送方式
[request setTimeoutInterval: 20]; //设置连接超时
[request setValue:length forHTTPHeaderField:@"Content-Length"]; //设置数据长度
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; //设置发送数据的格式
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; //设置预期接收数据的格式
[request setHTTPBody:postData]; //设置编码后的数据
//发起连接,接受响应
NSHTTPURLResponse* urlResponse = nil;
NSError *error = [[NSError alloc] init] ;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request
returningResponse:&urlResponse
error:&error];
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]; //返回数据,转码
NSLog(responseString);
NSLog(@"post_end");
return responseString;
}
将下面的代码加入到某个类中,然后调用httpPost方法就可以了,这个是异步的
-(void)httpPost:(NSString*)strcontext URL:(NSString*)urlstr{
strcontext = nil;
NSLog(@"url--%@",urlstr);
NSLog(@"param--%@",strcontext);
assert(strcontext!=NULL);
assert(urlstr!=NULL);
NSData* postData = [strcontext dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];//转码
NSString* postLength = [NSString stringWithFormat:@"%d",[postData length]];
NSMutableURLRequest* request = [[[NSMutableURLRequest alloc]init]autorelease];
[request setURL:[NSURL URLWithString:urlstr]]; //设置地址
[request setHTTPMethod:@"POST"]; //设置发送方式
[request setTimeoutInterval: 20]; //设置连接超时
[request setValue:postLength forHTTPHeaderField:@"Content-Length"]; //设置数据长度
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; //设置发送数据的格式
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; //设置预期接收数据的格式
[request setHTTPBody:postData]; //设置编码后的数据
NSURLConnection*conn=[[NSURLConnection alloc]initWithRequest:request delegate:self]; //设置类代理,注意要是self哦
if(conn)
{
NSLog(@"ConnectionSuccess");
[UIApplication sharedApplication].networkActivityIndicatorVisible=YES;
[conn retain];
}
else
{
NSLog(@"ConnectionFailed");
//informtheuserthatthedownloadcouldnotbemade
}
}
#pragma mark------------------以下为相应的回调函数-------------------------------
//收到响应时,会触发
-(void)connection:(NSURLConnection*)connection didReceiveResponse:(NSURLResponse*)response{
//注意这里将NSURLResponse对象转换成NSHTTPURLResponse对象才能去
NSHTTPURLResponse*httpResponse=(NSHTTPURLResponse*)response;
if([response respondsToSelector:@selector(allHeaderFields)]){
NSDictionary*dictionary=[httpResponse allHeaderFields];
NSLog(@"didReceiveResponse1:%@",[dictionary description]);
NSLog(@"didReceiveResponse2:%d",[response statusCode]);
}
}
//链接错误
-(void)connection:(NSURLConnection*)connection didFailWithError:(NSError*)error{
//[selfperformSelectorOnMainThread:@selector(httpConnectEnd)withObject:nil waitUntilDone:NO];
NSLog(@"didFailWithError:%@",[error localizedDescription]);
}
//Calledwhenachunkofdatahasbeendownloaded.
//接收数据每收到一次数据,会调用一次
-(void)connection:(NSURLConnection*)connection didReceiveData:(NSData*)data{
//Processthedownloadedchunkofdata.
NSLog(@"didReceiveData_length:%d",[data length]);
// NSLog(@"didReceiveData_data:%d",[data description]);
[[self responseData] appendData:data];
NSLog(@"didReceiveData_data%@",[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]);
//[selfperformSelectorOnMainThread:@selector(updateProgress)withObject:nil waitUntilDone:NO];
}
//接收结束
-(void)connectionDidFinishLoading:(NSURLConnection*)connection{
NSLog(@"connectionDidFinishLoading:%@",connection);
//NSLog(@"%lld",received_);
//[selfperformSelectorOnMainThread:@selector(httpConnectEnd)withObject:nil waitUntilDone:NO];
//Settheconditionwhichendstherunloop.
NSString *responseString = [[NSString alloc] initWithData:[self responseData] encoding:NSUTF8StringEncoding];
[responseData release];
NSLog(@"responseString:%@",responseString);
}