0

CocoaTouchのサーバーからデータをアップロード/ダウンロードするにはどうすればよいですか。これが私がこれまでに持っているものです...

-(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];
    }
}

私はとても迷っています...

4

3 に答える 3

1

を過剰にリリースしたため、コードがクラッシュしますconnectionCocoaのメモリ管理規則を確認します。

それとは別に、どのような問題を抱えているかをより具体的にする必要があります。

ところで、この用語は「メソッド インスタンス」ではなく「インスタンス変数」です。インスタンス変数はインスタンス内の変数であり、メソッドとは関係ありません。

于 2009-09-02T10:20:13.233 に答える
0

これはここでカバーされています:

NSURLRequest - NSURLRequest POST ボディの URL をエンコードします (iPhone の目的-C)

受け入れられた回答は、私が使用したものと同様のASIHTTPRequestを使用し、HTML フォームからの投稿/取得を非常に簡単にします。これが例です(過去のstackoverflowから)

ASIFormDataRequest *request = [[[ASIFormDataRequest alloc] initWithURL:@"http://someSite.com"] autorelease];
[request setPostValue:@"myValue1" forKey:@"myFormField1"];
[request setPostValue:@"myValue2" forKey:@"myFormField2"];
// etc.
[request start];
NSError *error = [request error];
if (!error)
  NSString *response = [request responseString];
于 2009-07-30T03:46:45.460 に答える
0

また、ファイルが大きい場合は、追加するのではなく、を使用NSFilehandleして 内にデータを書き込むことをお勧めします。didReceiveData

于 2009-09-02T15:29:42.573 に答える