objective-c post json

先看同步,默認服務器接受json數據,返回json數據 

Java代碼  收藏代碼
  1. //同步post  
  2. -(NSString *)postSyn:(NSString *)urlStr jsonData:(NSString *)jsonData{  
  3.     NSLog(@"post_begin");  
  4.       
  5.     NSData* postData = [jsonData dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];//數據轉碼;  
  6.     NSString *length = [NSString stringWithFormat:@"%d", [postData length]];  
  7.       
  8.     NSMutableURLRequest* request = [[NSMutableURLRequest alloc]init];  
  9.     [request setURL:[NSURL URLWithString:urlStr]]; //設置地址  
  10.     [request setHTTPMethod:@"POST"]; //設置發送方式  
  11.     [request setTimeoutInterval: 20]; //設置連接超時  
  12.     [request setValue:length forHTTPHeaderField:@"Content-Length"]; //設置數據長度  
  13.     [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; //設置發送數據的格式  
  14.     [request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; //設置預期接收數據的格式  
  15.     [request setHTTPBody:postData]; //設置編碼後的數據  
  16.       
  17.     //發起連接,接受響應  
  18.     NSHTTPURLResponse* urlResponse = nil;  
  19.     NSError *error = [[NSError alloc] init] ;    
  20.     NSData *responseData = [NSURLConnection sendSynchronousRequest:request  
  21.                                                  returningResponse:&urlResponse   
  22.                                                              error:&error];    
  23.     NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]; //返回數據,轉碼  
  24.       
  25.     NSLog(responseString);  
  26.     NSLog(@"post_end");  
  27.     return responseString;  
  28. }  



將下面的代碼加入到某個類中,然後調用httpPost方法就可以了,這個是異步的 
Java代碼  收藏代碼
  1. -(void)httpPost:(NSString*)strcontext URL:(NSString*)urlstr{  
  2.     strcontext = nil;  
  3.     NSLog(@"url--%@",urlstr);  
  4.     NSLog(@"param--%@",strcontext);  
  5.     assert(strcontext!=NULL);  
  6.     assert(urlstr!=NULL);  
  7.     NSData* postData = [strcontext dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];//轉碼  
  8.     NSString* postLength = [NSString stringWithFormat:@"%d",[postData length]];  
  9.     NSMutableURLRequest* request = [[[NSMutableURLRequest alloc]init]autorelease];  
  10.     [request setURL:[NSURL URLWithString:urlstr]]; //設置地址  
  11.     [request setHTTPMethod:@"POST"]; //設置發送方式  
  12.     [request setTimeoutInterval: 20]; //設置連接超時  
  13.     [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; //設置數據長度  
  14.     [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; //設置發送數據的格式  
  15.     [request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; //設置預期接收數據的格式  
  16.     [request setHTTPBody:postData]; //設置編碼後的數據  
  17.     NSURLConnection*conn=[[NSURLConnection alloc]initWithRequest:request delegate:self];  //設置類代理,注意要是self哦  
  18.     if(conn)  
  19.     {  
  20.         NSLog(@"ConnectionSuccess");  
  21.         [UIApplication sharedApplication].networkActivityIndicatorVisible=YES;  
  22.         [conn retain];  
  23.     }  
  24.     else  
  25.     {  
  26.         NSLog(@"ConnectionFailed");  
  27.         //informtheuserthatthedownloadcouldnotbemade  
  28.     }  
  29. }  
  30.   
  31. #pragma mark------------------以下爲相應的回調函數-------------------------------  
  32. //收到響應時,會觸發  
  33. -(void)connection:(NSURLConnection*)connection didReceiveResponse:(NSURLResponse*)response{  
  34.     //注意這裏將NSURLResponse對象轉換成NSHTTPURLResponse對象才能去  
  35.     NSHTTPURLResponse*httpResponse=(NSHTTPURLResponse*)response;  
  36.     if([response respondsToSelector:@selector(allHeaderFields)]){  
  37.         NSDictionary*dictionary=[httpResponse allHeaderFields];  
  38.         NSLog(@"didReceiveResponse1:%@",[dictionary description]);  
  39.         NSLog(@"didReceiveResponse2:%d",[response statusCode]);  
  40.     }  
  41. }  
  42. //鏈接錯誤  
  43. -(void)connection:(NSURLConnection*)connection didFailWithError:(NSError*)error{  
  44.     //[selfperformSelectorOnMainThread:@selector(httpConnectEnd)withObject:nil waitUntilDone:NO];  
  45.     NSLog(@"didFailWithError:%@",[error localizedDescription]);  
  46. }  
  47. //Calledwhenachunkofdatahasbeendownloaded.  
  48. //接收數據每收到一次數據,會調用一次  
  49. -(void)connection:(NSURLConnection*)connection didReceiveData:(NSData*)data{  
  50.     //Processthedownloadedchunkofdata.  
  51.     NSLog(@"didReceiveData_length:%d",[data length]);  
  52.     // NSLog(@"didReceiveData_data:%d",[data description]);  
  53.     [[self responseData] appendData:data];  
  54.     NSLog(@"didReceiveData_data%@",[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]);  
  55.     //[selfperformSelectorOnMainThread:@selector(updateProgress)withObject:nil waitUntilDone:NO];  
  56. }  
  57. //接收結束  
  58. -(void)connectionDidFinishLoading:(NSURLConnection*)connection{  
  59.     NSLog(@"connectionDidFinishLoading:%@",connection);  
  60.     //NSLog(@"%lld",received_);  
  61.     //[selfperformSelectorOnMainThread:@selector(httpConnectEnd)withObject:nil waitUntilDone:NO];  
  62.     //Settheconditionwhichendstherunloop.  
  63.       
  64.       
  65.       
  66.       
  67.     NSString *responseString = [[NSString alloc] initWithData:[self responseData] encoding:NSUTF8StringEncoding];  
  68.     [responseData release];  
  69.       
  70.     NSLog(@"responseString:%@",responseString);  
  71. }  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章