Я хочу знать, что такое Android-эквивалент для setInterval javascript для класса. Я пытаюсь http отправлять данные на сервер через равные промежутки времени, например, на 10 секунд ниже приведен код.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv=(TextView)findViewById(R.id.textview);
text = "";
try {
postData(); // display the data
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void postData() throws JSONException{
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
final Random myRandom = new Random();
HttpPost httppost = new HttpPost("http://mywebpage.com/index.php?id="+myRandom.nextInt(100000));
JSONObject json = new JSONObject();
try {
JSONArray postjson=new JSONArray();
postjson.put(json);
// Post the data:
StringEntity se = new StringEntity( "JSON: " + json.toString());
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
httppost.setEntity(se);
// Execute HTTP Post Request
System.out.print(json);
HttpResponse response = httpclient.execute(httppost);
// for JSON:
if(response != null)
{
InputStream is = response.getEntity().getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
text = sb.toString();
}
tv.setText(text);
}catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
int initialDelay = 1000;
int period = 5000;
Timer timer = new Timer();
TimerTask task = new TimerTask() {
public void run() {
Looper.prepare();
try {
postData();
}
catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Looper.loop();
Looper.myLooper().quit();
}
};
timer.scheduleAtFixedRate(task, initialDelay, period);
}
все ответы приветствуются:)
ФАЙЛ ЛОГА:
01-21 02:07:30.441: W/dalvikvm(575): threadid=7: thread exiting with uncaught exception (group=0x4001d800)
01-21 02:07:30.451: E/AndroidRuntime(575): FATAL EXCEPTION: Timer-0
01-21 02:07:30.451: E/AndroidRuntime(575): android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
01-21 02:07:30.451: E/AndroidRuntime(575): at android.view.ViewRoot.checkThread(ViewRoot.java:2802)
01-21 02:07:30.451: E/AndroidRuntime(575): at android.view.ViewRoot.invalidateChild(ViewRoot.java:607)
01-21 02:07:30.451: E/AndroidRuntime(575): at android.view.ViewRoot.invalidateChildInParent(ViewRoot.java:633)
01-21 02:07:30.451: E/AndroidRuntime(575): at android.view.ViewGroup.invalidateChild(ViewGroup.java:2505)
01-21 02:07:30.451: E/AndroidRuntime(575): at android.view.View.invalidate(View.java:5139)
01-21 02:07:30.451: E/AndroidRuntime(575): at android.widget.TextView.checkForRelayout(TextView.java:5364)