HTTP_POST from iPhone SDK using JSON
I'm looking to call a HTTP_POST from the iPhone SDK to a php file on my server. If I call this below:
NSMutableDictionary *requestDictionary = [NSMutableDictionary dictionary];
[requestDictionary setObject:@"testvalue" forKey:@"test"];
[requestDictionary setObject:@"test2value" forKey:@"test2"];
[requestDictionary setObject:@"test3value" forKey:@"test3"];
NSString *jsonString = [requestDictionary JSONRepresentation];
NSString *urlString = @"http://www.example.org";
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:CONNECTION_TIMEOUT];
NSData *requestData = [NSData dataWithBytes:[jsonString UTF8String] length:[jsonString length]];
[request setHTTPMethod:@"POST"];
[request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody: requestData];
NSURLConnection *urlConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
Should I be able to receive this post data like so (in php):
echo "Hello";
if(isset($_POST['test']))
{
echo $_POST['test'];
}
EDIT:
To test, in my connectionDidFinishLoading
I'm simply logging the whole response
- (void)connectionDidFinishLoading:(NSURLConnection *)conn
{
NSString *responseString = [[NSString alloc] initWithDa开发者_C百科ta:dataRecieved encoding:NSASCIIStringEncoding];
[dataRecieved release];
NSLog(@"Response: %@", responseString);
}
I get the hello that I echoed before looking for the post, but nothing else. Does anyone know where I'm going wrong?
Your request body is a JSON value, but your server expects application/x-www-form-urlencoded
encoded fields.
You can convert the dictionary to form encoded fields with the following category.
@interface NSDictionary (FormData)
- (NSString*) formUrlEncodedData;
@end
@implementation NSDictionary (FormData)
- (NSString*) formUrlEncodedData
{
NSString *result = nil;
for( NSString *k in [self keyEnumerator]) {
if (![result isEqual:@""]) {
result = [result stringByAppendingString:@"&"];
}
result = [result stringByAppendingString:[k stringByUrlEncoding]];
result = [result stringByAppendingString:@"="];
NSString *v = [self objectForKey:k];
result = [result stringByAppendingString:[v stringByUrlEncoding]];
}
return result;
}
@end
Where stringByUrlEncoding is provided by a category on NSString, which I sourced from Cocoanetics.
@interface NSString (Helpers)
- (NSString*) stringByUrlEncoding;
@end
@implementation NSString (UrlEncoding)
- (NSString*) stringByUrlEncoding
{
return (NSString*)CFURLCreateStringByAddingPercentEscapes(NULL,
(CFStringRef)self,
NULL,
(CFStringRef)@"!*'();:@&=+$,/?%#[]",
kCFStringEncodingUTF8);
}
@end
You will get the response in the NSURLConnection delegate methods. And once you get the response you can use NSXMLParser to parse it and get the fields you want. Let me know if you need additional info on the same. Will try to post some relevant code.
If i can you give an advice. Checks the source code of the Facebook IOS SDK. It is a good way to learn a new technology.
精彩评论