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 $

S.N
  • 2,157
  • 3
  • 29
  • 78
  • Show `UyeBilgiler` and the Json Responce from the Server – Saran Sankaran Jul 23 '19 at 10:29
  • its basically parsing issue , something like 1 any variable you are expecting object type but getting string from response or vice versa – Neha Rathore Jul 23 '19 at 10:31
  • @Saran Sankaran I put UyeBilgiler class and the error coming from JSON Response. – S.N Jul 23 '19 at 10:32
  • Possible duplicate of ["Expected BEGIN\_OBJECT but was STRING at line 1 column 1"](https://stackoverflow.com/questions/28418662/expected-begin-object-but-was-string-at-line-1-column-1) – Ashish Jul 23 '19 at 10:33
  • @Ashish My json format starts with opening braces as you can see. – S.N Jul 23 '19 at 10:36

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
  • 152
  • 2
  • 11
  • I edit the post. I think the issue is coming from $_POST["uyeId"]. – S.N Jul 23 '19 at 10:57
  • hey bro use $_GET["uyeId"]; because your sending data from android in getMethod() and your receiving in $_POST[]; so just change it to $_GET[]; – MustafaShaikh Jul 23 '19 at 11:02