我需要一些帮助......我正在尝试使用apptrackr API(API Apptrackr - You must be registred ) 使用我的iPhone应用程序,但没有成功。我有一些PHP示例代码与API工作正常,但我不知道如何使用objective-c。我已经做了一些测试,但我真的不知道我哪里错了!
这是PHP代码:
<?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);
?>
那是我的目标-c代码:
NSMutableDictionary *arg = [NSMutableDictionary dictionary];
[arg setObject:[NSNumber numberWithBool:YES] 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);
}
我希望有人可以帮助我!现在非常感谢! PS:抱歉我的英文不好
答案 0 :(得分:0)
我猜你不应该把你的jsonString放在HTTP头字段中,而是放在你请求的POST Body中。
试试这个:
[request setHTTPBody:[NSString stringWithFormat:@"request=%@",jsonString]];