ios - 如何在 AFNetworking 的帮助下发出 Twilio api Post 请求?

标签 ios objective-c afnetworking twilio

我要访问 Twilio api 使用 AF网络 .我尝试了多种方法,但没有成功。如果有人使用 AFNetworking 发布请求,请帮助我。

案例 1:这是我的原生 Objective-C 工作代码。

NSString *urlString = [NSString stringWithFormat:@"https://%@:%@@api.twilio.com/2010-04-01/Accounts/%@/SMS/Messages", kTwilioSID, kTwilioSecret, kTwilioSID];

NSURL *url                      = [NSURL URLWithString:urlString];
NSMutableURLRequest *request    = [[NSMutableURLRequest alloc] init];

[request setURL:url];
[request setHTTPMethod:@"POST"];

NSString *bodyString    = [NSString stringWithFormat:@"From=%@&To=%@&Body=%@", from, to, message];

NSData *data            = [bodyString dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:data];

[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {

    if (connectionError)
    {
        DLog(@"Error: %@", connectionError);

        completionBlock(connectionError, NO);
    }
    else
    {
        completionBlock(connectionError, YES);
    }
}];

案例 2:使用 AFNetorking:不工作的代码:

代码:
    NSString *urlString = [NSString stringWithFormat:@"https://%@:%@@api.twilio.com/2010-04-01/Accounts/%@/SMS/Messages", kTwilioSID, kTwilioSecret, kTwilioSID];

NSDictionary *dict = @{
                       @"From" : from,
                       @"To" : to,
                       @"Body" : message
                       };


AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFJSONResponseSerializer serializer];

[manager POST:urlString parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject)
 {
     NSLog(@"Success: %@", responseObject);
 }
      failure:
 ^(AFHTTPRequestOperation *operation, NSError *error) {
     NSLog(@"Error: %@", error);
 }];

相关错误:
Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0x1775e660 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set., NSUnderlyingError=0x175101b0 "Request failed: bad request (400)"}

案例 3:使用 AFNetorking:另一个也不起作用的代码:

代码:
 NSString *urlString = [NSString stringWithFormat:@"https://%@:%@@api.twilio.com/2010-04-01/Accounts/%@/SMS/Messages", kTwilioSID, kTwilioSecret, kTwilioSID];
NSURL *url                      = [NSURL URLWithString:urlString];
NSMutableURLRequest *request    = [[NSMutableURLRequest alloc] init];
[request setURL:url];
[request setHTTPMethod:@"POST"];

NSString *bodyString    = [NSString stringWithFormat:@"From=%@&To=%@&Body=%@", from, to, message];
NSData *data            = [bodyString dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:data];

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.responseSerializer = [AFJSONResponseSerializer serializer];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"%@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"error: %@", error);
}];

[operation start];

相关错误:
 Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0x177a3e70 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}

谢谢你。

最佳答案

来自 Twilio 的 Ricky 在这里。首先是一个快速的免责声明。直接从 iOS 应用程序向 Twilio 发出请求需要您在应用程序中嵌入您的 Twilio 帐户凭据,这很危险。我的建议是从您的应用发出请求的服务器端脚本发送 SMS,以确保您的凭据安全。

话虽这么说,你的代码真的很接近。默认情况下,Twilio's REST API returns XML .如果您想解析默认返回的响应,您可以更新版本 2 中的代码以使用 AFXMLParserResponseSerializer :

operation.responseSerializer = [AFXMLParserResponseSerializer serializer];

如果您更愿意使用 JSON,则更新您发出 POST 请求的 Twilio URL 并表明您想要 JSON 响应:
NSString *urlString = [NSString stringWithFormat:@"https://%@:%@@api.twilio.com/2010-04-01/Accounts/%@/SMS/Messages.json", kTwilioSID, kTwilioSecret, kTwilioSID];

希望有帮助。

关于ios - 如何在 AFNetworking 的帮助下发出 Twilio api Post 请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31826787/

相关文章:

ios - Swift后台的PushListener

iphone app允许背景音乐继续播放

objective-c - 是否可以触发从 html 页面到 iphone 应用程序的功能?

iphone - Xcode 配置文件问题

objective-c - iOS中UITableView异步图片加载的建议

ios - xib/uibuttons 添加为 ViewController subview ?

ios - 为什么设备旋转时 self.view.frame 和 self.view.bounds 不同?

ios - 自定义 UIViews 奇怪的行为

ios - 我可以构建自己的网络框架(依赖于 AFNetworking)作为 watchOS2 框架吗?

ios - 如何使用AFHTTPSessionManager记录每个请求/响应?