应用程序因sqllite查询而挂起

时间:2019-05-23 21:53:30

标签: android sqlite

我制作了一个Android应用程序,该应用程序获取了一些信息并保存在电话中的sqllite数据库中并发送到服务器,但该应用程序的代码挂起了

private void obtenerLista() {
    cadenaMandar=new String();
    String updatestr = new String();

    for (int i=0; i<listaDatos.size();i++){
        cadenaMandar=listaDatos.get(i).getCodEmpleado()+"/"+listaDatos.get(i).getLatitud()+"/"+listaDatos.get(i).getLongitud()+"/"+listaDatos.get(i).getFecha()+"/"+listaDatos.get(i).getHora()+"/"+listaDatos.get(i).getSubido();

        //mandar al servidor
        ClassConnection connection=new ClassConnection();
        try {
            cadenaMandar=cadenaMandar.replace(" ", "");
            String response=connection.execute("http://www.urlserver.com.ar/crvp.php?data="+cadenaMandar.toString()).get();
            if (response!=null){
                Toast.makeText(getApplicationContext(),"Mandar a servidor OK",Toast.LENGTH_LONG).show();
                actualizarUsuariosSql(listaDatos.get(i).getIdDatos());
            }
        } catch (ExecutionException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }


    }

}

我在论坛上读到我必须使用Asynctask,但是我仍然不明白如何在此代码中执行此操作。 有人可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

制作一个扩展AsyncTask的类,它将完成您的冗长任务。

public class AsyncClass extends AsyncTask<Void, Void, String> {
    String cadenaMandar = null;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        // Make your fields as variable and initialize them here.
        cadenaMandar = new String();

    }

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


        // Do all your lengthy tasks that you think can take time. Do that in  doInBackground method.

        for (int i = 0; i < listaDatos.size(); i++) {
            cadenaMandar = listaDatos.get(i).getCodEmpleado() + "/" + listaDatos.get(i).getLatitud() + "/" + listaDatos.get(i).getLongitud() + "/" + listaDatos.get(i).getFecha() + "/" + listaDatos.get(i).getHora() + "/" + listaDatos.get(i).getSubido();
            String response = null;

            //mandar al servidor
            ClassConnection connection = new ClassConnection();
            try {
                cadenaMandar = cadenaMandar.replace(" ", "");
                response = connection.execute("http://www.urlserver.com.ar/crvp.php?data=" + cadenaMandar.toString()).get();
                /*if (response!=null){
                    Toast.makeText(getApplicationContext(),"Mandar a servidor OK", Toast.LENGTH_LONG).show();
                    actualizarUsuariosSql(listaDatos.get(i).getIdDatos());
                }*/
            } catch (ExecutionException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return response;
        }

        @Override
        protected void onPostExecute (String s){ // That "s" in the argument is the "response" that you are returning from doInBackground
        super.onPostExecute(s);

            // Do all the things that are interacting with your UI here in this method.
            // Because you want to call this method actualizarUsuariosSql() before everything happens. Therefore put it there.
            if (s != null) {
                Toast.makeText(getApplicationContext(), "Mandar a servidor OK", Toast.LENGTH_LONG).show();
                actualizarUsuariosSql(listaDatos.get(i).getIdDatos());
            }
        }
    }

}