Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send audio using web service using AFNetworking

I have an audio that I need to send using a webservice along with other inputs. After doing some search I understood that I cannot send nsdata using XML SOAP message so I downloaded AFHTTPRequest Classes and followed examples on the internet, but it's not working.

This is the web service I am using:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <addlocation2 xmlns="http://tempuri.org/">
      <userid>int</userid>
      <name>string</name>
      <voicemsg>base64Binary</voicemsg>
    </addlocation2>
  </soap:Body>
</soap:Envelope>

This is the code I am using to send the data and other parameters:

   NSString *filePath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"m4a"];

NSData *audio = [NSData dataWithContentsOfFile:filePath];

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

NSString *URLString = @"http://host.com/Websr/Service.asmx?op=addlocation2";//[WebService getAudioURL];
NSDictionary *parameters = @{@"userid": @2,
                             @"name": @"usertest",
                             };

manager.requestSerializer = [AFHTTPRequestSerializer serializer];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];

manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html", @"audio/m4a", nil];
[manager POST:URLString parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {

    if (audio) {
        [formData appendPartWithFileData:audio name:@"voicemsg" fileName:[NSString stringWithFormat:@"test.m4a"] mimeType:@"audio/m4a"];
    }
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"Success %@", responseObject);
    NSLog(@"operation %@", operation.responseString);

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Failure" message:@"Sending Failure" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    [alert show];
    NSLog(@"Failure %@, %@", error, operation.responseString);
}];

[self dismissViewControllerAnimated:NO completion:nil];

The app is crashing with excess bad access on when it reaches the if statement containing data in class AFURLResponseSerialization.h:

- (BOOL)validateResponse:(NSHTTPURLResponse *)response
                data:(NSData *)data
               error:(NSError *)error
{
BOOL responseIsValid = YES;
NSError *validationError = nil;

if (response && [response isKindOfClass:[NSHTTPURLResponse class]]) {
    if (self.acceptableContentTypes && ![self.acceptableContentTypes containsObject:[response MIMEType]]) {
        NSLog(@"[response URL] %@", [response URL]);
         if ([data length] > 0 && [response URL]) {
                ...}
like image 708
coder Avatar asked Jul 13 '15 12:07

coder


1 Answers

You don't need to use AFHTTPRequest Classes. Your requirement is just to send base64Binary data over the network, which your web service says. Follow the below link, It will be helpful. http://iosdevelopertips.com/core-services/encode-decode-using-base64.html

like image 177
Radhakrishna Sharma Gorenta Avatar answered Oct 04 '22 18:10

Radhakrishna Sharma Gorenta