简体   繁体   中英

how can i send data from my device to my php file via json

i couldn't send data from my device to my php file. i am trying to send data from device using json. this is my php code

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

and my client code:

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);

i couldnt find the problem. can you help me?

I'd recommend you following two libraries, they will make it very easy to do your task:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM