【问题标题】:Repeat Volley Request Every x-seconds Java每隔 x 秒重复一次 Volley 请求 Java
【发布时间】:2021-04-22 01:05:25
【问题描述】:

好的 - 在你说这是重复之前,我已经查看了我能找到的每篇堆栈溢出文章,但它们都没有工作和/或正确/简单地回答问题。我所需要的只是每隔 x 秒重复一个带有凌空请求的函数。

基本上,我在一个函数中有一个相当简单的 Volley 请求,它在一次调用时绝对完美。

截击功能:

  private void SetBusPositions() {
        TextView textE = (TextView) findViewById(R.id.FirstInfo);
        RequestQueue Queue = ServerRequestsQueue.getInstance(context.getApplicationContext()).getRequestQueue();
        int SendMethod = Request.Method.GET;
        String ServerURL = "my url";
        JsonArrayRequest JOR = new JsonArrayRequest(SendMethod, ServerURL, null, new Listener<JSONArray>() {
            @Override
            public void onResponse(JSONArray resp) {
                textE.setText(resp.toString());
                System.out.println("Response is: " + resp.toString());
                //for each object in JSON Array
                for (int i = 0; i < resp.length(); i++) {
                }
            }
        }, new Response.ErrorListener() {
            public void onErrorResponse(VolleyError error) {
             //process
            }
        });
        Queue.add(JOR);
    }

我只想定期调用这个函数,从服务器接收数据并更新我的巴士位置数据。必须有一个相当简单的方法来做到这一点?我知道我一定遗漏了什么,但其他答案似乎都没有帮助。

另外,由于我使用的是 Google 地图,我的班级已经在扩展 FragmentActivity。我已经看到扩展 Runnable 以使其正常工作的方法——但我的 Java 在这里有点生疏。我做的JS太多了。

【问题讨论】:

    标签: java android-studio request android-volley


    【解决方案1】:
    private final class RepeatedOperation extends AsyncTask<Map, Void, String> {
    
        @Override
        protected String doInBackground(Map... params) {
        
            SetBusPosition(params[0]);
    
        }
    
        @Override
        protected void onPostExecute(String result) {
        }
    }
    
    
    private void callAsynchronousTask() {
    final Handler handler = new Handler();
    Timer timer = new Timer();
    TimerTask doAsynchronousTask = new TimerTask() {       
        @Override
        public void run() {
            handler.post(new Runnable() {
                public void run() {       
                    try {
                        RepeatedOperation performBackgroundTask = new RepeatedOperation();
                        // RepeatedOperation this class is the class that extends AsyncTask 
                        performBackgroundTask.execute(map);
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                    }
                }
            });
        }
    };
    timer.schedule(doAsynchronousTask, 0, 50000); //execute in every 50000 ms
    }
    

    试试这个并将它添加到与 SetBusPosition() 相同的范围内

    【讨论】:

    • @ACarWar 我更改了代码,看到我使用 Map 通用参数声明了该类,因此在调用 execute() 函数时访问它。
    • 太棒了!我通过为 RepeatedOperations 创建一个构造函数来使其工作,该构造函数需要所需的变量来使 SetBusPosition() 工作 - 即总线、地图等。
    • 是的,我在看到您添加的评论之前就这样做了。感谢您的帮助。
    猜你喜欢
    • 2020-02-02
    • 1970-01-01
    • 2010-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多