【问题标题】:How to make Twilio api Post request with the help of AFNetworking?如何在 AFNetworking 的帮助下发出 Twilio api Post 请求?
【发布时间】:2015-08-05 08:06:08
【问题描述】:

我想使用 AFNetworking 访问 Twilio api。我尝试了多种方法,但没有成功。如果有人使用 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.}

谢谢。

【问题讨论】:

    标签: ios objective-c afnetworking twilio


    【解决方案1】:

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

    希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 2012-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多