• 解决android.os.NetworkOnMainThreadException


       今天刚接触java的socket,自己写了一个简单的例子,客户端是android的,主要代码如下

     1     protected void onCreate(Bundle savedInstanceState) {
     2         
     3         
     4     
     5         super.onCreate(savedInstanceState);
     6         setContentView(R.layout.activity_main);
     7         
     8         btn=(Button)this.findViewById(R.id.but1);
     9         editText=(EditText)this.findViewById(R.id.edit);
    10         text=(TextView)this.findViewById(R.id.text1);
    11         
    12         
    13         btn.setOnClickListener(new Button.OnClickListener() {
    14             
    15             @Override
    16             public void onClick(View arg0) {
    17                 // TODO Auto-generated method stub
    18                 Log.v("random", "ffxxbb");
    19                 Socket socket=null;
    20                 String sendMsg=editText.getText().toString();
    21                 Log.v("random", sendMsg);
    22                 String ip="172.16.45.71";
    23                 
    24                 
    25             
    26                 try {
    27                     socket=new Socket(ip,6660);
    28                     Log.v("random", "socket------after");
    29                     //向服务器发送消息
    30                     OutputStreamWriter outputStreamWriter=new OutputStreamWriter(socket.getOutputStream());
    31                     BufferedWriter bufferedWriter=new BufferedWriter(outputStreamWriter);
    32                     PrintWriter printWriter=new PrintWriter(bufferedWriter,true);
    33                     printWriter.println(sendMsg);
    34                     
    35                     
    36                     //接收服务器的消息
    37                     InputStreamReader inputStreamReader=new InputStreamReader(socket.getInputStream());
    38                     BufferedReader br=new BufferedReader(inputStreamReader);
    39                     String msg=br.readLine();
    40                     text.setText(msg);
    41                     
    42                 
    43                      printWriter.close();
    44                     //br.close();
    45                     //socket.close();
    46                     
    47                 } catch (UnknownHostException e) {
    48                     // TODO Auto-generated catch block
    49                     Log.v("random", "ssss"+e.getMessage());
    50                     e.printStackTrace();
    51                     
    52                 } catch (IOException e) {
    53                     // TODO Auto-generated catch block
    54                     Log.v("random","vvvvvvvv"+ e.getMessage());
    55                     e.printStackTrace();
    56                     
    57                 }
    58     
    59                 
    60                 
    61             }
    62         });
    63     }

    可是,运行的时候,就报出 的错误,刚开始以为ip或端口写错了,修改了端口也一样,于是就上网找找资料,这个异常大概意思是在主线程访问网络时出的异常。 Android在4.0之前的版本 支持在主线程中访问网络,但是在4.0以后对这部分程序进行了优化,也就是说访问网络的代码不能写在主线程中了。

    修改后的代码如下

     1     protected void onCreate(Bundle savedInstanceState) {
     2         
     3         
     4     
     5         super.onCreate(savedInstanceState);
     6         setContentView(R.layout.activity_main);
     7         
     8         btn=(Button)this.findViewById(R.id.but1);
     9         editText=(EditText)this.findViewById(R.id.edit);
    10         text=(TextView)this.findViewById(R.id.text1);
    11         
    12         
    13         btn.setOnClickListener(new Button.OnClickListener() {
    14             
    15             @Override
    16             public void onClick(View arg0) {
    17                 // TODO Auto-generated method stub
    18                 Log.v("random", "ffxxbb");
    19                 Socket socket=null;
    20                 String sendMsg=editText.getText().toString();
    21                 Log.v("random", sendMsg);             
    25                 Thread t=new MyThread();
    26                 t.start();
    27         
    28             }
    29         });
    30     }
    31 
    32 
    33 }
    34 
    35 
    36 class  MyThread extends Thread{
    37     
    38     String ip ="172.16.45.71";
    39     
    40     public void run(){
    41         
    42         try {
    43             Socket socket=new Socket(ip,6669);
    44             
    45             OutputStreamWriter outputStreamWriter=new OutputStreamWriter(socket.getOutputStream());
    48             BufferedWriter bufferedWriter=new BufferedWriter(outputStreamWriter);
    49             PrintWriter printWriter=new PrintWriter(bufferedWriter,true);
    50             printWriter.println("ffxxbb");
    51 
    52 
    53             printWriter.close();
    54             //br.close();
    55             socket.close();
    56             
    57         } catch (UnknownHostException e) {
    58             // TODO Auto-generated catch block
    59             e.printStackTrace();
    60         } catch (IOException e) {
    61             // TODO Auto-generated catch block
    62             e.printStackTrace();
    63         }
    64         
    65     }
    66 }
  • 相关阅读:
    Linux 用户管理命令-usermod和chage
    用户管理命令-passwd
    Linux 用户管理命令-useradd
    用户和用户组管理-用户管理相关文件
    用户和用户组管理-组信息文件
    用户和用户组管理-影子文件shadow
    jQuery火箭图标返回顶部代码
    jQuery火箭图标返回顶部代码
    jQuery火箭图标返回顶部代码
    jQuery火箭图标返回顶部代码
  • 原文地址:https://www.cnblogs.com/ffxxbb/p/3511351.html
Copyright © 2020-2023  润新知