user1263350
user1263350

Reputation: 83

How to upload data from iphone app to mysql data base

I have a EMR app and i want that i may send the data which i have collected like images and voice to server. in data base so how can i do this . Is there any way to send these data to server through post method.

Upvotes: 0

Views: 891

Answers (2)

user170317
user170317

Reputation: 1202

In that case, you can do follow two ways: 1. if you strictly like to using POST (i like), u can using cocoahttpserver project:

https://github.com/robbiehanson/CocoaHTTPServer

In iphone app, you can do this code to send POST request:

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

}

Don't forget to pack attributes with images to base64.

Finally, if u don't like to keep data, which u send in you mac app, u can send to u database using any database C api. I recommend to using core data to save receive data.

Upvotes: 0

Stanley Tang
Stanley Tang

Reputation: 256

Here is an example of a HTTP Post request

// 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];

Might want to reference http://developer.apple.com/library/ios/#documentation/cocoa/reference/foundation/Classes/NSURLConnection_Class/Reference/Reference.html

This tutorial is also helpful http://www.raywenderlich.com/2965/how-to-write-an-ios-app-that-uses-a-web-service

Upvotes: 1

Related Questions