1

I have client iOS app, which should send json request to server with audio bytes.

On server I have Hibernate (MySQL) with next model:

@Column(name = "voice_data", nullable = false, unique = false, columnDefinition="mediumblob")
@Lob
public byte[] getVoiceData() { return voiceData; }
public void setVoiceData(byte[] voiceData) { this.voiceData = voiceData; }

At iOS I want to get just recorded voice and save to DB:

    @property (strong, nonatomic) AVAudioRecorder       *audioRecorder;

    // some code ... 
    NSData *data = [[NSData alloc] initWithContentsOfFile:_audioRecorder.url.path];
    NSString* dataAsString = [NSString stringWithUTF8String:[data bytes]];

    NSDictionary *params = @{
                             MODEL_VOICE_VOICE_DATA : dataAsString,
                             // etc parameters
                             };

    NSString *url = [NSString stringWithFormat:@"%@", URL];        
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

    manager.requestSerializer = [AFJSONRequestSerializer serializer];

    [manager POST:url parameters:params
          success:^(AFHTTPRequestOperation *operation, id responseObject) {
              NSLog(@"%@", responseObject);
          } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
              // ...
          }
     ];
}
//

Then on server I have input request data:

[{"id":13,"voiceData":[],"date":....

So, looks like it's not correctly parsing from Audio (as it's empty). What I do wrong? And how I should send data to server?

levo4ka
  • 2,248
  • 1
  • 19
  • 32

1 Answers1

0

As @i_am_jorf mentioned the problem was with converting binary data into UTF-8 string. Solved with QSUtilities library:

  1. Download and link library to project (in iOS 8+ needs some updates with ARC)
  2. Convert data using encodeBase64WithData function

NSData *data = [[NSData alloc] initWithContentsOfFile:_audioRecorder.url.path]; NSString* dataAsString = [QSStrings encodeBase64WithData:data];

levo4ka
  • 2,248
  • 1
  • 19
  • 32