いくつかのことを変更し、いくつかの新しいヒントを得ました。
私の最初のプロジェクトでは、iPad からいくつかのデータを送信し、それを自分のコンピューター (MAMP) で実行されている SQL データベースに挿入する必要があります。現時点では、文字列「city」と「country」のみに送信しようとしています。実際には、2 つの問題を解決できません
a) SBJSON 3,1 をダウンロードし、プロジェクトにクラスを追加しsbjson.h
、コードをインポートして記述しました。
それぞれの下に、NSLog
私が得たものを書きました:
#import "SBJson.h"
…
NSArray *keys = [NSArray arrayWithObjects:@"name",@"surname",@"address",@"email",@"city",@"country",@"gender",@"phone",@"age",@"store",@"time", nil];
NSArray *objects = [NSArray arrayWithObjects:name.text,surname.text,address.text,email.text,city.text,country.text,genderLabel,phone.text,age.text,storeCity,lapTime, nil];
NSDictionary *jsonDictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
NSString *jsonString = [jsonDictionary JSONRepresentation];
NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:NO];
NSString *postLength = [NSString stringWithFormat:@"%d", [jsonData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]init];
[request setURL:[NSURL URLWithString:@"http://192.168.2.111/*****acing/prueba.php"]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:jsonData];
NSLog(@"jsonData: %@",jsonData);//1
jsonData: <7b227469 6d65223a 2230313a 30373a30 35222c22 61646472 65737322 3a224d61 78696d69 6c69616e 74726173 73652c20 32222c22 6e616d65 223a224f 74746f22 2c226369 7479223a 224dc39c 4e434845 4e222c22 67656e64 6572223a 224d616c 65222c22 70686f6e 65223a22 2b343935 35353232 32323333 222c2265 6d61696c 223a226f 74746f40 64657574 63686c61 6e642e64 65222c22 7375726e 616d6522 3a225072 656d696e 67657222 2c22636f 756e7472 79223a22 4745524d 414e5922 2c226167 65223a22 3630222c 2273746f 7265223a 224dc39c 4e434845 4e227d>
NSLog(@"jsonLength: %d",jsonData.length);//2
223
NSData *urlData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil
error:nil];
NSString *response = [[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
NSLog(@"response: %@",response); //3
{"time":"01:07:05","address":"Maximiliantrasse, 2","name":"Otto","city":"MÜNCHEN","gender":"Male","phone":"+49555222233","email":"otto@deutchland.de","surname":"Preminger","country":"GERMANY","age":"60","store":"MÜNCHEN"}
b)私はphpについて何も知らず、「prueba.php」に書いたウェブを検索しています。MySQL は php に接続されており、フォームで値を挿入できますが、データを取得する方法 (php ファイル oO に到着した場合) と値をデータベースに挿入する方法がわかりません。
<?php
$hostname_ndb = "localhost";
$database_ndb = "PepeRacing";
$username_ndb = "root";
$password_ndb = "rona5lda";
$ndb = mysql_connect($hostname_ndb, $username_ndb, $password_ndb) or die("Could not connect");
mysql_select_db($database_ndb) or die("Could not connect to the database");
//get file
$body = @file_get_contents('php://input');
echo $body;
$datosJSON = json_decode($body);
print_r($datosJSON);
$datosJSON=utf8_encode($datosJSON);
mysql_query("INSERT INTO players (city,country,name) VALUES ('{$datosJSON['city']}','{$datosJSON['country']}','{$datosJSON['name']}')");
?>
しかし、次を使用する場合:
<?php
if($_POST) {
echo "recibo un paquetito- POST";
//recibo los datos y los descodifico con PHP
$misDatosJSON = json_decode($_POST["jsonData"]);
//debería haber una salida con los datos recibidos, no?
print_r($misDatosJSON);
$salida="";
$salida .="something: " .$misDatosJSON[1];
echo $salida;
}else{
echo "got nothing";
}
?>
出力は「何もありません」です
POST メソッドが機能しないことは明らかなようですが、それを解決するためのアイデアはありますか?
読んでくれてありがとう!