I am working on an app in which I am using REST APIs for data exchanges.My back-end scripts are in PHP, in one of my module I am sending as JSON response from back-end to Android client and fetching it at Android side using retrofit with @GET annotation.
Below is my JSON which I want to accept.
[
[
{
"column_name": "email"
},
{
"column_name": "product_code"
}
],
[
{
"column_value": "ht@t.com"
},
{
"column_value": "BBAN0003"
}
]
]
Below is my retrofit interface,
@GET("testing/getProductdetails.php")
Call<JSONArray> getColumnNames(@Query("email") String email);
The error I was getting is
expected begin object but was begin array
My PHP back-end code
<?php
/*
* Created by Belal Khan
* website: www.simplifiedcoding.net
* Retrieve Data From MySQL Database in Android
*/
//database constants
define('DB_HOST', 'localhost');
define('DB_USER', 'id6935081_pksinghhps');
define('DB_PASS', 'kingmessi10');
define('DB_NAME', 'id6935081_users');
//connecting to database and getting the connection object
$conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
//Checking if any error occured while connecting
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
die();
}
if(isset($_GET['email']))
{
$email=$_GET["email"];
$sql = "SHOW COLUMNS from product_details";
if (($result = $conn->query($sql)) == true) {
while ($row = $result->fetch_array()) {
$sub[] = $row[0];
}
}
$column_value = array();
$sql2 = "SELECT * FROM product_details WHERE email = '".$email."' ";
if (($result2 = $conn->query($sql2)) == true) {
while($row2 = $result2->fetch_array()){
for($j=0;$j<sizeof($sub);$j++){
$sub2 = array();
$sub2["column_value"] = $row2[$j];
array_push($column_value,$sub2);
}
}
}
else echo "NO";
$events = array();
$k=0;
while($k<sizeof($sub)){
$temp = array();
$temp['column_name']= $sub[$k];
array_push($events, $temp);
$k++;
}
$main = array();
array_push($main,$events);
array_push($main,$column_value);
echo json_encode($main);
}
?>
> where Response is your data model.
– Deˣ Feb 20 '19 at 09:11