1

I have a problem about getting JSON value from php file.

How can I fix it?

<?php


    //If the values are not blank
    //Connecting to our database by calling dbConnect script 
    include('connection.php');

    $id =$_POST["uyeId"];

    Class UyeBilgiler{
        public $uyeAd = "";
        public $uyeYas = "";
        public $uyeOkul = "";
        public $uyeResim = "";
        public $uyeEmail = "";
    }

    $uyeBilgiler = new UyeBilgiler();

    $informationSql = "SELECT * FROM bilgiler WHERE id = '$id' ";

    $list = mysqli_query($conn,$informationSql);

    while($result = mysqli_fetch_assoc($list)){
        $uyeBilgiler->uyeAd = $result["uyeAd"];
        $uyeBilgiler->uyeYas = $result["uyeYas"];
        $uyeBilgiler->uyeOkul = $result["uyeOkul"];
        $uyeBilgiler->uyeResim = $result["uyeResim"];
        $uyeBilgiler->uyeEmail = $result["uyeEmail"];

        echo json_encode($uyeBilgiler,JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT);
    }


?>

When I assign a value to $id variable as 1, the output of json format is displayed below.

{ "uyeAd": "Ali yılmaz", "uyeYas": "29", "uyeOkul": "Ali Myo", "uyeResim": "bir.jpg", "uyeEmail": "ali@gmail.com" }

There is a gap after first fancy braces and before last fancy braces.

For android part,

@GET("/getInformationByUyeId.php")
Call<UyeBilgiler>  bilgiGetir(@Query("uyeId") String id);

When I define a $id manually ($id = "1") without using $_POST["uyeId"], I get a json file without any error. But using $_POST["uyeId"] throw an error as shown below.

Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $

1 Answers1

1

Create a model class from your API response like this.

@SerializedName("uyeAd")
@Expose
private String uyeAd;
@SerializedName("uyeYas")
@Expose
private String uyeYas;
@SerializedName("uyeOkul")
@Expose
private String uyeOkul;
@SerializedName("uyeResim")
@Expose
private String uyeResim;
@SerializedName("uyeEmail")
@Expose
private String uyeEmail;

And assign your response to your model class like this

if(response.isSuccessful())
  {
     modelClass = response.body();
  }

Create getter setter mother in modelClass and get the value from it and show in your text view txt.setText("Isminiz : " + modelClass.getName());

MustafaShaikh
  • 142
  • 2
  • 10