Reputation: 71
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
Upvotes: 0
Views: 2239
Reputation: 37404
The issue is, url is a type of Get
request but the method is in java request is of type POST
so you need to send data as part of the request using as Body
so
see how to send a POST request using volley with string body?
As commented , Your response is jsonobject
not array so use
JSONObject jsonObject = new JSONObject(response);
String id = jsonObject.optString("result");
instead of
JSONObject jsonObject = new JSONObject(response);
JSONArray jsonArray = jsonObject.getJSONArray("result");
JSONObject jsonObject1 = jsonArray.getJSONObject(0);
String id = jsonObject1.getString("id_trilho");
or to get what you want then use
$result = array();
$result["result"]= array();
array_push($result["result"],array("id_trilho"=>$id_trilho)); // assume $id_trilho =0
echo json_encode($result);
// output {"result":[{"id_trilho":0}]}
or simply use
$id_trilho = 0;
$result = array();
array_push($result,array("id_trilho"=>$id_trilho));
echo json_encode($result);
// output [{"id_trilho":0}]
// then use JSONArray jsonArray = new JSONArray(response);
// JSONObject jsonObject1 = jsonArray.getJSONObject(0);
// String id = jsonObject1.getString("id_trilho");
Upvotes: 3