0

this is my insert.php script

<?php

$name=$_POST['Name'] ?? ''  ;
$address=$_POST['Address'] ?? '' ;

include("db_config.php");

$result = mysqli_query($con,"INSERT INTO `Firm`(`ID`,`NAME`,`ADDRESS`) VALUES ('','$name','$address')");    

$response = array();
 if($result){
    $response['success']=1;
    $response['message']="Record Inserted Successfully-".$result."--";
 }
else { 
    $response['success']=0;
    $response['message']="Insertion Failure-".$result."--";
 }

echo json_encode($response);

?>

this is the response code

request = new StringRequest(Request.Method.POST, Utils.INSERT_FIRM, new Response.Listener<String>() {
                    @Override
                    public void onResponse(String s) {
//                        Log.i("R","Response is "+s);

                        try {
                            JSONObject jsonObject = new JSONObject(s);
                            int success = jsonObject.getInt("success");
                            String message = jsonObject.getString("message");
                            Toast.makeText(Add_Firm.this,""+message+":"+ success,Toast.LENGTH_SHORT).show();

                        } catch (Exception e) {
                            Toast.makeText(Add_Firm.this, "Some Error"+e.toString(), Toast.LENGTH_SHORT).show();
                            Log.i("Error"," "+e.toString());
                        }
                    }

I am using php script to connect android with database When I click the submit button in application although insertion is happening at database but I keep getting this exception in android

org.json.JSONException: Value Connection of type java.lang.String cannot be converted to JSONObject

What is the issue ?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Aayush
  • 1
  • 2
  • Try to set the content type to `json` ex: https://stackoverflow.com/questions/4064444/returning-json-from-a-php-script – ka_lin Jul 09 '17 at 20:04
  • After going through this link , I added. header('Content-type:application/json;charset=utf-8'); to the starting go php script...still my application throws JsonException: Value connection of type String cannot be converted to JsonObject..Any solution ? – Aayush Jul 10 '17 at 08:56
  • What does this print: Log.i("R","Response is "+s);? – Juan Jul 10 '17 at 14:07
  • thanks. The issue was that I had a statement echo "working"; because of which it was showing such error – Aayush Jul 17 '17 at 09:34

0 Answers0