我正在編寫一個程序,它從互聯網獲取貨幣價格,並將此代碼放在一個asynctask中。我需要能夠獲得持續的更新,但我不太確定循環的asynctask而不會搞亂程序。任何幫助感謝!Android中的AsyncTask循環
我需要這個代碼連續運行:
public void parseJSON(){
new AsyncTask<Void, Void, String>() {
protected String doInBackground(Void... params) {
try {
return downloadJSON(ticker);
} catch (Exception e) {
Log.d("JWP", e.toString());
}
return "Can't reach server. Is Internet access enabled?";
}
@Override
protected void onProgressUpdate(Void... values) {
TextView textView = (TextView) findViewById(R.id.ltcdata);
textView.setText("Retrieving Data...");
}
@Override
protected void onPostExecute(String result) {
TextView textView = (TextView) findViewById(R.id.ltcdata);
try {
JSONObject items = (JSONObject) new JSONTokener(result)
.nextValue();
String price = items.getJSONObject("ticker").getString(
"last");
String high = items.getJSONObject("ticker").getString(
"high");
String low = items.getJSONObject("ticker").getString("low");
textView.append("Last Price: $" + price + "\n");
textView.append("High: $" + high + "\n");
textView.append("Low: $" + low + "\n");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}.execute();
}
謝謝。這解決了我的問題。 – Omid1989