I have found a lot of good advice on stackoverflow about setting up a HTTP POST request to my server. I have followed a lot of examples. I have setup a quick test in a project to atempt to send a POST request and retrieve some data from the server. Here is my code from the ViewController.h I have setup:
#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
And code from my 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];
}
Most of the code comes from examples I have found. It does work to send a POST request to the server, and the server does respond with data. I have proven this by doing a packet capture between the test device and the server. The correct request is getting sent and the correct response is being sent back. I am not getting any issues when I run the app. I have run through the Analyzer and I do not get any issues.
The problem I have is that I am not able to get the response to print in the NSLog. When
- (void)connectionDidFinishLoading:(NSURLConnection *)connection runs it does show a line of Response: in the NSLog, but that is all I get.
Is there something I am missing? I am new to objective-c and xcode. Any help is appreciated.
albertamg nailed it in a comment.
you need to
allocandinityourNSMutableData *httpResponseunless you’ve done that somewhere else and not shown it in the above code.You should add this in your
viewDidLoadmethod