当前位置:   article > 正文

手把手教你解决android网络请求问题:android.os.NetworkOnMainThreadException_could not dispatch event:messageevent to subscribi

could not dispatch event:messageevent to subscribing android.os.networkonmai


  1. public void onCreate(Bundle savedInstanceState) {
  2. super.onCreate(savedInstanceState);
  3. this.setContentView(R.layout.share_mblog_view);
  4. new Thread(runnable).start();
  5. }
  6. Handler handler = new Handler(){
  7. @Override
  8. public void handleMessage(Message msg) {
  9. super.handleMessage(msg);
  10. Bundle data = msg.getData();
  11. String val = data.getString("value");
  12. Log.i("mylog","请求结果-->" + val);
  13. }
  14. }
  15. Runnable runnable = new Runnable(){
  16. @Override
  17. public void run() {
  18. //
  19. // TODO: http request.
  20. //
  21. Message msg = new Message();
  22. Bundle data = new Bundle();
  23. data.putString("value","请求结果");
  24. msg.setData(data);
  25. handler.sendMessage(msg);
  26. }
  27. }

  最近在搞基于android虚拟机的通信项目,就是实现android虚拟机上的客户端和其所在电脑上的服务器进行通信。但是在所有的东西都写好后,发现android虚拟机端总是在代码:

HttpResponse response = new DefaultHttpClient().execute(request);

 

处包错,其错误为:

  1. java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.andconweb/com.example.andconweb.MainActivity}: android.os.NetworkOnMainThreadException
  2. 10-02 09:13:37.500: E/AndroidRuntime(2657): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
  3. 10-02 09:13:37.500: E/AndroidRuntime(2657): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
  4. 10-02 09:13:37.500: E/AndroidRuntime(2657): at android.app.ActivityThread.access$600(ActivityThread.java:141)
  5. 10-02 09:13:37.500: E/AndroidRuntime(2657): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
  6. 10-02 09:13:37.500: E/AndroidRuntime(2657): at android.os.Handler.dispatchMessage(Handler.java:99)
  7. 10-02 09:13:37.500: E/AndroidRuntime(2657): at android.os.Looper.loop(Looper.java:137)
  8. 10-02 09:13:37.500: E/AndroidRuntime(2657): at android.app.ActivityThread.main(ActivityThread.java:5041)
  9. 10-02 09:13:37.500: E/AndroidRuntime(2657): at java.lang.reflect.Method.invokeNative(Native Method)
  10. 10-02 09:13:37.500: E/AndroidRuntime(2657): at java.lang.reflect.Method.invoke(Method.java:511)
  11. 10-02 09:13:37.500: E/AndroidRuntime(2657): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
  12. 10-02 09:13:37.500: E/AndroidRuntime(2657): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
  13. 10-02 09:13:37.500: E/AndroidRuntime(2657): at dalvik.system.NativeStart.main(Native Method)
  14. 10-02 09:13:37.500: E/AndroidRuntime(2657): Caused by: android.os.NetworkOnMainThreadException
  15. 10-02 09:13:37.500: E/AndroidRuntime(2657): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
  16. 10-02 09:13:37.500: E/AndroidRuntime(2657): at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
  17. 10-02 09:13:37.500: E/AndroidRuntime(2657): at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
  18. 10-02 09:13:37.500: E/AndroidRuntime(2657): at libcore.io.IoBridge.connect(IoBridge.java:112)
  19. 10-02 09:13:37.500: E/AndroidRuntime(2657): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
  20. 10-02 09:13:37.500: E/AndroidRuntime(2657): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
  21. 10-02 09:13:37.500: E/AndroidRuntime(2657): at java.net.Socket.connect(Socket.java:842)
  22. 10-02 09:13:37.500: E/AndroidRuntime(2657): at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
  23. 10-02 09:13:37.500: E/AndroidRuntime(2657): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
  24. 10-02 09:13:37.500: E/AndroidRuntime(2657): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
  25. .......

 本来以为是代码出错或者是获取Http连接时传入的URL有问题,传入的URL如下:

http://10.0.2.2:8080/AndroidWeb/loadMessage

 但是,在android虚拟机中的浏览器上输入该网址后,却得到了想要的信息。有此推断出错的地方不是服务器端的代码。

调试了好久,才发现是因为Http请求写在了主线程里, 在4.0之后在主线程里面执行Http请求都会报这个错,大概是怕Http请求时间太长造成程序假死的情况吧。

解决方法有两种,下面我分别介 绍:

第一种,是在主线程中直接忽略,强制执行。(不推荐这种方法,但是该方法修改起来简单)

在MainActivity文件的setContentView(R.layout.activity_main)下面加上如下代码

if (android.os.Build.VERSION.SDK_INT > 9) {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
    }

 如果,有报错就在class头上加上

  1. @TargetApi(Build.VERSION_CODES.GINGERBREAD)
  2. @SuppressLint("NewApi")

在onCreate上面加上:

@SuppressLint("NewApi")

第二种,启动另一个线程执行网络连接任务,比如使用Thread、Runnable、Handler(推荐使用这种方法)

代码如下:

  1. public void onCreate(Bundle savedInstanceState) {
  2. super.onCreate(savedInstanceState);
  3. this.setContentView(R.layout.share_mblog_view);
  4. new Thread(runnable).start();
  5. }
  6. Handler handler = new Handler(){
  7. @Override
  8. public void handleMessage(Message msg) {
  9. super.handleMessage(msg);
  10. Bundle data = msg.getData();
  11. String val = data.getString("value");
  12. Log.i("mylog","请求结果-->" + val);
  13. }
  14. }
  15. Runnable runnable = new Runnable(){
  16. @Override
  17. public void run() {
  18. //
  19. // TODO: http request.
  20. //
  21. Message msg = new Message();
  22. Bundle data = new Bundle();
  23. data.putString("value","请求结果");
  24. msg.setData(data);
  25. handler.sendMessage(msg);
  26. }
  27. }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/465198
推荐阅读
相关标签
  

闽ICP备14008679号