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

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

问题描述

我使用Apple的Document的代码做一些HTTP通讯。我可以成功连接到该网址,但我未能从我的服务器接收数据。

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. 我声明 receivedData 在Action本身。注释说,我应该在其他地方声明。我应该在哪里声明它?

  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] 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.

有一堆方法,所以我赢了' t在这里列出他们,但他们都在NSURLConnection文档中详细描述。为了获得接收的数据,你需要在委托对象上实现-connection:didReceiveData:。此方法将被调用,可能不止一次,使用NSData对象表示新接收的数据。然后,您可以将它附加到您现有的NSMutableData对象,或做任何其他有意义的。

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,连接或其设置的数据,但如果您按照我描述的步骤,您可以添加数据,当它进来,并使用它,当连接完成。 / p>

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天全站免登陆