【发布时间】:2017-04-29 09:56:56
【问题描述】:
我想在运行它时将带有获取 URL 的 a 和 b 发送到 example.com,它可以工作一次。
我想在条件为真期间发送 a 和 b。我使用睡眠,所以连续 5 秒后发送数据。
public class MapsActivity extends FragmentActivity
implements OnMapReadyCallback {
public a=0,b=0;
public btn=2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
connect();
} //end of onCreate
public void connect() {
new Thread(new Runnable() {
@Override
public void run() {
communication();
}
}).start();
}
public void communication() {
while(btn >0){
try {
btn++;
a++;
b++;
URL url = new URL("example.com"+ "?a=" +a+"&b="+b);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.connect();
InputStream inputStream = conn.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String data = "", line = "";
while ((line = bufferedReader.readLine()) != null) {
data += line + "\n";
}
sleep(5000);
} catch (MalformedURLException e) {
Log.e("error", e.toString());
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}//end of while
} //end of communication
}//end of class
【问题讨论】:
标签: java android android-studio while-loop get