我是Android的新手,我正在开发一个应用程序,从bitrex获取当前速率并将其转换为印度卢比,但问题是我想在每一秒刷新速率,但我的处理程序只运行一次并且没有更改即使费率在bitrex API中发生变化,它在第一次之后的值
这是我的活动
public class Test extends AppCompatActivity {
double last;
double inr;
double x;
TextView tv, tt, ui,uer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
uer=(TextView)findViewById(R.id.usern);
ui = (TextView) findViewById(R.id.uid);
tv = (TextView) findViewById(R.id.name);
tt = (TextView) findViewById(R.id.email);
final RequestQueue queue = Volley.newRequestQueue(this);
final RequestQueue queuee = Volley.newRequestQueue(this);
String url = "https://bittrex.com/api/v1.1/public/getticker?market=usdt-btc";
String urll="http://api.fixer.io/latest?base=USD";
final StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
JSONObject json = null;
try {
json = new JSONObject(response);
} catch (JSONException e) {
e.printStackTrace();
}
try {
JSONObject result = json.getJSONObject("result");
last = result.getDouble("Last");
tv.setText(String.valueOf(last));
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
final StringRequest stringRequestmoney = new StringRequest(Request.Method.GET, urll,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
JSONObject jjson = null;
try {
jjson = new JSONObject(response);
} catch (JSONException e) {
e.printStackTrace();
}
try {
JSONObject money = jjson.getJSONObject("rates");
inr = money.getDouble("INR");
tt.setText(String.valueOf(inr));
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
// queue.add(stringRequest);
// queuee.add(stringRequestmoney);
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
//do something
queue.add(stringRequest);
queuee.add(stringRequestmoney);
x = (last * inr);
ui.setText(""+x);
handler.postDelayed(this, 5000);
}
}, 5000);
}
答案 0 :(得分:0)
我不知道如果这是你的问题的根源,但试图抓住你的处理程序中的任何错误
handler.postDelayed(new Runnable() {
public void run() {
try{
//do something
queue.add(stringRequest);
queuee.add(stringRequestmoney);
x = (last * inr);
ui.setText(""+x);
handler.postDelayed(this, 5000);
}
finally {
// 100% guarantee that this always happens, even if
// your update method throws an exception
handler.postDelayed(this, 5000);
}
}
}, 5000);