「测网速java」测网速在线测试

博主:adminadmin 2022-12-04 01:00:08 57

今天给各位分享测网速java的知识,其中也会对测网速在线测试进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

java如何获得当前的网络传输速度

1. JAVA还没有API可以做到。

2. 可以通过脚本等到主机的网络流量:windown使用wmi,linux使用命令(watch -n 1 "/sbin/ifconfig eth0 | grep bytes")。

3. 另外就是SNMP协议,通过对于的MID获得主机或者网络设备的流量。

java有个开源的实现叫做SNMP4J,你可以google “SNMP4J 网络流量”搜索到大量信息。(

使用SNMP轮询做实事流量监控的频率不要太高,否则会有负载问题。)

我家网速本来特别快但是我安装java以后变卡了

你是只装了一个jdk吗? jdk 只是一个开发包,不会占用网络资源的。 如果你是装了开发环境,然后部署了一个网络应用项目,如果别人访问你的网络服务器,你可能会卡。但是应该不会是这种情况,因为没什么人会在自己的电脑上装项目。 所以说,应该不是java 影响的网速,找找别的原因吧

Android开发中,如何测当前网速?

原理: 到网上找个可以下载的apk 的URL,记住apk不要过大一般1M足够了.然后通过记录下载这个apk的时间和大小,算出当前网速.当然一个URL并不是很准确,要求精确的可以多下载几个

URL求平均值/span.

package com.panodic.settings.net;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.net.HttpURLConnection;

import java.net.MalformedURLException;

import java.net.URL;

import java.net.URLConnection;

import com.panodic.settings.util.LogUtil;

import com.panodic.settings.util.NetUtil;

import com.panodic.settings.util.Util;

import com.panodic.settings.view.PatchItem;

import android.os.Bundle;

import android.os.Handler;

import android.os.Message;

import android.app.Activity;

import android.view.Menu;

import android.view.View;

import android.view.View.OnClickListener;

import android.view.Window;

import android.widget.Button;

import android.widget.ProgressBar;

import android.widget.TextView;

import android.widget.Toast;

import com.panodic.settings.R;

public class MesureSpeed extends Activity implements OnClickListener {

private static final int LOADING = 0x111;

private static final int STOP = 0x112;

private ProgressBar mBar;

private int mProgressState;

private TextView mSpeed;

private Button mMeasureSpeed;

private PatchItem mBack;

private float mSpeedContent;

private String mAddr = "";

private String mAddr2 = "";

private String mAddr3 = "";

private Handler mHandler = new Handler(Util.sTaskRunner.getLooper());

private int testCount = 0;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

requestWindowFeature(Window.FEATURE_NO_TITLE);

setContentView(R.layout.activity_mesure_speed);

mSpeed = (TextView) findViewById(R.id.speed_content);

mMeasureSpeed = (Button) findViewById(R.id.mesure_speed);

mBack = (PatchItem) findViewById(R.id.patch_settings_net_speed);

mBar = (ProgressBar) findViewById(R.id.bar);

mMeasureSpeed.setOnClickListener(this);

mBack.setOnClickListener(this);

testCount = 0;

}

@Override

public void onClick(View v) {

if (mBack.isMyChild(v)) {

Util.finish(this);

} else if (v == mMeasureSpeed) {

mMeasureSpeed.setEnabled(false);

mBar.setVisibility(View.VISIBLE);

mProgressState = 0;

testCount = 0;

mBar.setProgress(mProgressState);

mHandler.removeCallbacks(null);

mHandler.postDelayed(new Runnable() {

@Override

public void run() {

measureSpeed(mAddr);

}

}, 0);

}

}

private Handler mProgressHandler = new Handler() {

@Override

public void handleMessage(Message msg) {

switch (msg.what) {

case LOADING:

mBar.setProgress(mProgressState);

break;

case STOP:

mBar.setVisibility(View.GONE);

setSpeed();

mMeasureSpeed.setEnabled(true);

break;

default:

break;

}

}

};

private void setSpeed() {

if (mSpeedContent = 1024) {

mSpeedContent = (float) ((mSpeedContent) / (1024 + 0.0));

mSpeedContent = (float) (((int) (mSpeedContent * 10) % 10 + 0.0) / 10 + (int) mSpeedContent);

mSpeed.setText(mSpeedContent + getString(R.string.m));

} else {

mSpeed.setText((int) mSpeedContent + getString(R.string.kb));

}

}

private void measureSpeed(String httpUrl) {

if (!NetUtil.isWifiConnected(this) !NetUtil.isWireConnected(this)) {

Toast.makeText(this, getString(R.string.no_net), Toast.LENGTH_SHORT)

.show();

mProgressHandler.sendEmptyMessage(STOP);

return;

}

int fileLen = 0;

long startTime = 0;

long endTime = 0;

final String fileName = "tmp.apk";

HttpURLConnection conn = null;

InputStream is = null;

FileOutputStream fos = null;

File tmpFile = new File("/sdcard/temp");

if (!tmpFile.exists()) {

tmpFile.mkdir();

}

final File file = new File("/sdcard/temp/" + fileName);

try {

URL url = new URL(httpUrl);

try {

conn = (HttpURLConnection) url.openConnection();

LogUtil.d("lening");

fileLen = conn.getContentLength();

LogUtil.d("len=" + fileLen);

if (fileLen = 0) {

mSpeedContent = 0;

mProgressHandler.sendEmptyMessage(STOP);

Toast.makeText(this, getString(R.string.conn_fail),

Toast.LENGTH_SHORT).show();

return;

}

startTime = System.currentTimeMillis();

is = conn.getInputStream();

fos = new FileOutputStream(file);

byte[] buf = new byte[256];

conn.connect();

if (conn.getResponseCode() = 400) {

Toast.makeText(this, getString(R.string.no_time),

Toast.LENGTH_SHORT).show();

mProgressHandler.sendEmptyMessage(STOP);

return;

} else {

while (true) {

if (is != null) {

int numRead = is.read(buf);

if (numRead = 0) {

break;

} else {

fos.write(buf, 0, numRead);

}

mProgressState += (int) (((numRead + 0.0) / (fileLen + 0.0)) * 1000000);

mProgressHandler.sendEmptyMessage(LOADING);

// LogUtil.d("numRead=" + numRead + " fileLen="

// + fileLen);

} else {

break;

}

}

}

endTime = System.currentTimeMillis();

} catch (IOException e) {

e.printStackTrace();

Toast.makeText(this, getString(R.string.no_permission),

Toast.LENGTH_SHORT).show();

} finally {

if (conn != null) {

conn.disconnect();

}

try {

if (fos != null) {

fos.close();

}

if (is != null) {

is.close();

}

} catch (IOException e1) {

e1.printStackTrace();

}

}

} catch (MalformedURLException e) {

e.printStackTrace();

}

mSpeedContent = fileLen / (endTime - startTime);

mProgressHandler.sendEmptyMessage(STOP);

}

}

测网速java的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于测网速在线测试、测网速java的信息别忘了在本站进行查找喔。

The End

发布于:2022-12-04,除非注明,否则均为首码项目网原创文章,转载请注明出处。