Trying to get a value from my PHP API, on android, using volley... my API is working fine, returning the value I want, but on my android I can seem to save the value on a variable.
this is my PHP script
<?php
if($_SERVER['REQUEST_METHOD']=='POST'){
$titu = $_POST['titulo'];
$id_use = $_POST['id_user'];
$difi = $_POST['dificuldade'];
$user = "root";
$pass = "";
$host= "localhost";
$dbname="check";
$con = mysqli_connect($host,$user,$pass,$dbname);
$sql="insert into trilhos(titulo,id_user,dificuldade)
values('".$titu."','".$id_use."','".$difi."');";
$r = mysqli_query($con,$sql);
$id_trilho =mysqli_insert_id($con);
$result = array();
echo json_encode(array("result"=>$id_trilho));
mysqli_close($con);
}
on my android studio, I try to get the value like this, I send 3 values to insert into the database, and then I want to return the id of the row I inserted back to android. I have looked at many answers on StackOverflow but I just can't seem to understand and solve my problem.
public void insert(String titulo, String id_trilho, String dif) {
url = "http://192.168.1.68/loginsignup/insertTrilho.php?titulo=" + titulo +"&id_user="+ id_trilho+ "&dificuldade=" + dif + "";
Log.i("Http", "" + url);
RequestQueue requestQueue = Volley.newRequestQueue(InserirTrilho.this);
StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray jsonArray = jsonObject.getJSONArray("result");
JSONObject jsonObject1 = jsonArray.getJSONObject(0);
String id = jsonObject1.getString("id_trilho");
Toast.makeText(InserirTrilho.this, id, Toast.LENGTH_SHORT).show();
SharedPreferences shared_id = getSharedPreferences("Mytrilho", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = shared_id.edit();
editor.putString("id", id);
editor.commit();
Intent intent = new Intent(InserirTrilho.this, MapsActivity.class);
startActivity(intent);
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(InserirTrilho.this, "Dados Errados", Toast.LENGTH_SHORT).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.i("HttpError","erro"+error);
}
});
requestQueue.add(stringRequest);
}
keep getting a thrown exception, can someone help me out, thank you in advance