1

次のコードを使用して配列をphpサーバーに送信していますが、php側では値を取得していません。配列をjson文字列として送信しています。

- (void)postArray {
//Create the URL request
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://test.com./t.php"]];
    NSMutableArray *array = [[NSMutableArray alloc] init];
    [array addObject:@"one"];
    [array addObject:@"Two"];
    [array addObject:@"three"];
    [array addObject:@"four"];
    [request setHTTPMethod:@"POST"];
   // NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:array,@"comment", nil];
    SBJSON *parser =[[SBJSON alloc] init];
   // NSString *jsonString = [parser stringWithObject:dict];
    NSString *jsonString = [parser stringWithObject:array];
    NSLog(@"Str %@",jsonString);
    [request setValue:@"application/x-www-form-urlencoded"
   forHTTPHeaderField:@"Content-Type"];
    [request setValue:jsonString forHTTPHeaderField:@"comment"];
   [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue]  completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {

        NSHTTPURLResponse *HTTPResponse = (NSHTTPURLResponse *)response;
        NSInteger statusCode = [HTTPResponse statusCode];


        if (statusCode==200) {


            //Request goes in success
            NSError* error;
            NSMutableDictionary* json = [NSJSONSerialization
                                         JSONObjectWithData:data //1
                                         options:kNilOptions
                                         error:&error];
            NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];


            NSLog(@"Json for post array ----------%@",str);

        }else{

            ///request is get failed

            NSLog(@"Error Description %@",[error description]);
        }
    }];

    [request release];


}

そしてphpでの出力は

<pre><br>これは Iphone から取得する応答です<br>最初<br><br>最初に終了します<br>2 番目に json 応答を受信し、json 応答をデコードします<br><br>nullThird commaArray ( [0 ] => )

ここにphpコードがあります

<?php

echo "<pre>";
echo "<br>";
//echo "Testing array";
$totalitem=$_POST['total'];
echo "This is response getting from the Iphone";
echo "<br>";
echo "First";
echo $jsonarray1=$_POST['comment'];
echo "<br>";
print_r($jsonarray1);
echo "<br>";
echo "end First";
echo "<br>";



echo "Second receive json response and decode the json response";
echo "<br>";
echo $comment=$_POST['comment'];
echo "<br>";
$jsonarray=json_decode($comment);
echo json_encode($jsonarray);


echo "Third Breaking from comma";
echo "<br>";
$allval=explode(',',$jsonarray1);
print_r($allval);

?>

助けてください

4

2 に答える 2

0

次のコードを試してみてください。NSMutableDictionary を作成してから POST してみてください。

        NSMutableDictionary *dictionnary = [NSMutableDictionary dictionary];
        [dictionnary setObject:@"First" forKey:@"First"];


        NSError *error = nil;
        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictionnary
                                                           options:kNilOptions
                                                             error:&error];   

        NSString *urlString =@"Your URL";

        NSURL *url = [NSURL URLWithString:urlString];
 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
        [request setHTTPMethod:@"POST"];

        [request setHTTPBody:jsonData];
        NSURLResponse *response = NULL;
        NSError *requestError = NULL;
        NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&requestError];
        NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding] ;
         NSLog(@"%@", responseString);
于 2012-11-30T07:53:19.177 に答える