Hello !
I'm trying to build a link between an applet in java and php. My applet in Java sends data to php. And then php writes data in the database. I use json object
my code in java :
public static void main(String[] args) {
// TODO Auto-generated method stub
JSONObject json = new JSONObject();
json.put("json", "mkyong.com");
//obj.put("age", new Integer(100));
System.out.print(json);
System.out.print("\n");
System.out.print("\n");
try {
URL recup = new URL("http://localhost/json_good/recup_v2.php");
java.net.HttpURLConnection connexion = (HttpURLConnection)recup.openConnection();
connexion.setDoOutput(true); // Pour pouvoir envoyer des données
connexion.setRequestMethod("POST");
connexion.setRequestProperty("Content-type", "application/json");
//envoi de la requête
OutputStreamWriter writer = null;
writer = new OutputStreamWriter(connexion.getOutputStream());
//conversion en chaine
//System.out.print(json.toJSONString());
writer.write(json.toJSONString());
writer.flush();
writer.close();
//#2 lecture de la réponse
BufferedReader reader=null;
reader = new BufferedReader(new InputStreamReader(connexion.getInputStream()));
String ligne;
while ((ligne = reader.readLine()) != null) {
System.out.println(ligne);}
} catch (Exception e) {
System.out.println(" exception : " + e.getMessage());
}
}
My code in php to receive data :
include('config.php');
mysql_connect('localhost', 'root', '');
mysql_select_db('tangram');
if(mysql_select_db('tangram'))
echo 'OK';
$json=file_get_contents("php://input");
var_dump(json_decode($json));
$raw_json = json_decode($json,true);
echo $raw_json["json"];
$var=$raw_json["json"];
mysql_query('insert into tb_user(name, surname, email) values ("'.$var.'", "'.$var.'", "'.$var.'")');
Well the link between java and php works but when I add a request to write my data in my database it doesn't work. Why ? Help me please
Lucile