I'm currently developing an Android application for a small project which consists in learning development on Android platform. I already did all the app and now I need to send data from the app to a server in PHP with MySQL database. I have a web service to make the insert in the database from data sent through a json format. I have tested the web service with Advanced Rest Client and I have succeeded in inserting data into my database. Now I'm testing from my android app application but it doesn't work. I have checked the connection status and it is "OK" but when I check the database, there is no insertion.
Below is my web service in PHP:
<?php
$json = file_get_contents('php://input');
$obj = json_decode($json);
$con = mysql_connect('host','login','password')
or die('Cannot connect to the DB');
mysql_select_db('database_name',$con);
mysql_query("INSERT INTO `commercial` (firstName, surNameC, email, number, salonName, uuid)
VALUES ('".$obj->{'firstname'}."','".$obj->{'name'}."','".$obj-> {'email'}."','".$obj->{'phonenumber'}."','".$obj->{'fairreference'}."','".$obj-> {'commercialuuid'}."')");
mysql_close($con);
$posts = "INSERTION OK";
header('Content-type: application/json');
echo json_encode(array('posts'=>$posts));
?>
And the sending process from the android side:
URL url;
HttpURLConnection urlConnection;
StringBuilder strBuilder;
try {
url = new URL(URL);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("POST");
urlConnection.setDoOutput(true);
urlConnection.setUseCaches(false);
urlConnection.setRequestProperty("Content-Type", "application/json");
urlConnection.setReadTimeout(10000);
urlConnection.setConnectTimeout(15000);
urlConnection.connect();
JSONObject jsonObject = new JSONObject();
jsonObject.put("commercialuuid", args[0]);
jsonObject.put("name", args[1]);
jsonObject.put("firstname", args[2]);
jsonObject.put("phonenumber", args[3]);
jsonObject.put("email", args[4]);
jsonObject.put("fairreference", args[5]);
Log.d("json", jsonObject.toString());
Log.d("request", "starting");
OutputStreamWriter writer = new OutputStreamWriter(urlConnection.getOutputStream());
writer.write(jsonObject.toString());
writer.flush();
Log.e("connection status",urlConnection.getResponseMessage());
//get the response from the web service
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
strBuilder = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
strBuilder.append(line);
}
writer.close();
reader.close();
urlConnection.disconnect();
return null;
} catch (Exception e) {
e.printStackTrace();
}
As I have searched for several threads that have been validated and based my work on it, I really don't understand why it doesn't work. I would be grateful if anyone could help me.
Thanks.
mysql_query(), take a look at this: github.com/dmnugent80/ShareAppServerSide/blob/master/…