如何通过json将数据从我的设备发送到我的php文件

时间:2012-02-26 16:35:53

标签: php ios json

我无法将数据从我的设备发送到我的php文件。我正在尝试使用json从设备发送数据。 这是我的PHP代码

$cevap = $_POST["cevap"];
echo $cevap;

和我的客户代码:

NSString* postBody = @"cevap=ali";
NSLog(@"%@",postBody);
NSLog(@"postbody: %@",postBody);


NSData *postData = [postBody dataUsingEncoding:NSUTF8StringEncoding];


NSURL* urlData = [NSURL URLWithString:@"http://yatanadam.com/sonuc.php"];



NSMutableURLRequest* request1 = [NSMutableURLRequest requestWithURL:urlData 
                                                       cachePolicy:
                                NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];


[request1 setHTTPMethod:@"POST"];
[request1 setHTTPBody:postData];
 [request1 setValue:[NSString stringWithFormat:@"%d",[postData length]] 
   forHTTPHeaderField:@"Content-Length"];

conn = [[NSURLConnection alloc] initWithRequest:request1 delegate:self];
if (conn) {

    receivedData = [[NSMutableData data] retain];

}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {

receivedString = [[NSString alloc] initWithData:receivedData  encoding:NSUTF8StringEncoding];
SBJSON *parser = [[SBJSON alloc] init];
result = [parser objectWithString:receivedString error:nil];
[parser release], parser = nil;
// Set tableData
NSLog(@"%@",result);

我找不到问题。你能救我吗?

1 个答案:

答案 0 :(得分:0)

我建议您关注两个库,它们可以让您轻松完成任务: