我正在制作一个需要与Sendy API通信的 iPhone 应用程序。我相信它使用某种 JSON,但我不太确定,也不知道从哪里开始。我对subscribe
API 的部分特别感兴趣。基本上,我需要知道如何从我的应用程序中与 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);
}
当日志发生时,字符串为空。我通过断点知道调用了最后一个方法。