如果无法获取数据,则在10秒后显示重试和停止进度条

时间:2018-09-12 10:13:14

标签: android internet-connection

我正在使用Volley从API提取数据,并在回收站视图中显示,并显示进度条,直到未加载数据为止,现在的问题是有时需要花费很多时间来获取数据,并且也不断显示进度条。 我想要的是,如果要花10秒钟以上的时间,则进度条应停止并显示带有另一个布局上的“重试”按钮的检查Internet连接。

这是我的代码。

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_tab_one, container, false);
    SharedPreferences sharedPref = getActivity().getSharedPreferences("country", Context.MODE_PRIVATE);
    String country = sharedPref.getString("selectedCountry", "au");
    progressBar = (ProgressBar) view.findViewById(R.id.pbFragOne);
    listRecyclerView = (RecyclerView) view.findViewById(R.id.headlines_recycler);
    RecyclerView.LayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
    listRecyclerView.setLayoutManager(linearLayoutManager);
    listRecyclerView.setItemAnimator(new DefaultItemAnimator());
    listRecyclerView.setHasFixedSize(true);
    latestNewsResponse();
    return view;
}
public void latestNewsResponse() {
    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            String str_response = response.toString();
            Gson gson = new Gson();
            News news = gson.fromJson(str_response, News.class);
            news_list = news.getArticles();
            if (!news.getStatus().contentEquals("ok")) {
                Toast.makeText(getContext(), "Check your connection", Toast.LENGTH_SHORT).show();
            }
            newsListAdapter = new NewsListAdapter(getContext(), news_list);
            progressBar.setVisibility(View.GONE);
            listRecyclerView.setAdapter(newsListAdapter);

        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
        }
    });
    RequestQueue requestQueue = Volley.newRequestQueue(getContext());
    requestQueue.add(jsonObjectRequest);
}

2 个答案:

答案 0 :(得分:1)

如果使用Observable,只需创建Observable.interval填充间隔和时间单位作为参数。

答案 1 :(得分:0)

您可以使用HandlerThread类:https://developer.android.com/reference/android/os/HandlerThread

public void start10SecondsTimeout() {
        final HandlerThread handlerThread = new HandlerThread("MyHandlerThread");
        handlerThread.start();
        Looper looper = handlerThread.getLooper();
        final Handler handler = new Handler(looper);
        final Date initialTime = new Date();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                Date currentTime = new Date();
                long elapsed = currentTime.getTime() - initialTime.getTime();
                if (elapsed > 10000) {
                    // update ui here
                    handler.removeCallbacks(this);
                }
                handler.postDelayed(this, 1000);
            }
        }, 1000);
    }

此代码每秒运行一次,您可以选择自己安排HandlerThread的频率。