您的位置:首页 > 理论基础 > 计算机网络

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();
}
});
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  httpUrlConnection timer