PHP Json request to NSMutableRequest! Help?
I need a little help... i'm trying to use the apptrackr API (API Apptrackr - You must be registred ) with my iPhone application but without success. I have some php example code that works fine with the API but i don't know how to do that with objective-c. I already do some test but i really don't know where i'm wrong!
That's the PHP code:
<?php
$request = array('object' => 'Link',
'action' => 'get',
'args' => array(
'all_version' => true,
'app_id' => 284972998
)
);
// Wrap the request and JSON encode it.
$wrapper = array(
'request' => json_encode($request)
);
// JSON and URLencode the wrapper.
$wrapper = urlencode(json_encode($wrapper));
// Now, using the cURL extension, we perform the API request.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://api.apptrackr.org/');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "request=$wrapper");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
curl_close($ch);
// print the output
print_r($data);
?>
and that's my objective-c code:
NSMutableDictionary *arg = [NSMutableDictionary dictionary];
[arg setObject:[NSNumber numberWithBool:Y开发者_如何学运维ES] forKey:@"all_versions"];
[arg setObject:[NSNumber numberWithInt:284972998] forKey:@"app_id"];
NSMutableDictionary *requestDictionary = [NSMutableDictionary dictionary];
[requestDictionary setObject:@"Link" forKey:@"object"];
[requestDictionary setObject:@"get" forKey:@"action"];
[requestDictionary setObject:arg forKey:@"args"];
NSString *jsonString = [requestDict JSONRepresentation];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://api.apptrackr.org"]];
[request setHTTPMethod:@"POST"];
[request setValue:jsonString forHTTPHeaderField:@"request"];
NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
NSString *recivedSTR = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"RecivedData: %@", recivedSTR);
}
I hope someone can help me! For now thanks a lot! PS: Sorry for my bad english
I guess you shouldn't put your jsonString in the HTTP header field, but in the POST Body of your request.
Try this:
[request setHTTPBody:[NSString stringWithFormat:@"request=%@",jsonString]];
精彩评论