繁体   English   中英

Android应用程序出现Internet错误:UnknownHostException

[英]Internet Error on Android app: UnknownHostException

所以我已经发布了一个关于这个的问题,但没有答案,所以这次我会更加精确。 我的问题是这样的:当我的Android应用程序在模拟器上使用时,我的Android应用程序可以完美访问互联网,但是一旦我在手机上使用它,我就会不断收到

UnknownHostException

我更改了清单以允许连接到互联网,我的手机正在android 4.3上运行,WiFi处于“开启”状态,手机上的一切都很好,我可以使用手机连接到Web浏览器,Facebook ...我启动我的应用程序时遇到此异常。 这是我的代码:

package com.example.sslcient;

import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLPeerUnverifiedException;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.os.StrictMode;
import android.support.v7.appcompat.R.color;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class SSLClientActivity extends Activity {

    private EditText web_address_et;
    private EditText port_number_et;
    private TextView connection_information_tv;
    private Certificate[] certs;

    @Override
    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 default behaviour (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());
                        }
                }
            }
        });
    }

    private void printSSLSessionInfo(SSLSocket socket, SSLSession sslSession) throws SSLPeerUnverifiedException {

        String certIssuerDN = "";
        certs = sslSession.getPeerCertificates();

        for (Certificate cert : certs) {
            System.out.println("Certificate is: " + cert);
            if(cert instanceof X509Certificate) {
                    X509Certificate x = (X509Certificate ) cert;
                    certIssuerDN = certIssuerDN + x.getIssuerDN() + "\n";
            }
        }

        connection_information_tv.setText(
                        "SSL session id: " + sslSession.getId() +
                        " | Valid session? " + sslSession.isValid() +
                        "\nPeer host/port: " + sslSession.getPeerHost() + "/" + sslSession.getPeerPort() +
                        "\nRequire client authentificartion: " + socket.getNeedClientAuth() +
                        "\nProtocol: " + sslSession.getProtocol() +  
                        "\nCipher suite: " + sslSession.getCipherSuite() +
                        "\n\nCertificates retrieved: " + certs.length +
                        "\n" + certIssuerDN
                        );
    }
}

这是我的清单:

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.sslcient"
    android:versionCode="1"
    android:versionName="1.0" >

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

    <uses-sdk
        android:minSdkVersion="9"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".SSLClientActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

请我真的需要帮助,我不知道哪里出了问题应该起作用!

我解决了这个问题。 其实问题出在URL上,我忘了删除URL末尾的'\\ n',这就是为什么我有UnknownHostException

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM