【问题标题】:android studio , auto refresh data json url every 60 second [duplicate]android studio,每60秒自动刷新数据json url [重复]
【发布时间】:2017-12-17 17:58:25
【问题描述】:

我是 android studio 的新手,我正在尝试让我的简单应用程序使用 Volley 从 URL 获取 JSON 数据,一切都很好,但我想做自动刷新数据 json。我尝试做但不工作。

如果有人可以帮助我的代码

package imo.meteoiraq;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.json.JSONException;
import org.json.JSONObject;

import java.security.Timestamp;

public class MainActivity extends AppCompatActivity {
RequestQueue rq;
TextView timeDesc,tempDesc,windspeedDesc,windguestDesc,humdityDesc;
    int ages;
    int temp;
    int windspeed;
    int windguest;
    int humdity;
    long timeupdate;

    String url="/stationlookup?station=I1410&units=metric&v=2.0&format=json";
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        rq= Volley.newRequestQueue(this);

        timeDesc= (TextView) findViewById(R.id.timeupdateDesc);
        tempDesc= (TextView) findViewById(R.id.tempid);
        windspeedDesc= (TextView) findViewById(R.id.windid);
        windguestDesc= (TextView) findViewById(R.id.windgustid);
        humdityDesc= (TextView) findViewById(R.id.humdid);

        sendjsonrequest();
    }
public void  sendjsonrequest(){
    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            try {

                JSONObject stationsJO = response.getJSONObject("stations");
                JSONObject I1410JO = stationsJO.getJSONObject("I1410");
                 temp = I1410JO.getInt("temperature");
                 windspeed = I1410JO.getInt("wind_speed");
                 windguest = I1410JO.getInt("wind_gust_speed");
                 humdity = I1410JO.getInt("humidity");
                timeupdate = I1410JO.getLong("updated")* 1000L;

                 tempDesc.setText(Integer.toString(temp));
                 windspeedDesc.setText(Integer.toString(windspeed));
                 windguestDesc.setText(Integer.toString(windguest));
                 humdityDesc.setText(Integer.toString(humdity));
                 timeDesc.setText(getDate(timeupdate));
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

        }
    });
    rq.add(jsonObjectRequest);
}

    private String getDate(long  timeStamp){
        try{
            DateFormat sdf = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss");
            Date netDate = (new Date(timeStamp));
            return sdf.format(netDate);
        }
        catch(Exception ex){
            return "xx";
        }
    }
}

【问题讨论】:

  • 你想每 60 秒打一次齐射请求吗??
  • 是的,我的意思是自动更新,没有从应用程序和返回 agin
  • 没有应用程序又返回是什么意思?能解释清楚吗
  • @sumit ,我的意思是当我进入应用程序时,我希望每 60 秒看到一次自动更新数据。

标签: android json android-studio


【解决方案1】:

您可以使用Handler 来执行此操作。例如 `

Handler handler = new Handler();
    Runnable runnable = new Runnable() {
         @Override
         public void run() {
             // polling code 
             handler.postDelayed(this, SIXTY_SECONDS);
         }
    };Handler.postDelayed(runnable, DELAY_TIME) ;`

【讨论】:

  • 无法解析符号“处理程序”
  • 已编辑,需要创建Handler实例
  • 未知的“类”可运行
  • handler.postDelayed(runnable, DELAY_TIME);带小写字母,否则将其视为静态类
【解决方案2】:

使用这个:

        final Handler handler = new Handler();
        final Runnable runnable = new Runnable() {
            @Override
            public void run() {
                sendjsonrequest();
                handler.postDelayed(this,6000);//60 second delay
            }
        };handler.postDelayed(runnable,Initial_Delay_time);

希望对你有帮助!!!

【讨论】:

  • 谢谢,无法解析符号'postDelayed'
  • 确保你正在导入 import android.os.Handler;安卓操作系统处理程序
  • 代码工作正常但没有更新!! ?
  • 那么请检查您是否在服务器上获取了一些新数据??
  • 是的,每 60 秒更新一次,当我更改值 sec 时,我看到显示数据有一些延迟
猜你喜欢
  • 1970-01-01
  • 2021-10-23
  • 2019-08-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多