2016-11-08 83 views
0

我試過一個計時器任務,但沒有幫助
我試圖讓代碼不斷獲取和更新座標。 我v'e完成研究,但沒有適用於我正在使用。 我會把onPostExecute放在計時器內嗎?如何使用JSON每兩秒更新文本視圖?

我正在使用json OBJECT。

public class IsstatusActivity extends AppCompatActivity { 

    JSONParser jsonparser = new JSONParser(); 
    TextView latitude; 
    TextView longitude; 
    String lat; 
    String longit; 
    JSONObject jobj = null; 

    private final ReentrantLock lock = new ReentrantLock(); 
    private final Condition tryAgain = lock.newCondition(); 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.isstatus); 
     latitude = (TextView) findViewById(R.id.Coordinates); 
     longitude = (TextView) findViewById(R.id.longitude); 


     new retrievedata().execute(); 
    } 

    class retrievedata extends AsyncTask<String, String, String> { 


     @Override 
     public String doInBackground(String... args) { 

      jobj = jsonparser.makeHttpRequest("http://api.wheretheiss.at/v1/satellites/25544"); 


        try { 
         lat = "latitude : " + jobj.getString("latitude"); 
         longit = "longitude : " + jobj.getString("longitude"); 
        } catch (JSONException e) { 
         // TODO Auto-generated catch block 


         e.printStackTrace(); 
        } 



      return lat; 
     } 

     protected void onPostExecute(String lat) { 


      latitude.setText(lat); 
      longitude.setText(longit); 

     } 




    } 
} 

回答

0

我在過去的幾天裏看到您的問題,您正在努力解決這個問題,所以我會盡力幫助您。

下面是定時器部分的一種方法:AsyncTask有一種方法可以報告進度。當您在doInBackground內調用publishProgress時,將導致在UI線程上調用onProgressUpdate。您可以使用這些方法來進行更新。

// by convention class names begin with an uppercase letter in Java 
class RetrieveData extends AsyncTask<Void, String, Void> { 

    @Override 
    protected Void doInBackground(Void... params) { 

     try { 

      while (! isCancelled()) { 

       jobj = jsonparser.makeHttpRequest("http://api.wheretheiss.at/v1/satellites/25544"); 

       String lat = "latitude : " + jobj.getString("latitude"); 
       String long = "longitude : " + jobj.getString("longitude"); 

       // this will cause onProgressUpdate to be called with lat & long 
       publishProgress(lat, long); 

       // it's okay to sleep within the background thread 
       Thread.sleep(2000); 
      } 

     } catch (InterruptedException e) { 
      Log.w("RetrieveData", "thread was interrupted", e); 
     } catch (JSONException e) { 
      Log.e("RetrieveData", "parse error", e); 
     } 

     return null; 
    } 

    @Override 
    protected void onProgressUpdate(String... values) { 
     latitude.setText(values[0]); 
     longitude.setText(values[1]); 
    } 

} 

而且,最好的結果我想你應該有任務的引用,並在您的活動與和onResume像這樣進行管理:

private AsyncTask<Void, String, Void> retrieveData; 

    @Override 
    protected void onResume() { 
     super.onResume(); 
     retrieveData = new RetrieveData(); 
     retrieveData.execute(); 
    } 

    @Override 
    protected void onPause() { 
     retrieveData.cancel(true); 
     retrieveData = null; 
     super.onPause(); 
    } 

這意味着你不需要在onCreate中調用任務execute

這可能不是最好的方法,但它可能是最簡單的方法,讓你開始—,直到其他人給你更好的答案。