简体   繁体   English

我如何定期调用API?

[英]How can i call API periodically?

I am working in android. 我在android工作。 I want to draw a comment window. 我想画一个评论窗口。 in which I have a list View which shows comment retrieved from a API. 其中我有一个列表View,显示从API检索的注释。

I want to call that API after 30 second again and again so i can show recent comments in my list view. 我希望在30秒后再次调用该API,以便我可以在列表视图中显示最近的评论。

This is the code which is used to call my API. 这是用于调用我的API的代码。

   HttpClient hc = new DefaultHttpClient();

HttpGet get = new HttpGet("192.168.1.127/CC/comment"); HttpGet get = new HttpGet(“192.168.1.127/CC/comment”);

HttpResponse rp = hc.execute(get); HttpResponse rp = hc.execute(get);

if (rp.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { if(rp.getStatusLine()。getStatusCode()== HttpStatus.SC_OK){

 Then put values in some text boxes.

} }

I want to call this above code again and again after 10 second. 我想在10秒后反复调用上面的代码。 Please help me in writing code. 请帮我写代码。 Your may give a short example to solve this type of problem. 您可以举一个简短的例子来解决这类问题。 I have very short time and dont have time to search on google so please help how can i call API and change UI periodically ? 我的时间很短,没有时间在谷歌上搜索所以请帮助我如何调用API并定期更改UI?

I want to call that API after 30 second again and again

You Should use Service with Broadcast receiver . 您应该使用服务广播接收器

Service will do your Task in Background but will not provide any UI so you need to register with Broadcast receiver. 服务将在后台执行您的任务,但不提供任何UI,因此您需要注册广播接收器。

Check this Sample , this sample show how to repeat a task and update UI using Service and Broadcast receiver. 检查此示例 ,此示例显示如何使用服务和广播接收器重复任务和更新UI。

Also refer this tutorial for further reference. 请参阅本教程以获取进一步参考。

You can simple use just following code: 您只需使用以下代码即可:

// We need to use this Handler package
import android.os.Handler;

// Create the Handler object (on the main thread by default)
Handler handler = new Handler();
// Define the code block to be executed
private Runnable runnableCode = new Runnable() {
    @Override
    public void run() {
      // Do something here on the main thread
      Log.d("Handlers", "Called on main thread");
      // Repeat this the same runnable code block again another 2 seconds
      // 'this' is referencing the Runnable object
      handler.postDelayed(this, 2000);
    }
};
// Start the initial runnable task by posting through the handler
handler.post(runnableCode);

You can remove the scheduled execution of a runnable with: 您可以使用以下命令删除runnable的计划执行:

// Removes pending code execution //删除挂起的代码执行

handler.removeCallbacks(runnableCode);

I have solved my problem like this:- 我已经解决了这样的问题: -

public class ClassName extends Activity 公共类ClassName扩展了Activity

{ {

            @Override

    public void onCreate(Bundle savedInstance)

    {
        super.onCreate(savedInstance);


        setContentView(R.layout.live_stream_layout);

        handler.post(timedTask);

     private Runnable timedTask = new Runnable(){

      @Override

      public void run() 

               {

            //do your work here for calling API and UI


            handler.postDelayed(timedTask ,2000 );

      }};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM