gpt4 book ai didi

iOS 将 Sendy API 集成到 App 中

转载 作者:行者123 更新时间:2023-11-28 20:21:17 24 4
gpt4 key购买 nike

我正在制作一个需要与 Sendy API 通信的 iPhone 应用程序.我相信它使用某种 JSON,但我不太确定,也不知道从哪里开始。我对 API 的 subscribe 部分特别感兴趣。 基本上,我需要知道如何从我的应用程序与 Sendy API 对话。

感谢任何帮助。

我的代码:

- (IBAction)submitButtonPressed:(id)sender
{
self.data = [[NSMutableData alloc] initWithLength:0];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://www.erwaysoftware.com/sendy/subscribe"]];
[request setHTTPMethod:@"POST"];
[request setValue:@"john@test.com" forHTTPHeaderField:@"email"];
[request setValue:@"john" forHTTPHeaderField:@"name"];
[request setValue:@"LxxxxxxxxxxxxxxxxxxxxQ" forHTTPHeaderField:@"list"];
[request setValue:@"true" forHTTPHeaderField:@"boolean"];

NSURLConnection *conn = [NSURLConnection connectionWithRequest:request delegate:self];
[conn start];

}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[self.data setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)d {
[self.data appendData:d];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
[[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Error", @"")
message:[error localizedDescription]
delegate:nil
cancelButtonTitle:NSLocalizedString(@"OK", @"")
otherButtonTitles:nil] show];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSString *responseText = [[NSString alloc] initWithData:self.data encoding:NSUTF8StringEncoding];

// Do anything you want with it

NSLog(@"%@", responseText);
}

发生日志时,字符串为空。我通过断点知道调用了最后一个方法。

最佳答案

查看 API,它只是纯文本响应。

因为它是一个 POST,您可以使用 NSURLConnection 来编写请求。参见 this question有关格式化响应的信息。

另一种方法是使用类似 AFNetworking 的东西或 RestKit如果您使用 API 进行更多工作,这可能会更友好一些。

关于iOS 将 Sendy API 集成到 App 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15821452/

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