如何在 iPhone 上接收来自 URL 的数据? [英] How can I receive data from URL on iPhone?

查看:29
本文介绍了如何在 iPhone 上接收来自 URL 的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Apple 文档中的代码进行一些 HTTP 通信.我可以成功连接到 URL,但是我无法从我的服务器接收数据.

I'm using the code from Apple's Document to do some HTTP communication. I can connect to the URL successfully, but I failed to receive the data from my server.

// create the request
NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://..."]
                        cachePolicy:NSURLRequestUseProtocolCachePolicy
                        timeoutInterval:60.0];
// create the connection with the request
// and start loading the data
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (theConnection) {
    // Create the NSMutableData that will hold
    // the received data
    // receivedData is declared as a method instance elsewhere
    NSMutableData *receivedData=[[NSMutableData data] retain];
} else {
    // inform the user that the download could not be made
}

原因可能是:

  1. 我在 Action 本身中声明了 receivedData.注释说我应该在别处声明它.我应该在哪里申报?我应该将其声明为控制器的财产吗?

  1. I declare receivedData in the Action itself. The annotation says that I should declare it elsewhere. Where should I declare it? Should I declare it as controller's property?

[[NSMutableData data] retain] 如何在 if{} 之外找到 URL?

How can [[NSMutableData data] retain] find the URL as it is outside of the if{}?

推荐答案

当你使用 NSURLConnection 的 initWithRequest:delegate: 方法时,数据(连同其他东西)在一系列方法调用中被发送到委托对象.这些方法都是可选的,所以如果你的委托对象没有实现它们,连接对象就会跳过它们.

When you use NSURLConnection's initWithRequest:delegate: method, the data (along with other stuff) is sent to the delegate object in a sequence of method calls. The methods are all optional, so if your delegate object doesn't implement them, the connection object just skips them.

方法有很多,这里就不一一列举了,但是在 NSURLConnection 文档中都有详细的描述.要获取接收到的数据,您需要在委托对象上实现 -connection:didReceiveData:.这个方法会被调用,可能不止一次,一个 NSData 对象代表新接收到的数据.然后,您可以将其附加到您现有的 NSMutableData 对象,或者用它做任何其他有意义的事情.当在委托对象上调用 -connectionDidFinishLoading: 时,您将知道您已收到所有数据.

There are a bunch of methods, so I won't list them all here, but they're all described in detail in the NSURLConnection documentation. To get the received data you'll want to implement -connection:didReceiveData: on the delegate object. This method will be called, potentially more than once, with an NSData object representing newly-received data. You can then append that to your existing NSMutableData object, or do whatever else makes sense with it. You'll know that you've received all of the data when -connectionDidFinishLoading: is called on the delegate object.

回答您的两个具体问题:

To answer your two specific questions:

  1. 是的,您应该将其声明为控制器对象的属性.您还应该确保在调用 NSURLConnection 的 initWithRequest:delegate: 之前分配对象,因为一旦创建连接对象,连接就会开始异步加载数据.或者,您可以在委托上实现 -connection:didReceiveResponse:,检查 HTTP 状态,然后创建数据对象.

  1. Yes, you should declare it as a property of the controller object. You should also make sure to allocate the object BEFORE calling NSURLConnection's initWithRequest:delegate:, because the connection will start loading data asynchronously as soon as the connection object is created. Alternately you could implement -connection:didReceiveResponse: on the delegate, check the HTTP status, and create the data object then.

可变数据对象无法找到您设置的 URL、连接或其数据,但如果您按照我描述的步骤操作,则可以将数据添加到其中它进来,并在连接完成时使用它.

The mutable data object can't find the URL, the connection, or its data as you've got it set up, but if you follow the steps I've described then you can add data to it as it comes in, and use it when the connection finishes.

这篇关于如何在 iPhone 上接收来自 URL 的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆