使用Cocoa Touch从服务器上传和下载数据? [英] Upload and download data from server with Cocoa Touch?

查看:117
本文介绍了使用Cocoa Touch从服务器上传和下载数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Cocoa Touch中从服务器上传/下载数据。这是我到目前为止...

How can I upload/download data from a server in Cocoa Touch. Here's what I have so far...

-(void)uploadSchedule:(id)sender
{
    NSData *content = [NSData dataWithContentsOfFile:self.dataFilePath];
    NSString *stuff = [[NSString alloc] initWithData:content encoding:NSASCIIStringEncoding];

    NSURL *url = [NSURL URLWithString:@"http://thetis.lunarmania.com"];
    NSMutableURLRequest* urlRequest = [[NSMutableURLRequest alloc]initWithURL:url];
    [urlRequest setHTTPMethod:@"POST"];
    [urlRequest setHTTPBody:[stuff dataUsingEncoding:NSASCIIStringEncoding]];

    NSLog(@"great success!");
}

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    // this method is called when the server has determined that it
    // has enough information to create the NSURLResponse

    // it can be called multiple times, for example in the case of a
    // redirect, so each time we reset the data.
    // receivedData is declared as a method instance elsewhere

    [receivedData setLength:0];
}

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    // append the new data to the receivedData
    // receivedData is declared as a method instance elsewhere

    [receivedData appendData:data];
}

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    // release the connection, and the data object
    [connection release];
    // receivedData is declared as a method instance elsewhere
    [receivedData release];

    // inform the user
    NSLog(@"Connection failed! Error - %@ %@",
          [error localizedDescription],
          [[error userInfo] objectForKey:NSErrorFailingURLStringKey]);
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    // do something with the data
    UIImage *image = [[UIImage alloc] initWithData:receivedData];
    [cat setImage:image];
    [image release];

    // receivedData is declared as a method instance elsewhere
    NSLog(@"Succeeded! Received %d bytes of data",[receivedData length]);

    // release the connection, and the data object
    [connection release];
    [receivedData release];
}

-(void)connection:(NSURLConnection *)connection
didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
    if ([challenge previousFailureCount] == 0) {
        NSURLCredential *newCredential;
        newCredential=[NSURLCredential credentialWithUser:@"ican@moeyo.org"
                                                 password:@"icanican"
                                              persistence:NSURLCredentialPersistenceNone];
        [[challenge sender] useCredential:newCredential
               forAuthenticationChallenge:challenge];
    } else {
        [[challenge sender] cancelAuthenticationChallenge:challenge];
        // inform the user that the user name and password
        // in the preferences are incorrect
        //[self showPreferencesCredentialsAreIncorrectPanel:self];
    }
}

>

推荐答案

代码崩溃,因为你过度释放连接。查看 Cocoa内存管理规则

The code crashes because you over-release connection. Review the Cocoa memory management rules.

除此之外,你必须更具体地了解你使用它的问题。

Aside from that, you'll have to be more specific about what problem you're having with it.

BTW,该术语是实例变量,而不是方法实例。实例变量是实例中的变量,与方法无关。

BTW, the term is "instance variable", not "method instance". An instance variable is a variable inside of an instance, and has nothing to do with methods.

这篇关于使用Cocoa Touch从服务器上传和下载数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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