0

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);


}
?>
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

2 Answers2

0

Just Change

Call<List<List<Map>>>
0

Entity to hold your actual data:

class JsonColumnNameHolder {
    @SerializedName("column_name")
    private final String myField;
    @SerializedName("column_value")
    private final String myField2;

    public JsonColumnNameHolder(String myField, String myField2) {
        this.myField = myField;
        this.myField2 = myField2;
    }

    public String getName() {
        return myField;
    }

    public String getValue() {
        return myField2;
    }
}

Retrofit signature:

@GET("testing/getProductdetails.php") 
Single<List<List<JsonColumnNameHolder>>> getColumnNames(@Query("email") String email);
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
ror
  • 3,295
  • 1
  • 19
  • 27
  • how to get data in my fragment class ?? – prashant kumar singh Feb 20 '19 at 09:18
  • `yourRetrofiService.getColumnNames(",,,").subscribe(...)` – ror Feb 20 '19 at 09:22
  • by using this i will not get all the values of my array , i think it will throw error again because the column_name and value are in array – prashant kumar singh Feb 20 '19 at 09:36
  • It's fairly broad topic, pls check here for example https://android.jlelse.eu/making-your-rxjava-intentions-clearer-with-single-and-completable-f064d98d53a8 (just first link I stumbled upon). I would definitely recommend going rx path as it is pretty standard way of doing async ops these days. You will have to add couple of dependencies to your gradle in order to use it, in particular this one https://github.com/square/retrofit/tree/master/retrofit-adapters/rxjava – ror Feb 20 '19 at 09:50