Reputation: 11
Android Java code
private void delete(){
StringRequest stringRequest = new StringRequest(Request.Method.POST, "http://192.168.43.63/deletr.php",
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject res = new JSONObject(response);
if (res.has("Deleted")) {
String aJsonString = res.getString("Deleted");
Toast.makeText(getActivity(), aJsonString, Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getActivity(), "Something went wrong",Toast.LENGTH_LONG).show();
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("title",getCarEdit.getText().toString().trim());
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(getContext());
requestQueue.add(stringRequest);
}
PHP code
<?php
include 'connection.php';
global $connect;
$id = $_POST["title"];
$query = "DELETE FROM `products` WHERE title='$id'";
$result = mysqli_query($connect, $query);
if ($result) {
echo json_encode("Deleted");
}
else{
echo json_encode("Delete Failed");
}
php response PHP response after asigned some manual values
Upvotes: 1
Views: 1942
Reputation: 1316
Changes in your PHP file :
$success = array(
"output"=>"your data Deleted");
$error = array(
"output"=>"your data not Deleted");
// Use json_encode() function
if($result){
$json = json_encode($success);
}else{
$json = json_encode($error);
}
// Display the output
echo($json);
//
// check hear your result if else code
Output like : {"output":"your data Deleted"}
and in your android code :
StringRequest request = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Toast.makeText(MainActivity.this,response.toString(),Toast.LENGTH_LONG).show();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("error",error.toString());
}
});
I hope it'll help you...!
Upvotes: 1
Reputation: 1925
As your response is already in string as you mentioned here: https://i.sstatic.net/UKhwC.png
First check your response with Log.e()
;
@Override public void onResponse(String response) {
Log.e("response=="," "+response);
try {
// Your response is already in String, so just past it in toast message.
Toast.makeText(getActivity(), response, Toast.LENGTH_LONG).show();
} catch (JSONException e) {
e.printStackTrace();
}
}
}
Upvotes: 0
Reputation: 9225
I don't know php but your android code is ok, we have use two ways to get api response .
first : simple use json paring
JSONObject res = new JSONObject(response);
if (res.has("Deleted")) {
String aJsonString = res.getString("Deleted");
Toast.makeText(getActivity(), aJsonString, Toast.LENGTH_LONG).show();
}
second : assign whole api response to pojo class and get data from Pojo class
add this gradle
First Step
implementation 'com.google.code.gson:gson:2.8.5'
Second Step
copy response and create pojo class using http://www.jsonschema2pojo.org/
Third Step
Gson gson = new GsonBuilder().create();
Response r = gson.fromJson(jsonString, Response.class);
Toast.makeText(getActivity(), r.getDeleted, Toast.LENGTH_LONG).show();
In My case my pojo class name is Response
Upvotes: 0