[英]android timer handler java
我想使用計時器在我的數據庫中插入值,我從使用計時器開始顯示味精開始,它可以工作,但是當我添加了第一個插入值的函數時,只顯示味精顯示而沒有插入數據! 這是我的代碼
public class TimertestActivity extends Activity {
private double lat;
private double lng;
Handler handler = new Handler();
Runnable runnable = new Runnable() {
public void run() {
afficher();
}
};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
lat = 123456.98;
lng = 321654.78;
runnable.run();
}
public void afficher()
{
Toast.makeText(getBaseContext(),
"lat" +lat+ "lng" +lng,
Toast.LENGTH_SHORT).show();
insertvalues(lat, lng);
handler.postDelayed(runnable, 25000);
}
public void insertvalues(double lat, double lng){
//http post c à d envoi des données
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new
HttpPost("http://www.pizza-paris.com/clic/marwa/test/form.php?lat="+lat+"&lng="+lng);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
Log.i("postData", response.getStatusLine().toString());
}
catch(Exception e)
{
Log.e("log_tag", "Error in http connection "+e.toString());
}
}
}
解決方案是在清單中添加Internet權限。 總是簡單的事情。
請閱讀此鏈接以獲取有關權限的信息。
正確的格式為:
<uses-permission
android:name="android.permission.INTERNET" />
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.