2012-03-13 39 views
0

我有一个EMR应用程序,我希望我可以将我收集的数据像图像和语音发送到服务器。在数据库中,我该如何做到这一点。有没有办法通过post方法将这些数据发送到服务器。如何将数据从iphone应用程序上传到mysql数据库

+0

你可以使用ASIHTTPRequest – welsonla 2012-03-13 06:09:22

+0

你能告诉我样本或代码的帮助吗 – user1263350 2012-03-13 06:10:54

+0

也许这将帮助你http://stackoverflow.com/questions/7792949/ios-5-https-asihttprequest-stop-working – 2012-03-13 06:24:04

回答

1

这里是一个HTTP POST请求的例子

// define your form fields here: 
NSString *content = @"field1=42&field2=Hello"; 

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://www.example.com/form.php"]]; 
[urlRequest setHTTPMethod:@"POST"]; 
[urlRequest setHTTPBody:[content dataUsingEncoding:NSISOLatin1StringEncoding]]; 

// generates an autoreleased NSURLConnection 
[NSURLConnection connectionWithRequest:request delegate:self]; 

可能要参考http://developer.apple.com/library/ios/#documentation/cocoa/reference/foundation/Classes/NSURLConnection_Class/Reference/Reference.html

本教程也很有帮助http://www.raywenderlich.com/2965/how-to-write-an-ios-app-that-uses-a-web-service

0

在这种情况下,你可以做遵循两种方式: 1.如果您严格使用POST(我喜欢),您可以使用cocoahttpserver项目:

https://github.com/robbiehanson/CocoaHTTPServer

在iPhone应用程序,你可以做到这一点的代码来发送POST请求:

-(NSDictionary *) getJSONAnswerForFunctionVersionTwo:(NSString *)function 
            withJSONRequest:(NSMutableDictionary *)request; 
{ 
    [self updateUIwithMessage:@"server download is started" withObjectID:nil withLatestMessage:NO error:NO]; 
    NSDictionary *finalResultAlloc = [[NSMutableDictionary alloc] init]; 
    @autoreleasepool { 


     NSError *error = nil; 

     NSString *jsonStringForReturn = [request JSONStringWithOptions:JKSerializeOptionNone serializeUnsupportedClassesUsingBlock:nil error:&error]; 
     if (error) NSLog(@"CLIENT CONTROLLER: json decoding error:%@ in function:%@",[error localizedDescription],function); 
     NSData *bodyData = [jsonStringForReturn dataUsingEncoding:NSUTF8StringEncoding]; 
     NSData *dataForBody = [[[NSData alloc] initWithData:bodyData] autorelease]; 
     //NSLog(@"CLIENT CONTROLLER: string lenght is:%@ bytes",[NSNumber numberWithUnsignedInteger:[dataForBody length]]); 
     NSString *functionString = [NSString stringWithFormat:@"/%@",function]; 
     NSURL *urlForRequest = [NSURL URLWithString:functionString relativeToURL:mainServer]; 
     NSMutableURLRequest *requestToServer = [NSMutableURLRequest requestWithURL:urlForRequest]; 
     [requestToServer setHTTPMethod:@"POST"]; 
     [requestToServer setHTTPBody:dataForBody]; 
     [requestToServer setTimeoutInterval:600]; 
     [NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[urlForRequest host]]; 

     NSData *receivedResult = [NSURLConnection sendSynchronousRequest:requestToServer returningResponse:nil error:&error]; 

     if (error) { 
      NSLog(@"CLIENT CONTROLLER: getJSON answer error download:%@",[error localizedDescription]); 
      [self updateUIwithMessage:[error localizedDescription] withObjectID:nil withLatestMessage:YES error:NO]; 
      [finalResultAlloc release]; 
      return nil; 
     } 
     NSString *answer = [[NSString alloc] initWithData:receivedResult encoding:NSUTF8StringEncoding]; 
     JSONDecoder *jkitDecoder = [JSONDecoder decoder]; 
     NSDictionary *finalResult = [jkitDecoder objectWithUTF8String:(const unsigned char *)[answer UTF8String] length:[answer length] error:&error]; 
     [finalResultAlloc setValuesForKeysWithDictionary:finalResult]; 

     [answer release]; 
     [self updateUIwithMessage:@"server download is finished" withObjectID:nil withLatestMessage:NO error:NO]; 

     if (error) NSLog(@"CLIENT CONTROLLER: getJSON answer failed to decode answer with error:%@",[error localizedDescription]); 
    } 
    NSDictionary *finalResultToReturn = [NSDictionary dictionaryWithDictionary:finalResultAlloc]; 
    [finalResultAlloc release]; 

    return finalResultToReturn; 

} 

千万别忘了带上与图像为base64属性。

最后,如果你不喜欢保存数据,你发送给你的Mac应用程序,你可以使用任何数据库C api发送到你的数据库。我建议使用核心数据来保存接收数据。

相关问题