Store Php Value To Java
Solution 1:
Based on the comments, I am creating new answer, as both my answers are correct in the basics. I made example, working with android (apache commons 4.5.1) and php 5.6. Both version (4.5.1, 5.6) are not requirements, just what I am using right now.
Example assume you have a mysql table called information with fields status, time_in and with another field marked as AUTO_INCREMENT.
Java Part
in original ::doInBackground(String ...params) function you could have
HttpClient client = HttpClients.createDefault();
HttpPost httpPost = new HttpPost("http://localhost/so/sendrequest/addInformation.php");
try{
List<NameValuePair> data = new ArrayList<NameValuePair>(2);
data.add(new BasicNameValuePair("status", "ok"));
data.add(new BasicNameValuePair("timein", "12:55"));
httpPost.setEntity(new UrlEncodedFormEntity(data));
String response = EntityUtils.toString(client.execute(httpPost).getEntity());
System.out.println(response); //here you have your insertid
}catch(ClientProtocolException e){
// TODO Auto-generated catch block
}catch(IOException e){
// TODO Auto-generated catch block
}
based on native Java
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
and apache commons, which should be included with android library (download link if not)
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
PHP Part
addInformation.php
<?phpclassAddInformation{
functionresponse(){
/** @var mysqli $con */require_once('dbConnect.php'); //$con = new mysqli('127.0.0.1', 'root', '', 'so');$status = $con->real_escape_string($_POST['status']);
$timein = $con->real_escape_string($_POST['timein']);
$con->query("INSERT INTO information (status, time_in) VALUES ('$status', '$timein')");
echo$con->insert_id;
}
}
$ai = new AddInformation();
$ai->response();
Solution 2:
As first, the line
echo'Information Added Successfully';
will break your json format, I suggest you to format the desired json like
{"status":"ok","message":"Information Added Successfully","data":{"lastid":1234}}
after that, read what to do with
Stringresult= rh.sendPostRequest(Config.ADD_INFORMATION, data);
at How to convert String to JSONObject in Java. Most satisfying approach is to prepare java class for the json response, eg. like
classAddInformationResponse{
publicString status;
publicString messsage;
public ObjectNode data;
}
you can read how to map json to java object in one of many tutorials.
Solution 3:
In the ADD_INFORMATION script, all you need to do is get the data after the insert statement. Therefore I would use something similar to the below:
<?phpif($_SERVER['REQUEST_METHOD']=='POST'){
//Getting values$status = $_POST['status'];
$timeIn = $_POST['timeIn'];
//Creating an sql query$sql = "INSERT INTO information(status, time_in) VALUES ('$status', '$timeIn')";
//Importing our db connection scriptrequire_once('dbConnect.php');
//Executing query to databaseif(mysqli_query($con,$sql)){
$sql = "SELECT * FROM INFORMATION ORDER BY time_in DESC LIMIT 1";
echo$sql;
$insertId=mysql_insert_id();
echo json_encode($insertId);
}else{
echo'Could Not Add Information';
}
//Closing the database
mysqli_close($con);
}
?>
In reality what I did was create a SELECT statement on the data I have just inserted in the INSERT statement.
Hope this helps.
Post a Comment for "Store Php Value To Java"