0

I am retrieving the data (all topic_name) from a database table "topicnames". Here is the PHP script for it

<?php

   header('Content-type=application/json;charset=utf-8');

   include("connection.php");
   session_start();

   $query = ("SELECT DISTINCT(topic_name) FROM `topic_names` ");
   $response=mysqli_query($con,$query);


// set array
$array = array();

// look through query
while($row = mysqli_fetch_assoc($response))
{

  // add each row returned into an array
  $array[] = $row;
}

$data['details'] = $array;
echo json_encode($data);

mysqli_close($con);

?>

This is the result I am getting

{"details":[{"topic_name":"abc"},{"topic_name":"xyz"},{"topic_name":"bbb"},{"topic_name":"ccc"},{"topic_name":"1a"},{"topic_name":"2b"},{"topic_name":"3c"}]}

PROBLEM:But now I want this data to be send to Android,and be stored as an array "array_topic_name" in my Android Activity.Below is my PostAsync class

class PostAsync extends AsyncTask<String, String, JSONObject> {

        JSONParser jsonParser = new JSONParser();

        private ProgressDialog pDialog;

        private static final String LOGIN_URL = "http://10.0.3.2/topic_array_populate.php";


        private static final String TAG_SUCCESS = "success";
        private static final String TAG_MESSAGE = "message";


        @Override
        protected void onPreExecute() {
            Log.d("preexec", "starting");

        }

        @Override
        protected JSONObject doInBackground(String... args) {

            try {

                HashMap<String, String> params = new HashMap<>();

                JSONObject json = jsonParser.makeHttpRequest(
                        LOGIN_URL, "POST", params);


                if (json != null) {
                    Log.d("JSON result", json.toString());
                    return json;
                }



            } catch (Exception e) {
                e.printStackTrace();
            }

            return null;
        }

        protected void onPostExecute(JSONObject json) {

            int success = 0;
            String message = "";

            Log.d("post exec", "starting");

            Log.d("json", json.toString());

            if (pDialog != null && pDialog.isShowing()) {
                pDialog.dismiss();
            }

            if (json != null) {
                {
                    Log.d("json", "not null");

                    JSONArray data = null;
                    try {
                        data = json.getJSONArray("details");
                        JSONObject obj = data.getJSONObject(0);

                        Log.d("JSON result",json.toString());


                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }

                try {
                    success = json.getInt(TAG_SUCCESS);
                    message = json.getString(TAG_MESSAGE);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

        }


    }

This is my LOGCAT showing that I am receiving the data correctly

06-27 11:55:01.549 1921-1921/com.example.somya.client_feedback_application D/post exec: starting
06-27 11:55:01.550 1921-1921/com.example.somya.client_feedback_application D/json: {"details":[{"topic_name":"abc"},{"topic_name":"xyz"},{"topic_name":"bbb"},{"topic_name":"ccc"},{"topic_name":"1a"},{"topic_name":"2b"},{"topic_name":"3c"}]}
06-27 11:55:01.550 1921-1921/com.example.somya.client_feedback_application D/json: not null
06-27 11:55:01.552 1921-1921/com.example.somya.client_feedback_application D/JSON result: {"details":[{"topic_name":"abc"},{"topic_name":"xyz"},{"topic_name":"bbb"},{"topic_name":"ccc"},{"topic_name":"1a"},{"topic_name":"2b"},{"topic_name":"3c"}]}
Dharman
  • 30,962
  • 25
  • 85
  • 135
Somya Arora
  • 87
  • 11

0 Answers0