android使用timer检测网络请求时长
2015-10-13 18:42
399 查看
一个小例子,里面用到了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"; @Override protected 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; } }; }; @Override public void onClick(View view) { switch (view.getId()) { case R.id.tv_connect: if (isRun) { stopTimer(); } else { new Thread(new Runnable() { @Override public void run() { getCourses(); } }).start(); startTimer(); } break; default: break; } } private void startTimer() { if (mTimerTask == null) { mTimerTask = new TimerTask() { @Override public 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() { @Override public void run() { Toast.makeText(MainActivity.this, "连接成功", 0).show(); } }); Log.e("success", "success"); } } catch (MalformedURLException e) { e.printStackTrace(); stopTimer(); MainActivity.this.runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(MainActivity.this, "连接失败", 0).show(); } }); } catch (IOException e) { e.printStackTrace(); stopTimer(); MainActivity.this.runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(MainActivity.this, "连接失败", 0).show(); } }); } } }
相关文章推荐
- java-用HttpURLConnection发送Http请求.
- C#中Forms.Timer、Timers.Timer、Threading.Timer的用法分析
- C#使用timer实现的简单闹钟程序
- C#中timer定时器用法实例
- C#自定义基于控制台的Timer实例
- C#中的Timer和DispatcherTimer使用实例
- php实现的Timer页面运行时间监测类
- .NET中的Timer类型用法详解
- asp.net中Timer无刷新定时器的实现方法
- C#使用timer定时在屏幕上输出信息的方法
- Android Timer使用的实例代码
- Android 中HttpURLConnection与HttpClient使用的简单实例
- Android通过HttpURLConnection和HttpClient接口实现网络编程
- PHP定时执行任务实现方法详解(Timer)
- Python定时执行之Timer用法示例
- python定时器(Timer)用法简单实例
- go语言中使用timer的常用方式
- Timer计时任务因系统时间的修改导致挂起解决方案
- android定时方法
- EJB3.0 Timer