【问题标题】:android studio auto refresh data json url in thread every X second [duplicate]android studio每隔X秒自动刷新线程中的数据json url [重复]
【发布时间】:2020-01-30 02:46:32
【问题描述】:

我需要例如每 15 秒更新一次数据。什么是最正确的方法,我尝试了处理程序,但它不起作用,也许我做错了什么。

我该如何实现?

import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.sumb.AllTecTag.TecOneTag;
import com.example.sumb.R;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

public class TecOne extends Fragment {
    private TextView city_p_forward;
    private TextView city_t_forward;
    private TextView city_q_forward;
    private TextView city_w_forward;
    private TextView east_p_return;

    public TecOne() {}

    private void jsonTecOne(){
        final ObjectMapper mapper = new ObjectMapper();
        final Handler handler = new Handler();
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    final TecOneTag tecOneTag = mapper.readValue(new URL("https://some-address.json"),TecOneTag.class);
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            city_p_forward.setText(tecOneTag.getCity_P_T1());
                            city_t_forward.setText(tecOneTag.getCity_T_T1());
                            city_q_forward.setText(tecOneTag.getCity_Q_T1());
                            city_w_forward.setText(tecOneTag.getCity_W_T1());
                            east_p_return.setText(tecOneTag.getEast_P_T2());
                        }
                    });
                } catch (JsonParseException e) {
                    e.printStackTrace();
                } catch (JsonMappingException e) {
                    e.printStackTrace();
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View RootView = inflater.inflate(R.layout.fragment_tec_one, container, false);
        city_p_forward = RootView.findViewById(R.id.city_p_forward);
        city_t_forward = RootView.findViewById(R.id.city_t_forward);
        city_q_forward = RootView.findViewById(R.id.city_q_forward);
        city_w_forward = RootView.findViewById(R.id.city_w_forward);
        east_p_return = RootView.findViewById(R.id.east_p_return);
        jsonTecOne();
        return RootView;
    }
}

我是通过以下示例来实现的:https://www.codexpedia.com/java/jackson-parser-example-in-android/

【问题讨论】:

标签: java android json


【解决方案1】:

创建1个单独的方法来获取数据,

像这样的

private void getData(){
    //retrieve your data here
    //also update your data here in TextViews and all
}

现在您可以使用handler 每 15 秒更新一次数据,例如

final Handler handler = new Handler();

handler.post(r);

final Runnable r = new Runnable() {
    public void run() {
        //Do something after 15000ms or 15sec
        //call you getData() method here
        getData();

        //now call the runnable again after 15sec
        //you can also add some condition to stop this

        handler.postDelayed(this, 15000); // 15000 = 15 sec
    }
};

希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 2017-12-17
    • 2021-10-23
    • 1970-01-01
    • 2013-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多