开发者

How to get the data from remote database to android quickly?

开发者 https://www.devze.com 2023-04-12 16:22 出处:网络
I am displaying the student details from remote MSSQL database into android using php scripts. The data is displaying fine but it is taking more time to display them i.e until the data is displayed it

I am displaying the student details from remote MSSQL database into android using php scripts. The data is displaying fine but it is taking more time to display them i.e until the data is displayed it is showing black screen. How to get the data quickly and where should I make 开发者_JS百科the changes to avoid the black screen?

Thanks in advance.


You should pull the Data Asynchronously. Refer to AsyncTask. If the remote call is so slow you can also think about some caching.

new Thread(new Runnable() {

        @Override
        public void run() {
            //loaddata
        }
    }).start();

keep in mind that when you want to access the UI Thread you have to use the runOnUiThread method


In Android every App can have a Caching Directory. Its a good way to cache your Data and display this data for the time the fresh data is loading in the Background.

With your Datastructure Serializable you be able to write down your Data into this Caching Directory and speed up your Loading.

Here some Snippeds for Caching data:

private void loadData() {
    ObjectInputStream in;
    try {
        FileInputStream fis = new FileInputStream(new File(getCacheDir(),
                "cache.dat"));
        in = new ObjectInputStream(fis);
        data = (Data) in.readObject();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
}

private void saveData() {
    if (data.size() > 0) {
        File file = new File(getCacheDir(), "cache.dat");

        ObjectOutputStream out;
        try {
            file.createNewFile();
            out = new ObjectOutputStream(new FileOutputStream(file));
            out.writeObject(data);
            out.close();
        } catch (IOException e) {
        }
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号