0

I have been trying to test internet connection by connecting to the server https://mobile.twitter.com/.

For that reason, I have used an AsyncTask as follow:

class CheckInternetConnection extends AsyncTask<Void, Void, Boolean>{
        @Override
        protected void onPostExecute(Boolean result){
            if(result)
                //do stuff
            else
                //do stuff
        }
        @Override
        protected Boolean doInBackground(Void... params) {
            try {
                HttpURLConnection urlc = (HttpURLConnection) (new URL("https://mobile.twitter.com/").openConnection());
                urlc.setConnectTimeout(1500); 
                urlc.connect();
                return (urlc.getResponseCode() == 200);
            } catch (IOException e) {
                Log.e(LOG_TAG, "Error checking internet connection", e);
            }
            return false;
        }
    }

Well the problem is that I get the error

07-23 11:09:24.310: E/(14596): java.io.IOException: Hostname 'mobile.twitter.com' was not verified
07-23 11:09:24.310: E/(14596):  at libcore.net.http.HttpConnection.verifySecureSocketHostname(HttpConnection.java:223)
07-23 11:09:24.310: E/(14596):  at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.connect(HttpsURLConnectionImpl.java:446)
07-23 11:09:24.310: E/(14596):  at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)
07-23 11:09:24.310: E/(14596):  at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)
07-23 11:09:24.310: E/(14596):  at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:81)
07-23 11:09:24.310: E/(14596):  at libcore.net.http.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:165)
07-23 11:09:24.310: E/(14596):  at com.bledi.android.twittertest.TwitterActivity$CheckInternetConnection.doInBackground(TwitterActivity.java:83)
07-23 11:09:24.310: E/(14596):  at com.bledi.android.twittertest.TwitterActivity$CheckInternetConnection.doInBackground(TwitterActivity.java:1)
07-23 11:09:24.310: E/(14596):  at android.os.AsyncTask$2.call(AsyncTask.java:287)
07-23 11:09:24.310: E/(14596):  at java.util.concurrent.FutureTask.run(FutureTask.java:234)
07-23 11:09:24.310: E/(14596):  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
07-23 11:09:24.310: E/(14596):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
07-23 11:09:24.310: E/(14596):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
07-23 11:09:24.310: E/(14596):  at java.lang.Thread.run(Thread.java:856)

How can I verify the hostname?

user3764893
  • 697
  • 5
  • 16
  • 33

1 Answers1

0

Most likely, the site uses a certificate that is not trusted by Android. This answer may help if you want to install the missing root certificate yourself: How to install trusted CA certificate on Android device?

Community
  • 1
  • 1
Henry
  • 42,982
  • 7
  • 68
  • 84