5

My code works fine when it is run in Android 5.0 and up. But in Android 4.1.1 it throws java.net.SocketTimeoutException: SSL handshake timed out.

    URL url;
    HttpURLConnection connection = null; 
    String charset = "UTF-8";

    String accessToken = "";

    try {

        ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
        postParameters.add(new BasicNameValuePair("client_id", CLIENT_ID));
        postParameters.add(new BasicNameValuePair("client_secret", CLIENT_SECRET));
        postParameters.add(new BasicNameValuePair("grant_type", CLIENT_CREDENTIALS_GRANT_TYPE));

        //Create connection
        url = new URL(ACCESS_TOKEN_URL);
        connection = (HttpURLConnection)url.openConnection();
        connection.setReadTimeout( 10000 /*milliseconds*/ );
        connection.setConnectTimeout( 15000 /* milliseconds */ );
        connection.setInstanceFollowRedirects(false);
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=" + charset);
        connection.setUseCaches(false);
        connection.setDoInput(true);
        connection.setDoOutput(true);

        //Send request
        DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
        wr.write(getQuery(postParameters).getBytes(charset));
        wr.flush();
        wr.close();

        int responseCode = connection.getResponseCode();
        InputStream is = null;

        if(responseCode==HttpStatus.SC_OK) {
            is = connection.getInputStream();
        }
        else{
            is = connection.getErrorStream();
        }

        Log.d(TAG, "responseCode: "+responseCode);

        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();
        String line = null;

        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }

        is.close();
        reader.close();

        String jsonResult = sb.toString();
        JSONObject jsonObject = new JSONObject(jsonResult);

        if(responseCode==HttpStatus.SC_OK){
            accessToken = jsonObject.getString("access_token");
            SettingsPreference.setAccessToken(accessToken);
            Log.d(TAG, "access token: "+accessToken);
        }
        else {
            accessToken = null;
        }

    } catch (Exception e) {
        e.printStackTrace();
    } finally {

        if(connection != null) {
          connection.disconnect(); 
        }
    }

I already done a lot of research and it is quite frustrating. What did I go wrong? Is this a server issue? Your help is greatly appreciated.

5
  • I had similar issues with URL connection on 4.1.1 version, mine was EOF while it worked perfectly on other versions. I remember this post helped me realize what the problem was stackoverflow.com/a/12643663/1865583 . But i dont think the soruce files are available any more. Anyway what helped fix the issue was switching to DefaultHttp client which worked on all versions...thats the fastest solution i would recommend to you: Try using a network library/wrapper such as DefaultHttp or Android client or OkHttp ...
    – JanBo
    Jun 17, 2015 at 11:40
  • Does your phone with 4.1.1 have the correct time? SSL doesn't work when client and server differ in time. (And of course date!)
    – hgoebl
    Jun 17, 2015 at 12:22
  • looks like it's a bug see here and here you can try to workaround it; I had a similar issues but was specific to Samsung galaxy tab, is it your case?
    – Paizo
    Jun 17, 2015 at 12:25
  • @Paizo not just in Samsung device but in Genymotion as well. Jun 17, 2015 at 12:42
  • It might help if you give the URL to see if there is something special with the host you are trying to access. Might be related to ciphers, protocol version, SNI, middleboxes in between... Jun 17, 2015 at 12:51

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.