如何将检索到的数据(从sql数据库)存储为Android中的数组?

时间:2016-06-27 15:59:13

标签: php android mysql android-studio

我正从数据库表“topicnames”中检索数据(所有topic_name)。 这是它的PHP脚本

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

?>

这是我得到的结果

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

问题:但现在我想将这些数据发送到Android,并在我的Android Activity中存储为数组“array_topic_name”.Below是我的PostAsync类

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

        }


    }

这是我的LOGCAT,显示我正在接收数据

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"}]}

0 个答案:

没有答案