【问题标题】:How to insert data retrieved from MySQL into array in Android Application?如何将从 MySQL 检索到的数据插入 Android 应用程序的数组中?
【发布时间】:2016-11-30 23:23:21
【问题描述】:

我正在尝试从我的数据库中检索数据,我编写了这段代码

<?php
require "conn.php";

$mysql_qry = "select * from domande"; 
$res = mysqli_query($conn,$mysql_qry);
$result = array();

while($row = mysqli_fetch_array($res)){
array_push($result,
array('id_domanda'=>$row[0],
'domanda'=>$row[1],
'username'=>$row[2]
));
}

echo json_encode(array("result"=>$result));

mysqli_close($conn);

?>

它应该返回一个数组。我已经能够以这种方式在警报对话框中显示数组:

class BackgroundWorkerDomande extends AsyncTask<String,Void,String> {

        Context context;
        AlertDialog alertDialog;
        BackgroundWorkerDomande (Context ctx){
            context = ctx;
        }
        @Override
        protected String doInBackground(String... params) {
            String type = params[0];


            if (type=="getquestion"){
                try {


                    URL url = new URL(params[3]);
                    HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                    httpURLConnection.setRequestMethod("POST");
                    httpURLConnection.setDoOutput(true);
                    httpURLConnection.setDoInput(true);


                    InputStream inputStream = httpURLConnection.getInputStream();
                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
                    String result = "";
                    String line = "";
                    while ((line = bufferedReader.readLine()) != null) {
                        result += line;
                    }
                    bufferedReader.close();
                    inputStream.close();
                    httpURLConnection.disconnect();
                    return result;
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }



            return null;
        }

        @Override
        protected void onPreExecute() {


            alertDialog = new AlertDialog.Builder(context).create();
            alertDialog.setTitle("Status");
        }

        @Override
        protected void onPostExecute(String result) {
            alertDialog.setMessage(result);
            alertDialog.show();



        }

        @Override
        protected void onProgressUpdate(Void... values) {
            super.onProgressUpdate(values);
        }
    }

但是如果我想将结果插入到 MainActivity 中声明的新数组中,我该怎么办?我只是按照互联网上的一些教程来编写这段代码,所以我并不完全知道它是如何工作的。 (对不起我的英语不好)

【问题讨论】:

    标签: php android mysql


    【解决方案1】:

    由于您发布了工作代码但没有指定确切的问题,因此很难确切地知道您想要什么。特别是,“将结果插入新数组”是什么意思?从表面上看,我看到了这一点:

    ArrayList<String> results;
    results.add (result);
    

    但是,我会提出一个建议:在您的 php 代码中,您可以将其分解为字段并将它们作为数组发布,而不是将整个结果作为一个字符串发布吗?然后,在接收端,如果您想处理单个字段值,则不必从 result 解析它们。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-29
      • 1970-01-01
      • 1970-01-01
      • 2016-05-07
      • 2015-08-08
      • 1970-01-01
      相关资源
      最近更新 更多