2011-09-04 138 views
0

我已经找到了关于设置HTTP POST请求到我的服务器的很多关于stackoverflow的好建议。我遵循了很多例子。我在项目中设置了一个快速测试,试图发送POST请求并从服务器检索一些数据。下面是从ViewController.h我的代码我已经安装:iOS:为什么从HTTP POST的responseData不显示

#import <UIKit/UIKit.h> 

@interface ViewController : UIViewController { 
IBOutlet UILabel *label; 
NSString *moviePost; 
NSMutableData *httpResponse; 
    NSURLConnection *connection; 

} 

@property (nonatomic, retain) NSString *moviePost; 
@property (nonatomic, retain) NSURLConnection *connection; 

@end 

和代码从我ViewController.m

#import "ViewController.h" 
#import <YAJLiOS/YAJL.h> 

@implementation ViewController 

@synthesize moviePost; 
@synthesize connection; 

- (void) viewDidLoad { 
[super viewDidLoad]; 

    NSArray *fields = [[NSArray alloc] initWithObjects:@"rating",@"genre",@"plotoutline",@"runtime",@"director",@"writer",@"mpaa",@"year",@"studio", nil]; 

NSDictionary *params = [[NSDictionary alloc] initWithObjectsAndKeys: 
         fields,@"fields", 
         nil]; 
[fields release]; 
NSDictionary *tempPost = [[NSDictionary alloc] initWithObjectsAndKeys: 
          @"2.0", @"jsonrpc", 
          @"VideoLibrary.GetMovies", @"method", 
          @"1", @"id", 
          params, @"params", 
          nil]; 
[params release]; 
moviePost = [tempPost yajl_JSONString]; 
[tempPost release]; 


NSLog(@"Going to post: %@\n\n", moviePost); 

NSString *url = [NSString stringWithFormat:@"http://192.168.1.133:8080/jsonrpc"]; 

NSMutableString* requestURL = [[NSMutableString alloc] init]; 
    [requestURL appendString:url]; 

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: [NSString stringWithString:requestURL]]]; 

NSData *requestData = [NSData dataWithBytes: [moviePost UTF8String] length: [moviePost length]]; 

    [request setHTTPMethod: @"POST"]; 
    [request setValue:@"text/plain" forHTTPHeaderField:@"content-type"]; 
    [request setHTTPBody: requestData]; 

    connection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 
    [request release]; 

} 

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

// Called when data has been received 
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { 
[httpResponse appendData:data]; 
} 

- (void)connectionDidFinishLoading:(NSURLConnection *)connection { 
    NSString* responseString = [[NSString alloc] initWithData:httpResponse encoding:NSUTF8StringEncoding]; 

    // Do something with the response 
    NSLog(@"Response: %@", responseString); 

    [responseString release]; 
    [connection release]; 
    [httpResponse release]; 
} 

- (void)dealloc { 
    [moviePost release]; 
    [super dealloc]; 
} 

大部分代码来自我发现的例子。它可以发送POST请求到服务器,并且服务器确实响应数据。我通过在测试设备和服务器之间进行数据包捕获来证明这一点。正确的请求正在发送并且正在发回正确的响应。我在运行应用程序时没有遇到任何问题。我已经运行了分析器,我没有遇到任何问题。

我遇到的问题是我无法获得在NSLog中打印的响应。当运行 - (void)connectionDidFinishLoading:(NSURLConnection *)connection时,它在NSLog中显示出一行Response:,但这就是我所得到的。

有什么我失踪?我是新来的Objective-C和Xcode。任何帮助表示赞赏。

+3

需要初始化'httpResponse'。否则,当调用NSURLConnection委托方法时,'httpResponse'为'nil'。 – albertamg

回答

4

albertamg将它钉在评论中。

需要allocinitNSMutableData *httpResponse,除非你已经做了别的,而不是某个地方,这在上面的代码所示。

你应该在你viewDidLoad方法添加此

httpResponse = [[NSMutableData alloc] init]; 
+0

感谢您的快速响应和帮助。我不能相信我忽略了这个基本的东西,但我做到了。那是其中的一个。 – John