【发布时间】:2011-09-12 00:02:05
【问题描述】:
我需要一点帮助...我正在尝试使用 apptrackr API (API Apptrackr - You must be registred ) 使用我的 iPhone 应用程序但没有成功。我有一些适用于 API 的 php 示例代码,但我不知道如何使用 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);
?>
这是我的 Objective-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:对不起我的英语不好
【问题讨论】: