Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
android studio,每60秒自动刷新数据json url_Android_Json_Android Studio - Fatal编程技术网

android studio,每60秒自动刷新数据json url

android studio,每60秒自动刷新数据json url,android,json,android-studio,Android,Json,Android Studio,我是android studio的新手,我正在尝试让我的简单应用程序通过截击从URL获取JSON数据,一切都很好,但我想做自动刷新数据JSON。我正在尝试,但不起作用 我的代码,如果有人可以帮助我 package imo.meteoiraq; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuIt

我是android studio的新手,我正在尝试让我的简单应用程序通过截击从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";
        }
    }
}
包imo.meteoiraq;
导入android.support.v7.app.AppActivity;
导入android.os.Bundle;
导入android.view.Menu;
导入android.view.MenuItem;
导入android.widget.TextView;
导入android.widget.Toast;
导入com.android.volley.Request;
导入com.android.volley.RequestQueue;
导入com.android.volley.Response;
导入com.android.volley.VolleyError;
导入com.android.volley.toolbox.JsonObjectRequest;
导入com.android.volley.toolbox.volley;
导入java.text.DateFormat;
导入java.text.simpleDataFormat;
导入java.util.Date;
导入org.json.JSONException;
导入org.json.JSONObject;
导入java.security.Timestamp;
公共类MainActivity扩展了AppCompatActivity{
请求队列rq;
TextView timeDesc、tempDesc、windspeedDesc、windguestDesc、HumditDesc;
智力;
内部温度;
内部风速;
温德盖斯特酒店;
温润;
长时间更新;
字符串url=“/stationlookup?station=I1410&units=metric&v=2.0&format=json”;
创建时受保护的void(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=新的JsonObjectRequest(Request.Method.GET,url,null,new Response.Listener()){
@凌驾
公共void onResponse(JSONObject响应){
试一试{
JSONObject stationsJO=response.getJSONObject(“stations”);
JSONObject I1410JO=stationsJO.getJSONObject(“I1410”);
温度=I1410JO.getInt(“温度”);
风速=I1410JO.getInt(“风速”);
windguest=I1410JO.getInt(“风速”);
湿度=I1410JO.getInt(“湿度”);
timeupdate=I1410JO.getLong(“更新”)*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));
}捕获(JSONException e){
e、 printStackTrace();
}
}
},new Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
}
});
rq.add(jsonObjectRequest);
}
私有字符串getDate(长时间戳){
试一试{
DateFormat sdf=新的简化格式(“MM/dd/yyyy hh:MM:ss”);
日期netDate=(新日期(时间戳));
返回sdf.format(netDate);
}
捕获(例外情况除外){
返回“xx”;
}
}
}
您可以使用。例如 `

使用以下命令:

        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);

希望这有帮助

你想每60秒点击一次截击请求??是的,我指的是自动更新,无需从应用程序返回你指的是什么??你能解释一下吗clearly@sumit,我的意思是,当我进入应用程序时,我希望每60秒看到一次自动更新数据。无法解析已编辑的符号“handler”,您需要创建Handlerunknown“class”runnablehandler.postDelayed的实例(runnable,DELAY_TIME);用小写字母,否则它会认为它是静态类谢谢,不能解析符号“后置延迟”,以确保导入的是导入ANDROID.OS.HANDLER;android操作系统处理器代码运行良好,但没有更新?因此,请检查您是否在服务器上获得一些新数据??是的,每60秒更新一次,当我更改值秒时,我会看到一些延迟来显示数据
        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);