I've have tried many method from Stack Overflow on how to retrieve the php value to android app but no success. I have no idea what's wrong here. Can someone figure out the problem for me? or at least a link/ hint ?
Firstly, I'm call addInformation(name, weather, date2, status, first1[1], last1[1]); //to insert the data into MySQL through php.
Then showList (); // show the lastID from php
Finally display it Toast.makeText(getApplicationContext(),"aa"+lastID,Toast.LENGTH_LONG).show();
addInformation(name, weather, date2, status, first1[1], last1[1]); showList(); Toast.makeText(getApplicationContext(),lastID,Toast.LENGTH_LONG).show(); I declared the lastID as global.
This is my addInformation function
public void addInformation(final String name, final String weather, final String date2, final String status, final String timeIn, final String timeOut) { class AddInfo extends AsyncTask<String, Void, String> { ProgressDialog loading; @Override protected void onPreExecute() { super.onPreExecute(); loading = ProgressDialog.show(WorkDetailsTable.this, "Please Wait", null, true, true); } @Override protected void onPostExecute(String s) { super.onPostExecute(s); loading.dismiss(); Toast.makeText(getApplicationContext(),s, Toast.LENGTH_LONG).show(); } @Override protected String doInBackground(String... params) { HashMap<String, String> data = new HashMap<String, String>(); data.put(Config.KEY_USER_NAME, name); data.put(Config.KEY_WEATHER, weather); data.put(Config.KEY_DATE, date2); data.put(Config.KEY_STATUS, status); data.put(Config.KEY_TIMEIN, timeIn); data.put(Config.KEY_TIMEOUT, timeOut); RequestHandler rh = new RequestHandler(); String result = rh.sendPostRequest(Config.ADD_INFORMATION, data); return result; } } AddInfo ru = new AddInfo(); ru.execute(name, weather, date2, status, timeIn, timeOut); } addInformation.php
<?php require_once('dbConnect.php'); if($_SERVER['REQUEST_METHOD']=='POST'){ $response = array(); //Getting values $name = $_POST['name']; $weather = $_POST['weather']; $date = $_POST['date']; $status = $_POST['status']; $timeIn = $_POST['timeIn']; $timeOut = $_POST['timeOut']; //Creating an sql query $sql = "INSERT INTO information(name, weather, date, status, time_in, time_out) VALUES ('$name','$weather','$date', '$status', '$timeIn', '$timeOut')"; //Importing our db connection script require_once('dbConnect.php'); //Executing query to database if(mysqli_query($con,$sql)){ echo 'Information Added Successfully'; $insertId= mysqli_insert_id($con); $response["lastId"]=$insertId; echo json_encode($response); }else{ echo 'Could Not Add Information'; } //Closing the database mysqli_close($con); } ?> // I get Information Added Successfully follow by the latest ID Next, go to showList() to get the latestID by using the same php script.
public void showList() { class GetLastID extends AsyncTask<Void, Void, String> { private ProgressDialog progressDialog = new ProgressDialog(WorkDetailsTable.this); InputStream inputStream = null; String result = ""; protected void onPreExecute() { progressDialog.setMessage("Downloading your data..."); progressDialog.show(); progressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() { public void onCancel(DialogInterface arg0) { GetLastID.this.cancel(true); } }); } @Override protected String doInBackground(Void... params) { String url_select = "http://192.168.107.115/Android/CRUD/addInformation.php"; // same php in addInformation, get the latestID and return to android app ArrayList<NameValuePair> param = new ArrayList<NameValuePair>(); try { // Set up HTTP post // HttpClient is more then less deprecated. Need to change to URLConnection HttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(url_select); httpPost.setEntity(new UrlEncodedFormEntity(param)); HttpResponse httpResponse = httpClient.execute(httpPost); HttpEntity httpEntity = httpResponse.getEntity(); // Read content & Log inputStream = httpEntity.getContent(); } catch (UnsupportedEncodingException e1) { Toast.makeText(getApplicationContext(), "ERROR", Toast.LENGTH_LONG).show(); //Log.e("UnsupportedEncodingException", e1.toString()); //e1.printStackTrace(); } catch (ClientProtocolException e2) { Toast.makeText(getApplicationContext(), "ERROR 1", Toast.LENGTH_LONG).show(); //Log.e("ClientProtocolException", e2.toString()); // e2.printStackTrace(); } catch (IllegalStateException e3) { Toast.makeText(getApplicationContext(), "ERROR 2", Toast.LENGTH_LONG).show(); // Log.e("IllegalStateException", e3.toString()); // e3.printStackTrace(); } catch (IOException e4) { Toast.makeText(getApplicationContext(), "ERROR 3", Toast.LENGTH_LONG).show(); // Log.e("IOException", e4.toString()); //e4.printStackTrace(); } // Convert response to string using String Builder try { BufferedReader bReader = new BufferedReader(new InputStreamReader(inputStream, "utf-8"), 8); StringBuilder sBuilder = new StringBuilder(); String line = null; while ((line = bReader.readLine()) != null) { sBuilder.append(line + "\n"); } inputStream.close(); result = sBuilder.toString(); JSONObject resultObject = new JSONObject(result); lastID = resultObject.getString("lastId"); // get the latestID from php Log.e("TAG",lastID); } catch (Exception e) { Log.e("TAG", "A"); } return result; } } } I suppose the Toast will display the latest ID, but it display null instead.
Edited
addInformation()
@Override protected void onPostExecute(String s) { super.onPostExecute(s); loading.dismiss(); showList(); showList()
public void showList() { class GetLastID extends AsyncTask<Void, Void, String> { private ProgressDialog progressDialog = new ProgressDialog(WorkDetailsTable.this); InputStream inputStream = null; String result = ""; ProgressDialog loading; protected void onPreExecute() { progressDialog.setMessage("Downloading your data..."); progressDialog.show(); progressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() { public void onCancel(DialogInterface arg0) { GetLastID.this.cancel(true); } }); } @Override protected void onPostExecute(String s) { super.onPostExecute(s); loading.dismiss(); Toast.makeText(getApplicationContext(),"aa"+lastID,Toast.LENGTH_LONG).show(); }