gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 16:30:35 25 4
gpt4 key购买 nike

我要访问 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com