【问题标题】:Send audio using web service using AFNetworking使用 AFNetworking 使用 Web 服务发送音频
【发布时间】:2015-07-13 12:55:06
【问题描述】:

我有一个音频,我需要使用网络服务连同其他输入一起发送。做了一些搜索后,我了解到我无法使用 XML SOAP 消息发送 nsdata,因此我下载了 AFHTTPRequest 类并在互联网上遵循示例,但它不起作用。

这是我正在使用的网络服务:

<?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>

这是我用来发送数据和其他参数的代码:

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

当应用程序到达包含 AFURLResponseSerialization.h 类中的数据的 if 语句时,应用程序因过多的错误访问而崩溃:

- (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]) {
                ...}

【问题讨论】:

    标签: objective-c web-services afnetworking nsdata


    【解决方案1】:

    您不需要使用 AFHTTPRequest 类。您的要求只是通过网络发送 base64Binary 数据,正如您的 Web 服务所说。 按照下面的链接,这将是有帮助的。 http://iosdevelopertips.com/core-services/encode-decode-using-base64.html

    【讨论】:

    • 感谢您的提示,我正在寻找一种立即发送数据的方法,因为当我将其作为字符串类型发送时,它非常庞大并且在数据库中占用大量空间,但如果我将其作为数据发送,数据库会自动将其转换为字节并以智能方式保存,因此不会占用大量内存空间且易于检索。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-06
    相关资源
    最近更新 更多