2011-12-15 45 views
21

Tôi muốn biết làm cách nào để nhận giá trị trả về 1 hoặc 0 chỉ .... trở lại từ yêu cầu URL không đồng bộ.cách gửi Yêu cầu URL Không đồng bộ?

hiện tôi làm điều đó theo cách này:

 NSString *UTCString = [NSString stringWithFormat:@"http://web.blah.net/question/CheckQuestions?utc=%0.f",[lastUTCDate timeIntervalSince1970]]; 
     NSLog(@"UTC String %@",UTCString); 
     NSURL *updateDataURL = [NSURL URLWithString:UTCString]; 
     NSString *checkValue = [NSString stringWithContentsOfURL:updateDataURL encoding:NSASCIIStringEncoding error:Nil]; 
     NSLog(@"check Value %@",checkValue); 

làm việc này, tuy nhiên nó được chặn chủ đề chính của tôi cho đến khi tôi nhận được trả lời lại từ URL, làm thế nào để tôi đặt nó vì vậy nó sẽ làm điều đó trong một chủ đề khác thay vì chủ đề chính?

EDIT: ĐÁP tôi kết thúc upcalling chức năng của tôi với điều này, nó hoạt động tốt :)

[self performSelectorInBackground:@selector(shouldCheckForUpdate) withObject:nil]; 
+5

Tính đến iOS 5, bạn có thể sử dụng '+ (void) sendAsynchronousRequest: (NSURLRequest *) yêu cầu đợi: (NSOperationQueue *) đợi completionHandler: (void (^) (NSURLResponse *, NSData *, NSError *)) handler' thay vào đó, dễ dàng hơn và rõ ràng hơn cho các yêu cầu đơn giản hơn là tạo đối tượng đại biểu. Tôi sẽ cố gắng viết một câu trả lời cho thấy phương pháp này sau. –

+1

@MarkAmery Sẽ rất vui khi thấy câu trả lời của bạn. Tôi gặp khó khăn khi tìm một ví dụ đơn giản về 'sendAsynchronousRequest'. –

Trả lời

28

bạn có thể sử dụng NSURLConnection lớp

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]]; 
[[[NSURLConnection alloc] initWithRequest:request delegate:self] autorelease]; 

và xử lý phản ứng và các lỗi sử dụng phương pháp đại biểu của mình.

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
- (void)connectionDidFinishLoading:(NSURLConnection *)connection 

Bạn có thể tìm thấy thi hành NSURLConnection

Edit: Mặc dù NSURLConnection được cung cấp bởi táo là nhiều đề nghị cách đặt yêu cầu URL. Nhưng tôi thấy thư viện AFNetworking rất tiết kiệm thời gian, dễ triển khai và mạnh mẽ nhưng đơn giản như triển khai của bên thứ ba. Bạn nên cung cấp cho nó một thử.

+1

phương pháp nào tôi nên sử dụng để nhận các giá trị cookie ?? – kAnNaN

5

Sử dụng NSURLConnection và thực hiện yêu cầu của bạn. Sau đó, bạn có thể bắt đầu kết nối đồng bộ hoặc không đồng bộ với các phương pháp NSURLConnection của:

Đang tải dữ liệu đồng bộ

+ sendSynchronousRequest:returningResponse:error: 

Đang tải dữ liệu không đồng bộ

+ connectionWithRequest:delegate: 
– initWithRequest:delegate: 
– initWithRequest:delegate:startImmediately: 
– start 

Kiểm tra lớp NSURLConnection trong Apple phát triển API Reference.

+0

đây là liên kết: http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSURLConnection_Class/Reference/Reference.html – samfisher

26

thử điều này:

.h:

NSMutableData *responseData; 

.m:

- (void)load 
{ 
    NSURL *myURL = [NSURL URLWithString:@"http://www.example.com"]; 
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:myURL cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60]; 

    [[NSURLConnection alloc] initWithRequest:request delegate:self]; 
} 

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{ 
    responseData = [[NSMutableData alloc] init]; 
} 

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

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
{ 
    [responseData release]; 
    [connection release]; 
    [textView setString:@"Unable to fetch data"]; 
} 

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
    NSLog(@"Succeeded! Received %d bytes of data",[responseData 
                length]); 
    NSString *txt = [[[NSString alloc] initWithData:responseData encoding: NSASCIIStringEncoding] autorelease]; 
} 
+0

tôi chỉ cần một câu trả lời của 1 hoặc 0, là quá nhiều cho nó? – Desmond

+0

@Maulik Thêm phòng "Nhà phát triển thông minh". – MinuMaster

+0

@Maulik, bạn đã cứu ngày của tôi, cảm ơn! – DeZigny

2

Có một ví dụ trong tài liệu gọi là LazyTableImages iOS XCode. URL này không đồng bộ cũng như tải hình ảnh không đồng bộ thành UITableView ô được hiển thị trên màn hình sau khi dừng di chuyển. Ví dụ tuyệt vời về giao thức, xử lý dữ liệu không đồng bộ, v.v.

3

Sao chép không biết xấu hổ từ https://gist.github.com/knmshk/3027474. Tất cả tín dụng đều đến https://gist.github.com/knmshk.

xmlData = [[NSMutableData alloc] init]; 
NSURL *url = [NSURL URLWithString: 
@"http://forrums.bignerdranch.com/smartfeed.php?" 
@"limit=NO_LIMIT&count_limit20&sort_by=standard&" 
@"feed_type=RSS2.0&feed_style=COMPACT"]; 
NSURLRequest *request = [NSURLRequest requestWithURL:url]; 
//connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES]; 
NSOperationQueue *queue = [[NSOperationQueue alloc]init]; 
[NSURLConnection sendAsynchronousRequest:request 
            queue:queue 
         completionHandler:^(NSURLResponse *response, NSData *data, NSError *error){ 
          if (error) { 
           xmlData = nil; 
           NSLog(@"error:%@", error.localizedDescription); 
          } 
          [xmlData appendData:data]; 
         }]; 
Các vấn đề liên quan