1

So I already posted a question about that and got no answer so I will be more precise this time. My problem is this one: I have my Android app accessing internet perfectly when it is used on the emulator however as soon as I use it on my phone I keep receiving

UnknownHostException

I have changed my manifest to allow a connection to internet, my phone is running on android 4.3, the WiFi is "on", everything is fine on my phone I can connect to my web browser, Facebook... with my phone but when I launch my application I get this exception. Here is my code:

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
                        );
    }
}

Here is my manifest:

 <?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>

Please I really need help, I don't understand what is going wrong that should work!

Kimbluey
  • 1,199
  • 2
  • 12
  • 23
aress31
  • 340
  • 5
  • 20
  • Is `UnknownHostException` the entire error message?... – Kimbluey Mar 26 '15 at 22:18
  • java.net.UnknownHostException: Unable to resolve host "url": No address associzted with hostname – aress31 Mar 26 '15 at 22:26
  • 1
    Check out [this possible duplicate question](http://stackoverflow.com/questions/3314137/android-unknownhostexception) and see if you can find your solution there. – Kimbluey Mar 26 '15 at 22:29
  • 1
    I also suggest searching your problem on SO before posting questions - it seems this has many possible duplicates. Search [Android UnknownHostException](http://stackoverflow.com/search?q=Android+UnknownHostException) on Stack Overflow and you'll see a ton of questions about this error! If you still cannot find a solution, update your question to explain why the solutions other users have provided did not work for you. – Kimbluey Mar 26 '15 at 22:38
  • I already checked before posting, none of the previous post helped me. I edited the manifest I have an internet connection on my phone I am targeting the good sdk version... That's why I am posting my problem. – aress31 Mar 26 '15 at 22:40
  • Try putting the permission *right* before the closing `` tag, exactly like [this answer](http://stackoverflow.com/a/8893558/4019922) illustrates. – Kimbluey Mar 26 '15 at 22:45
  • @kimbluey I already tried that and that made no difference :S – aress31 Mar 26 '15 at 23:24

1 Answers1

0

I solved this problem. Actually the problem came from the URL, I forgot to delete the '\n' at the end of the URL that's why I had the exception of UnknownHostException

aress31
  • 340
  • 5
  • 20