我明白這個問題已被多次詢問,但其他解決方案似乎並沒有幫助我,因爲我的代碼非常具體。我試圖監視我的網絡帶寬和質量,並且我已經查看了有關如何在StackOverflow上執行此操作的示例代碼。監控網絡帶寬的問題
以下是我有什麼(很多是由成員SO資),在我MainActivity.java類:
public class MainActivity extends Activity {
@SuppressLint("NewApi")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final String TAG = "test";
long startTime;
long endTime;
BufferedHttpEntity bufHttpEntity;
float bandwidth;
try {
startTime = System.currentTimeMillis();
String urlString = "http://www.google.com";
HttpGet httpRequest = new HttpGet(new URL(urlString).toURI());
HttpClient httpClient = new DefaultHttpClient();
HttpResponse response = (HttpResponse) httpClient.execute(httpRequest);
endTime = System.currentTimeMillis();
HttpEntity entity = response.getEntity();
bufHttpEntity = new BufferedHttpEntity(entity);
//You can re-check the size of your file
final long contentLength = bufHttpEntity.getContentLength();
// Log
Log.d(TAG, "[BENCHMARK] Dowload time :"+(endTime-startTime)+" ms");
// Bandwidth : size(KB)/time(s)
System.out.println("here");
bandwidth = contentLength/((endTime-startTime) *1000);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
bandwidth = -1;
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
bandwidth = -1;
e.printStackTrace();
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
bandwidth = -1;
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
bandwidth = -1;
e.printStackTrace();
}
int linkSpeed;
WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
if (wifiInfo != null) {
linkSpeed = wifiInfo.getLinkSpeed(); //measured using WifiInfo.LINK_SPEED_UNITS
}
else {
linkSpeed = -1;
}
// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(25);
textView.setText("Linkspeed = " + linkSpeed + "\nbandwidth = " + bandwidth);
// Set the text view as the activity layout
setContentView(textView);
}
//Set up the {@link android.app.ActionBar}, if the API is available.
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void setupActionBar() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
getActionBar().setDisplayHomeAsUpEnabled(true);
}
}
我已經做了錯誤檢查,我發現兩個問題。
呼叫 「WifiInfo wifiInfo = wifiManager.getConnectionInfo()」 導致 「不幸的是,XXX已經停止」 的錯誤,導致我的模擬器崩潰。我似乎無法解決這個問題。我不認爲我需要拋出一個try-catch塊......?
當我在urlString中輸入一個網站時,發生類似的「不幸的是,xxx已停止」錯誤。當我將該字段留空時,帶寬變爲-1,因爲沒有指定網站,並拋出異常。
06-26 19:10:15.164: D/AndroidRuntime(2402): Shutting down VM
06-26 19:10:15.203: W/dalvikvm(2402): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
06-26 19:10:15.244: E/AndroidRuntime(2402): FATAL EXCEPTION: main
06-26 19:10:15.244: E/AndroidRuntime(2402): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.networkinfo/com.example.networkinfo.MainActivity}: android.os.NetworkOnMainThreadException
06-26 19:10:15.244: E/AndroidRuntime(2402): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
06-26 19:10:15.244: E/AndroidRuntime(2402): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
06-26 19:10:15.244: E/AndroidRuntime(2402): at android.app.ActivityThread.access$600(ActivityThread.java:141)
06-26 19:10:15.244: E/AndroidRuntime(2402): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
06-26 19:10:15.244: E/AndroidRuntime(2402): at android.os.Handler.dispatchMessage(Handler.java:99)
06-26 19:10:15.244: E/AndroidRuntime(2402): at android.os.Looper.loop(Looper.java:137)
06-26 19:10:15.244: E/AndroidRuntime(2402): at android.app.ActivityThread.main(ActivityThread.java:5041)
06-26 19:10:15.244: E/AndroidRuntime(2402): at java.lang.reflect.Method.invokeNative(Native Method)
06-26 19:10:15.244: E/AndroidRuntime(2402): at java.lang.reflect.Method.invoke(Method.java:511)
06-26 19:10:15.244: E/AndroidRuntime(2402): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-26 19:10:15.244: E/AndroidRuntime(2402): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-26 19:10:15.244: E/AndroidRuntime(2402): at dalvik.system.NativeStart.main(Native Method)
06-26 19:10:15.244: E/AndroidRuntime(2402): Caused by: android.os.NetworkOnMainThreadException
06-26 19:10:15.244: E/AndroidRuntime(2402): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
06-26 19:10:15.244: E/AndroidRuntime(2402): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
06-26 19:10:15.244: E/AndroidRuntime(2402): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
06-26 19:10:15.244: E/AndroidRuntime(2402): at java.net.InetAddress.getAllByName(InetAddress.java:214)
06-26 19:10:15.244: E/AndroidRuntime(2402): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
06-26 19:10:15.244: E/AndroidRuntime(2402): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
06-26 19:10:15.244: E/AndroidRuntime(2402): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
06-26 19:10:15.244: E/AndroidRuntime(2402): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
06-26 19:10:15.244: E/AndroidRuntime(2402): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
06-26 19:10:15.244: E/AndroidRuntime(2402): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
06-26 19:10:15.244: E/AndroidRuntime(2402): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
06-26 19:10:15.244: E/AndroidRuntime(2402): at com.example.networkinfo.MainActivity.onCreate(MainActivity.java:46)
06-26 19:10:15.244: E/AndroidRuntime(2402): at android.app.Activity.performCreate(Activity.java:5104)
06-26 19:10:15.244: E/AndroidRuntime(2402): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
06-26 19:10:15.244: E/AndroidRuntime(2402): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
06-26 19:10:15.244: E/AndroidRuntime(2402): ... 11 more
發佈logcat stacktrace –
發佈,希望有所幫助。 –
顯而易見'由android.os.NetworkOnMainThreadException'引起的,你不能在MainThread上做網絡操作。 –