代码之家  ›  专栏  ›  技术社区  ›  snowman

每隔一段时间通过截击更新内容

  •  0
  • snowman  · 技术社区  · 7 年前

    我知道有很多这样的帖子,但由于Android经验有限,我不太清楚该相信谁。我有一个应用程序,在启动时通过截击请求从我的服务器加载内容。一段时间后,我想发出截击请求,以更新显示给用户的内容。当应用程序首次加载时,我确定从半小时到以下时间的秒数

    public static void refreshAllContent(final long timetoupdate) {
           new CountDownTimer(timetoupdate, 1000) {
               public void onTick(long millisUntilFinished) {
               }
               public void onFinish() {
                 Log.i("SCROLLS ", "UPDATE CONTENT HERE ");
                   resetContent();
               }
           }.start();
               }
    

    在完成时,它调用refreshAllContent,在这里我会发出截击请求并重置下一次更新的计数,我有

    public static void resetContent(){
            Handler handler= new Handler();
            Runnable runnable= new Runnable() {
                @Override
                public void run() {
    //PUT VOLLEY REQUEST HERE
                    refreshAllContent(Times. initialTime());
                }
            };
        }
    

    我想我被困在了如何发出截击请求的问题上,这意味着我需要担心什么才能做到这一点。就像我说的,没有太多经验,所以不确定是否在特殊的runnable或task中运行请求。欢迎任何指示。

    编辑:我对这个做了一些修改,没有返回到refreshAllContent,而是用

    private static void resetContent(){
            Log.i("SCROLLS ", "ENTER resetContent");
             final Handler handler= new Handler();
             Runnable  runnableCode = new Runnable() {
                @Override
                public void run() {
                   refreshData(); // Volley Request
                   handler.postDelayed(runnableCode, 20000);
                }
            };
            handler.post(runnableCode);
       }
    

    逻辑现在处于初始运行状态,第一次创建更新并传递给refreshAllContent。倒计时完成后,resetContent()将运行,从而在refreshData()中发出截击请求。现在,我遇到一个错误,指出runnableCode需要声明为final,因为它是从内部类访问的,处理程序也是如此。好吧,将final添加到

    final Runnable runnableCode=new Runnable(){
    

    行没有修复错误,我仍然有一个错误告诉我runnableCode尚未初始化。我能在这方面得到点帮助吗。

    1 回复  |  直到 7 年前
        1
  •  2
  •   will    7 年前

    您没有创建可运行的卷运行。默认情况下,Volly在后台线程上运行网络调用。

    下面是一个简单的Volly代码:

    public void volleyProcess(){
        RequestQueue requestQueue = Volley.newRequestQueue(this);
            StringRequest request = new StringRequest(Request.Method.GET, "https://api.myjson.com/bins/753rt", new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Log.d(TAG, response);
                refreshAllContent(30000);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.d(TAG, error.toString());
            }
        });
        requestQueue.add(request);
    }
    

    当调用成功返回响应时,将调用onResponse()。此方法在主线程上调用,因此可以在此处运行refreshAllContent()方法。参数“response”是返回的数据,在这里可以随意使用它(我只是将其打印到Logcat)。

    现在,要使此代码在所需的时间间隔之后运行,只需在countdownTimer的onFinish()中调用它。

    public static void refreshAllContent(final long timetoupdate) {
       new CountDownTimer(timetoupdate, 1000) {
           public void onTick(long millisUntilFinished) {
           }
           public void onFinish() {
             Log.i("SCROLLS ", "UPDATE CONTENT HERE ");
               volleyProcess();
           }
       }.start();
    }
    

    希望这有帮助