简体   繁体   English

将值从 android 发送到服务器

[英]sending values from android to server

I want to set the following code in a start and stop button to send my values to server after every second.我想在开始和停止按钮中设置以下代码,以每秒将我的值发送到服务器。 so i am wondering how to adopt best strategy for it.should i put in a thread or refresh the activity.所以我想知道如何为它采用最佳策略。我应该加入一个线程还是刷新活动。 please guide me the easy direction to adopt.请指导我采用简单的方向。 Thanks谢谢

URL url = new URL("http://............./add.jsp?a="+value+"&b="+value+"");

URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.flush();
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream(),"UTF-8"));
String line = null;
String response = "";
while ((line = rd.readLine()) != null) {
    response += line;                          
}

wr.close();
rd.close();

Maybe you will want to you an AsyncTask for that, calling this task frequently via a Timer.也许你会想要一个 AsyncTask,通过 Timer 频繁调用这个任务。 Or without AsyncTask and just the Timer.或者没有 AsyncTask 而只有 Timer。 http://developer.android.com/reference/android/os/AsyncTask.html http://developer.android.com/reference/java/util/Timer.html http://developer.android.com/reference/android/os/AsyncTask.html http://developer.android.com/reference/java/util/Timer.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM