android使用timer检测网络请求时长

来源:互联网 发布:手机优化软件哪个最好 编辑:程序博客网 时间:2024/05/18 03:29

一个小例子,里面用到了httpUrlConnection请求网络、timerTask定时任务、handler通信,Activity.runOnUIThread()


package com.example.httpurlconnection;import java.io.IOException;import java.io.OutputStream;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.URL;import java.net.URLConnection;import java.util.Timer;import java.util.TimerTask;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.app.Activity;import android.text.Html;import android.util.Log;import android.view.View;import android.widget.TextView;import android.widget.Toast;public class MainActivity extends Activity implements View.OnClickListener {private TextView tv_connect;private TextView tv_time;private int time = 0;private Timer mTimer;private TimerTask mTimerTask;private boolean isRun = false;private static final String PATH = "http://baidu.com";@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);tv_connect = (TextView) findViewById(R.id.tv_connect);tv_time = (TextView) findViewById(R.id.tv_time);tv_connect.setOnClickListener(this);}private Handler mHandler = new Handler() {public void handleMessage(Message msg) {switch (msg.what) {case 1:tv_time.setText(Html.fromHtml("等待<font color='#ff4163'> "+ msg.arg1 + "</font> s"));break;default:break;}};};@Overridepublic void onClick(View view) {switch (view.getId()) {case R.id.tv_connect:if (isRun) {stopTimer();} else {new Thread(new Runnable() {@Overridepublic void run() {getCourses();}}).start();startTimer();}break;default:break;}}private void startTimer() {if (mTimerTask == null) {mTimerTask = new TimerTask() {@Overridepublic void run() {Message msg = new Message();msg.arg1 = time++;msg.what = 1;mHandler.sendMessage(msg);isRun = true;}};mTimer = new Timer();mTimer.schedule(mTimerTask, 0, 1000);}}private void stopTimer() {if (mTimer != null) {mTimer.cancel();}if (mTimerTask != null) {mTimerTask.cancel();mTimerTask = null;time = 0;}isRun = false;}private void getCourses() {try {URL url = new URL(PATH);URLConnection connection = url.openConnection();HttpURLConnection httpUrlConnection = (HttpURLConnection) connection;httpUrlConnection.setDoOutput(true);httpUrlConnection.setDoInput(true);httpUrlConnection.setUseCaches(false);httpUrlConnection.setRequestProperty("Content-type","application/x-java-serialized-object");httpUrlConnection.setRequestMethod("POST");httpUrlConnection.setConnectTimeout(3000);httpUrlConnection.setReadTimeout(3000);httpUrlConnection.connect();String param = "name=abc&password=123";OutputStream outStrm = httpUrlConnection.getOutputStream();outStrm.write(param.getBytes());if (httpUrlConnection.getResponseCode() == 200) {stopTimer();// 通过runOnUiThread方法进行修改主线程的控件内容MainActivity.this.runOnUiThread(new Runnable() {@Overridepublic void run() {Toast.makeText(MainActivity.this, "连接成功", 0).show();}});Log.e("success", "success");}} catch (MalformedURLException e) {e.printStackTrace();stopTimer();MainActivity.this.runOnUiThread(new Runnable() {@Overridepublic void run() {Toast.makeText(MainActivity.this, "连接失败", 0).show();}});} catch (IOException e) {e.printStackTrace();stopTimer();MainActivity.this.runOnUiThread(new Runnable() {@Overridepublic void run() {Toast.makeText(MainActivity.this, "连接失败", 0).show();}});}}}

0 0