2013-04-04 53 views
0

我正在製作需要與Sendy API進行通信的iPhone應用程序。我認爲,它使用某種JSON,但我不太確定,也不知道從哪裏開始。我對API的subscribe部分特別感興趣。 基本上,我需要知道如何從我的應用程序與Sendy API交談。iOS將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:@"[email protected]" 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); 
} 

當日志發生,字符串爲空。我通過斷點知道最後一個方法被調用。

回答

2

看看這些API都是純文本響應。

由於這是一個POST,您可以使用NSURLConnection來組合請求。有關格式化響應的信息,請參閱this question

另一種方法是使用類似AFNetworkingRestKit的東西,如果您在使用API​​進行更多工作時可能會更友好些。

+0

我仍然很難與它...我已經在問題中張貼我的代碼。 – Undo 2013-04-04 21:28:37

+0

如果我向代碼中的URL發送POST請求,它會顯示爲空白。你確定服務設置正確嗎? – 2013-04-04 21:40:11

+0

我真的不知道...今天晚些時候我會做一些研究。 – Undo 2013-04-04 21:52:35

1

我猜你已經解決了這個問題,但是如果有其他人被困在這裏(就像我那樣),我想我會發布我所做的工作。

您需要做的第一件事是創建一個名爲NSString + URLEncoding(或其他)的類別,它將從[email protected]接收您的電子郵件和名稱字段並將其轉換爲blah%40blah.com。我修改這從便利博客帖子發現here

@interface NSString (URLEncoding) 
-(NSString *)urlEncodeUsingEncoding:(NSStringEncoding)encoding; 
@end 

#import "NSString+URLEncoding.h" 

@implementation NSString (URLEncoding) 
-(NSString *)urlEncodeUsingEncoding:(NSStringEncoding)encoding { 
return (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(NULL, 
                  (CFStringRef)self, 
                  NULL, 
                  (CFStringRef)@"!*'\"();:@&=+$,/?%#[]% ", 
                      CFStringConvertNSStringEncodingToEncoding(encoding)));} 
@end 

好了,所以現在只需要導入的NSString + URLEncoding.h並添加以下代碼,你會在企業。這post幫我這部分

- (IBAction)submitButtonPressed:(id)sender 
{ 
NSMutableURLRequest *newRequest = [[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:@"http://stashdapp.com/sendy/subscribe"]]; 
[newRequest setHTTPMethod:@"POST"]; 
[newRequest setValue:@"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"]; 

NSString *email = @"[email protected]"; 
NSString *name = @"First Lastname"; 
NSString *list = @"XXXXXXXXXXXXXXXXXX"; 

NSString *postData = [NSString stringWithFormat:@"email=%@&boolean=true&name=%@&list=%@", [email urlEncodeUsingEncoding:NSUTF8StringEncoding],[name urlEncodeUsingEncoding:NSUTF8StringEncoding],list]; 

[newRequest setHTTPBody:[postData dataUsingEncoding:NSUTF8StringEncoding]]; 

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

您還包括你在你的問題引述委託方法。

希望它可以幫助別人!

相關問題