不幸的是,____已停止工作(网络问题?测量带宽)

时间:2013-07-03 19:50:19

标签: android network-programming logcat bandwidth android-logcat

我真的被logcat stacktrace(我不熟悉android)所困扰,同时创建这个基本方法来检查我的设备的网络带宽。代码加载网页,花费多长时间,并测量下载的内容。然后将该值除以加载网页以计算近似带宽所需的毫秒数。

代码:

public class MainActivity extends Activity {
private int linkSpeed;
private TextView textView;

/** Called when the activity is created. */

@Override
public void onCreate(Bundle savedInstanceState) {       
    super.onCreate(savedInstanceState);
    textView = new TextView(this);
    textView.setTextSize(25);

  //Download your image
    Thread thread = new Thread(new Runnable() {
        public void run() {
            try {
                String urlString = "http://www.google.com";
                long startTime = System.currentTimeMillis();
                HttpGet httpRequest = new HttpGet(new URL(urlString).toURI());
                HttpClient httpClient = new DefaultHttpClient();
                HttpResponse response = (HttpResponse) httpClient.execute(httpRequest);
                long endTime = System.currentTimeMillis();

                HttpEntity entity = response.getEntity();
                BufferedHttpEntity bufHttpEntity;
                bufHttpEntity = new BufferedHttpEntity(entity);

                //You can re-check the size of your file
                final long contentLength = bufHttpEntity.getContentLength();

                // Log
                String TAG = "tag";
                Log.d(TAG, "[BENCHMARK] Dowload time :"+(endTime-startTime)+" ms");

                // Bandwidth : size(KB)/time(s)
                float bandwidth = contentLength / ((endTime-startTime) *1000);

                textView.setText("bandwidth = " + bandwidth);
                setContentView(textView);
            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (URISyntaxException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    });
    thread.start();
}

我对LOGCAT抱怨无法创建处理程序感到困惑,因为我从未在这个类中使用过。 LOGCAT TRACE:

07-03 14:42:18.214: D/dalvikvm(2401): Late-enabling CheckJNI
07-03 14:42:18.474: D/tag(2401): [BENCHMARK] Dowload time :166 ms
07-03 14:42:18.474: W/dalvikvm(2401): threadid=11: thread exiting with uncaught exception (group=0x4162e930)
07-03 14:42:18.474: E/AndroidRuntime(2401): FATAL EXCEPTION: Thread-201
07-03 14:42:18.474: E/AndroidRuntime(2401): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
07-03 14:42:18.474: E/AndroidRuntime(2401):     at android.os.Handler.<init>(Handler.java:197)
07-03 14:42:18.474: E/AndroidRuntime(2401):     at android.os.Handler.<init>(Handler.java:111)
07-03 14:42:18.474: E/AndroidRuntime(2401):     at com.android.internal.app.ActionBarImpl.<init>(ActionBarImpl.java:108)
07-03 14:42:18.474: E/AndroidRuntime(2401):     at android.app.Activity.initActionBar(Activity.java:1867)
07-03 14:42:18.474: E/AndroidRuntime(2401):     at android.app.Activity.setContentView(Activity.java:1902)
07-03 14:42:18.474: E/AndroidRuntime(2401):     at com.example.networkinfo.MainActivity$1.run(MainActivity.java:69)
07-03 14:42:18.474: E/AndroidRuntime(2401):     at java.lang.Thread.run(Thread.java:856)
07-03 14:42:18.524: D/dalvikvm(2401): GC_CONCURRENT freed 232K, 5% free 7525K/7912K, paused 9ms+2ms, total 50ms
07-03 14:42:18.894: D/libEGL(2401): loaded /system/lib/egl/libEGL_tegra.so
07-03 14:42:18.914: D/libEGL(2401): loaded /system/lib/egl/libGLESv1_CM_tegra.so
07-03 14:42:18.924: D/libEGL(2401): loaded /system/lib/egl/libGLESv2_tegra.so
07-03 14:42:18.944: D/OpenGLRenderer(2401): Enabling debug mode 0

3 个答案:

答案 0 :(得分:0)

您无法从非UI线程调用setContentView。我的兴趣是将该线程转换为AsyncTask并在onPostExecute中设置内容视图。您的setText调用中存在同样的问题。

答案 1 :(得分:0)

这一行

textView.setText("bandwidth = " + bandwidth);

给你带来麻烦。您正尝试更新后台UI上的Thread。您需要将其设为AsyncTask并使用UI以外的方法更新doInBackground()或使用runOnUiThread()

Here is an example of using runOnUiThread()

and one of AsyncTask

AsyncTask Docs

答案 2 :(得分:0)

java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

您无法从后台线程调用setContentView()。此外,您需要在活动启动时显示部分 UI,而不是等待可能永远无法成功完成的HTTP操作。

相反,在执行后台工作之前调用setContentView() 。特别是,请考虑使用AsyncTask而不是Thread,并使用onPostExecute()中的HTTP操作结果(如果成功)更新您的小部件。