如下代码所示,在andorid stuio中我创建了个SocketThread线程,然后试图在里面进行网络连接,ip,端口设置都正确 ,同事我在android Manifest文件中也添加了网络权限,但是我在调试的时候每次获取的socket对象都为空,请问一下这是什么原因,快被整崩溃了 
------------------------------------------------------------------------------------------------
 public void conn() {        try {
            Log.i(TAG, "连接中……");
            //client = new Socket(Ip, Port);
            Socket client = new Socket(Ip, Port);
            client.setSoTimeout(timeout); // 设置阻塞时间
            Log.i(TAG, "连接成功");
            in = client.getInputStream();
            out = client.getOutputStream();
            out.write("haha,shouji fasong chenggong ".getBytes());
            out.flush();
            Log.i(TAG, "输入输出流获取成功");
        } catch (UnknownHostException e) {
            Log.i(TAG, "连接错误UnknownHostException 重新获取");
            e.printStackTrace();
            //conn();
        } catch (IOException e) {
            Log.i(TAG, "连接服务器io错误");
            e.printStackTrace();
        } catch (Exception e) {
            Log.i(TAG, "连接服务器错误Exception" + e.getMessage());
            e.printStackTrace();
        }
    }
-------------------------------------------------------------------------------------------------------------------------------------调试捕捉到的代码
**********************************************************************************************************
07-09 12:35:53.217 12041-12041/smarthome.hackha.wificonnection I/socket thread: 创建线程socket
07-09 12:35:53.218 12041-12041/smarthome.hackha.wificonnection I/socket thread: 连接中……
07-09 12:35:53.236 12041-12041/smarthome.hackha.wificonnection D/libc-netbsd: [getaddrinfo]: mtk hostname=192.168.1.6; servname=(null); cache_mode=(null), netid=0; =0
07-09 12:35:53.236 12041-12041/smarthome.hackha.wificonnection D/libc-netbsd: getaddrinfo( app_uid:10128
07-09 12:35:53.236 12041-12041/smarthome.hackha.wificonnection D/libc-netbsd: getaddrinfo() uid prop:
07-09 12:35:53.237 12041-12041/smarthome.hackha.wificonnection D/libc-netbsd: getaddrinfo() getuid():10128
07-09 12:35:53.237 12041-12041/smarthome.hackha.wificonnection D/libc-netbsd: [getaddrinfo]: mtk ai_addrlen=0; ai_canonname=(null); ai_flags=4; ai_family=0
07-09 12:35:53.243 12041-12041/smarthome.hackha.wificonnection I/System.out: [CDS]connect[/192.168.1.6:8080] tm:90
07-09 12:35:53.252 12041-12041/smarthome.hackha.wificonnection I/socket thread: 连接服务器错误Exceptionnull
**********************************************************************************************************

解决方案 »

  1.   

    你访问对应的IP和port有在运行吗
      

  2.   

    楼主把错误的类型打出来看估计能知道点什么
    Log.i(TAG, "连接服务器错误Exception" + e.getClass().getName());
      

  3.   

    我用手机端的一个socket测试软件过,该ip端口也是正确的。但是仍然连接不上,对于三楼说的这个我试一下的
      

  4.   

    我用三楼的方法获取的错误是"NetworkOnMainThreadException"这个有知道啥子原因的嘛
      

  5.   

    我参考了网上的方法,在要调用socket的主线程的的Activity里面的onCreate函数里面添加如下代码:
    StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites().detectNetwork().penaltyLog().build());StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectLeakedSqlLiteObjects().detectLeakedClosableObjects().penaltyLog().penaltyDeath().build());
    居然就没事了,我也是醉了哦
      

  6.   

     android调试中也遇到了该情况,mtk hostname=192.168.7.40; servname=(null); netid=0; =0
    卸载,再次安装又没问题了,