doucong3048 2017-01-21 15:14
浏览 206

快速将数据从mysql加载到recyclerview

I have an android app that's load data from php to recyclerview and show it on cards , I load more data every time the scroller gets to some point , the problem is that it still load the data pretty slowly and I don't know how to make it load faster.

Here is the BackgroundTask that load the data

 public class BackgroundTask extends AsyncTask<Void, Void, String> {
        String json_url;


        protected void onPreExecute() {
            json_url = "http://website.com/file.php";
        }

        @Override
        protected String doInBackground(Void... params) {

            try {
                String urlLimit = URLEncoder.encode("limit", "UTF-8") + "=" + URLEncoder.encode(limit+"", "UTF-8") + "&" +
                        URLEncoder.encode("offset", "UTF-8") + "=" + URLEncoder.encode(offest+"", "UTF-8");

                String   data = urlLimit ;


                URL url = new URL(json_url);
                HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                httpURLConnection.setRequestMethod("POST");
                httpURLConnection.setDoOutput(true);
                // if(AgeOn == true) {
                OutputStream outputStream = httpURLConnection.getOutputStream();
                BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
                bufferedWriter.write(data);
                bufferedWriter.flush();
                //    }
                InputStream inputStream = httpURLConnection.getInputStream();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                StringBuilder stringBuilder = new StringBuilder();


                while ((JSON_STRING = bufferedReader.readLine()) != null) {

                    stringBuilder.append(JSON_STRING + "
");

                }

                bufferedReader.close();
                inputStream.close();
                httpURLConnection.disconnect();
                return stringBuilder.toString().trim();
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return "";
        }





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

    @Override
    protected void onPostExecute(String result) {
            Log.d("results:", result + "");
            items = result.split(",:,");
            id = new int[items.length / 10];
            name = new String[items.length / 10];
            lastname = new String[items.length / 10];
            date = new String[items.length / 10];


            List<ListItem> data = new ArrayList<>();

            int finish = 0;
            int counter = 0;

            while (finish < items.length) {
                id[counter] = Integer.parseInt(items[finish]);
                name[counter] = items[finish + 1];
                lastname[counter] = items[finish + 2];
                date[counter] = items[finish + 3];

                finish = finish + 4;
                counter++;

            }
        }
            SetViews();

    }

}

after it loads it it just call for a method that create the items of the recycler view and set the list.

And here is the php file that load the data

  <?php
header('Content-Type: text/html; charset=utf-8');
    $db = new PDO('mysql:host**********=;dbname=*******;charset=utf8mb4',******** , '********');
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);


$limit =$_POST["limit"];
$offset =$_POST["offset"];



$stmt = $db->query("SELECT * FROM appdb LIMIT $limit OFFSET $offset");

$results = $stmt->fetchAll(PDO::FETCH_ASSOC);


$out = '';
if (is_array($results) && count($results) > 0) 
{
  foreach ($results as $sqlRow) 
  {
    $out .= $sqlRow['id'].",:,".$sqlRow['name'].",:,".$sqlRow['lastname'].",:,".$sqlRow['date'].",:,";
  }

  $out = substr($out, 0, -3);
}
echo $out;


?>      

How can I load the data faster?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 opencv图像处理,需要四个处理结果图
    • ¥15 无线移动边缘计算系统中的系统模型
    • ¥15 深度学习中的画图问题
    • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条
    • ¥15 Python报错怎么解决
    • ¥15 simulink如何调用DLL文件
    • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
    • ¥30 线性代数的问题,我真的忘了线代的知识了
    • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统
    • ¥188 需要修改一个工具,懂得汇编的人来。