2012-03-13 134 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發送到你的數據庫。我建議使用核心數據來保存接收數據。

相關問題