便携端(苹果:xcode ;安卓:Java)调用PHP的API

手头一个小项目,服务端是PHP的,做了几个API供苹果的PAD使用.

其实就是一个简单的模拟页面POST

大概的实现方式写一下自己备忘

Xcode:

NSString *urlString = @"http://WebAPI.php";
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
NSString *variableToSend = [NSString stringWithFormat:@"objJsonData={'para1':'value1','para2':'value2'}"];
[request setHTTPMethod:@"POST"];
[request setValue:[NSString stringWithFormat:@"%d", [variableToSend length]] forHTTPHeaderField:@"Content-length"];
[request setHTTPBody:[variableToSend dataUsingEncoding:NSUTF8StringEncoding]];
// Create url connection and fire request
NSData *response = [NSURLConnection sendSynchronousRequest:request
                                         returningResponse:nil error:nil];
NSLog(@"Response: %@",[[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]);

PHP端(Service)

$objJsonInfo = $_POST['objJsonData'];
$obj = json_decode($objJsonInfo);
$para1 =  $obj->{'para1'};
$para2 =  $obj->{'para2'}; 

可以用简单的网页测试API

安卓的以后有机会再补吧,懒了,找代码太麻烦。