Note that there are some explanatory texts on larger screens.

plurals
  1. POSocketTimeoutException after converting from http to https in android app
    text
    copied!<p>I am getting some problems after trying to convert my android app to use SSL to transport information between my android app and web server. (SocketTimeOutException)</p> <p>I have bought a Positive SSL certificate from a Certificate Authority (CA) and configured my server to work with it correctly. I have tested it in my web browser and its working correctly.</p> <p>Now I am trying to modify my android app to use https instead of http but as this is the first time I have used https myself, I am a little confused as to what steps I need to implement in the java code.</p> <p>Currently I am working on my settings page where I configure my app to use the correct url. For example I have 2 text fields on an activity where I enter the website url (http://www.mydomain.com) and the application folder that the relevant pages are stored in (myappfolder)</p> <p>I then use the following code to connect and test that the connection variables are configured correctly where validateurl.aspx is a webpage that returns a JSON string if the page exists:</p> <pre><code>protected boolean validateConnectionSettings() { String result = ""; boolean validated = false; try { StringBuilder urlBuilder = new StringBuilder(); urlBuilder.append(tvWebsiteURLValue.getText() + File.separator + tvApplicationMiddlewareValue.getText() + File.separator + "validateurl.aspx"); URL url = new URL(urlBuilder.toString()); URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef()); url = uri.toURL(); URLConnection urlConn = url.openConnection(); BufferedReader in = new BufferedReader( new InputStreamReader(urlConn.getInputStream())); String inputLine; while((inputLine = in.readLine()) != null){ result += inputLine; } in.close(); if(result.equals("exists")) { validated = true; } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); Log.e(TAG, e.getMessage()); } catch (Exception e) { e.printStackTrace(); Log.e(TAG, e.getMessage()); } return validated; } </code></pre> <p>Now when I try converting the (http://www.mydomain.com) variable to use https I get the above mentioned java.net.SocketTimeoutException.</p> <p>I have googled this issue and seen that I should be implementing HttpsURLConnection instead of URLConnection in the above code so I have modified my code accordingly to the following:</p> <pre><code> protected boolean validateConnectionSettings() { String result = ""; boolean validated = false; try { StringBuilder urlBuilder = new StringBuilder(); urlBuilder.append(tvWebsiteURLValue.getText() + File.separator + tvApplicationMiddlewareValue.getText() + File.separator + "validateurl.aspx"); URL url = new URL(urlBuilder.toString()); URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef()); url = uri.toURL(); // URLConnection urlConn = url.openConnection(); HttpsURLConnection urlConn = (HttpsURLConnection)url.openConnection(); BufferedReader in = new BufferedReader( new InputStreamReader(urlConn.getInputStream())); String inputLine; while((inputLine = in.readLine()) != null){ result += inputLine; } in.close(); if(result.equals("exists")) { validated = true; } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); Log.e(TAG, e.getMessage()); } catch (Exception e) { e.printStackTrace(); Log.e(TAG, e.getMessage()); } return validated; } </code></pre> <p>However I still recieve the java.net.SocketTimeoutException. Any Ideas what Im doing wrong?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload