2015-03-18 108 views
0

我的应用程序可以通过avd仿真器访问互联网,但只要我在手机上使用它,或者我将手机用作模拟器。我得到了我仔细检查了,我是在无线网络中的UnknownHostException,我改变了清单与:Android应用程序无法访问互联网

<uses-permission android:name="android.permission.INTERNET" />

我加入这个代码在我的课:

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); 
    StrictMode.setThreadPolicy(policy); 

我重复一遍,我没有问题通过我的AVD访问互联网,但不能与我的手机:S

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_sslclient); 

    //Allowing to access the network 
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); 
    StrictMode.setThreadPolicy(policy); 

    //Getting the GUI user input information 
    web_address_et    = (EditText)findViewById(R.id.editText1); 
    port_number_et    = (EditText)findViewById(R.id.editText2); 
    connection_information_tv = (TextView)findViewById(R.id.textView3); 

    findViewById(R.id.button1).setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      findViewById(R.id.button1).setBackgroundColor(Color.CYAN); 
      findViewById(R.id.button2).setBackgroundColor(color.button_material_dark); 
      if (web_address_et.getText().toString().matches("") 
       || port_number_et.getText().toString().matches("")) { 
       connection_information_tv.setText("Please fill the URL and Port# fields!"); 
      } 

      else { 
       try { 
        InetAddress host = InetAddress.getByName(web_address_et.getText().toString()); 

        //Android native behavior (do not accept untrusted certificate) 
        SSLSocketFactory socketFactory = (SSLSocketFactory)SSLSocketFactory.getDefault(); 
        SSLSocket socket = (SSLSocket)socketFactory.createSocket(host, Integer.parseInt(port_number_et.getText().toString())); 

        socket.startHandshake(); 
        printSSLSessionInfo(socket, socket.getSession()); 
        socket.close(); 

        } catch (UnknownHostException e) { 
         connection_information_tv.setText(e.toString()); 
        } catch (SSLPeerUnverifiedException e) { 
         connection_information_tv.setText(e.toString()); 
        } catch (IOException e) { 
         connection_information_tv.setText(e.toString()); 
        } 
      } 
     } 
    }); 

    findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      findViewById(R.id.button1).setBackgroundColor(color.button_material_dark); 
      findViewById(R.id.button2).setBackgroundColor(Color.CYAN); 
      if (web_address_et.getText().toString().matches("") 
       || port_number_et.getText().toString().matches("")) { 
       connection_information_tv.setText("Please fill the URL and Port# fields!"); 
      } 

      else { 
       try { 
        InetAddress host = InetAddress.getByName(web_address_et.getText().toString()); 

        //Naive custom TrustManager (empty checkServerTrusted) 
        SSLContext sslContext = SSLContext.getInstance("SSL"); 
        TrustManager trustManagerNaive = new X509TrustManager(){ 
                  @Override 
                  public void checkClientTrusted(
                    X509Certificate[] chain, 
                    String authType) 
                    throws CertificateException { 
                   // TODO Auto-generated method stub 
                  } 

                  @Override 
                  public X509Certificate[] getAcceptedIssuers() { 
                   // TODO Auto-generated method stub 
                   return null; 
                  } 

                  @Override 
                  public void checkServerTrusted(
                    X509Certificate[] chain, 
                    String authType) 
                    throws CertificateException { 
                   // TODO Auto-generated method stub 
                  } 
                 }; 

        sslContext.init(null, new TrustManager[]{trustManagerNaive}, new SecureRandom()); 

        SSLSocketFactory socketFactory = (SSLSocketFactory)sslContext.getSocketFactory(); 
        SSLSocket socket = (SSLSocket)socketFactory.createSocket(host, Integer.parseInt(port_number_et.getText().toString())); 

        socket.startHandshake(); 
        printSSLSessionInfo(socket, socket.getSession()); 
        socket.close(); 

        } catch (UnknownHostException e) { 
         connection_information_tv.setText(e.toString()); 
        } catch (NoSuchAlgorithmException e) { 
         connection_information_tv.setText(e.toString()); 
        } catch (KeyManagementException e) { 
         connection_information_tv.setText(e.toString()); 
        } catch (SSLPeerUnverifiedException e) { 
         connection_information_tv.setText(e.toString()); 
        } catch (IOException e) { 
         connection_information_tv.setText(e.toString()); 
        } 
      } 
     } 
    }); 
} 
+0

在ui线程上运行网络的严格模式已经废除了几个版本。不确定哪一个。您的手机和模拟器是否运行相同的android风格? – jb15613 2015-03-18 22:01:23

回答

1

UnknownHo stException

有时与你不容访问,也许你正在使用的资源:

http://localhosthttp://your_IP

您的域名必须是一个有效的域,您可以从您的访问电脑和你的移动设备。

+0

不,它不是在模拟器上,我可以访问谷歌,YouTube或任何和我的手机上,这个例外总是上升! – 2015-03-18 22:06:22

+0

是的,但您尝试访问的网址可能不适用于您的手机。 – Jorgesys 2015-03-18 22:08:56

相关问题