1

I am getting

javax.net.ssl.SSLHandshakeException: Connection closed by peer

while calling SOAP webservice using KSOAP2 (ksoap2_android_2.6.4) in Android 7.0 Nougat. I have refereed all the below questions

“Connection closed by peer” error occurs in Android 7.0 Nougat while connecting to SHA256 CA installed Windows 2003 Server SP2 through HTTPS

Android 7.0 : 'javax.net.ssl.SSLHandshakeException: Connection closed by peer

Below is my code for calling webservice usiong KSOAP

public class ValidateUserAsyncTask extends AsyncTask<String, Integer, Void> {
    ProgressDialog dialog = new ProgressDialog(LoginActivity.this);
    String tempAction = "";

    @Override
    protected void onPreExecute() {
        dialog.setMessage("Validating User...");
        dialog.setCancelable(true);
        dialog.show();
        super.onPreExecute();
    }

    @Override
    protected Void doInBackground(String... strings) {

        try {
            if (SplashScreenActivity.checkNetworkConnection(getApplicationContext()) == true) {
                Log.d("", "host is reachable");
                callingValidateUser(strings[0], strings[1], strings[2], strings[3], strings[4]);

            } else {
                fault = true;
                strFault = "Please Check your Internet Connection!";
            }

        } catch (Exception e) {
            Log.d("Connection error", "Connection Error in call service");
            e.printStackTrace();
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        try {

            dialog.dismiss();
            if (SplashScreenActivity.checkNetworkConnection(getApplicationContext()) == true) {

                ParsingForValidateUser(envelope_Actions);
            }
            if (fault == true) {
                if (strFault.contains("User Name/Password is not valid")) {
                    editor = setPref.edit();
                    editor.putBoolean("flagValidatedUser", false);
                    editor.commit();
                }
                if(strFault.contains("Contact to Hr Or Submit Your Mobile Number"))
                {
                    fault = false;
                    strFault = "";
                    Intent i = new Intent(getApplication(), NoMobileRegisterActivity.class);
                    startActivity(i);

                }
                else
                {
                    Toast.makeText(getApplicationContext(), "" + strFault, Toast.LENGTH_LONG).show();
                    fault = false;
                    strFault = "";
                }

            }

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

    }

    public void callingValidateUser(String userName, String password, String nameSpace, String methodName, String url) {

        androidHttpTransport = new HttpTransportSE(url, 60000);
        SoapObject request = new SoapObject(nameSpace, methodName);

        try {
            password = AESencrp.encrypt(password);
        } catch (Exception e1) {
            e1.printStackTrace();
        }

        request.addProperty("UserName", userName);
        request.addProperty("Password", password);

        Log.d("UserName", "" + userName);
        Log.d("password", "" + password);
        Log.d("nameSpace", "" + nameSpace);
        Log.d("methodName", "" + methodName);
        Log.d("url", "" + url);

        Element[] header = new Element[1];
        header[0] = new Element().createElement("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", "Security");
        header[0].setAttribute(null, "mustUnderstand", "1");

        Element usernametoken = new Element().createElement("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", "UsernameToken");
        usernametoken.setAttribute(null, "Id", "UsernameToken-1");
        header[0].addChild(Node.ELEMENT, usernametoken);

        Element username = new Element().createElement(null, "n0:Username");
        username.addChild(Node.IGNORABLE_WHITESPACE, userName);
        usernametoken.addChild(Node.ELEMENT, username);

        Element pass = new Element().createElement(null, "n0:Password");
        pass.setAttribute(null, "Type", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText");
        pass.addChild(Node.TEXT, password);
        usernametoken.addChild(Node.ELEMENT, pass);

        envelope_Actions.headerOut = header;
        envelope_Actions.dotNet = true;
        envelope_Actions.bodyOut = request;

        envelope_Actions.setOutputSoapObject(request);
        try {
            androidHttpTransport.debug = true;
            androidHttpTransport.call("process", envelope_Actions); // Calling
        } catch (SoapFault e) {
            Log.d("", "SoapFault ERROR in call worklist type");
            e.printStackTrace();
        } catch (XmlPullParserException e) {
            Log.d("", "XmlPullParserException ERROR in call worklist type");
            e.printStackTrace();
        } catch (UnknownHostException e) {
            Log.d("", "UnknownHostException ERROR in call worklist type");
            e.printStackTrace();

        } catch (MalformedURLException e) {
            Log.d("", "MalformedURLException ERROR in call worklist type");
            e.printStackTrace();

        } catch (SocketTimeoutException e) {
            fault = true;
            strFault = "Request Time Out";
            Log.d("", "SocketTimeoutException ERROR in call worklist type");
            e.printStackTrace();

        } catch (IOException e) {
            Log.d("", "IOException ERROR in call worklist type");
            e.printStackTrace();
        }

    }

    public void ParsingForValidateUser(SoapSerializationEnvelope envelope) {

        SoapObject result;
        db = dbHelper.getWritableDatabase();
        try {
            result = (SoapObject) envelope.getResponse();
        } catch (NullPointerException e) {
            e.printStackTrace();
        } catch (ClassCastException e) {
            result = (SoapObject) envelope.bodyIn;
            String res = result.getPropertyAsString(0).toString();
        } catch (SoapFault e) {
            e.printStackTrace();
        } 

    }

}

I've asked the person who handle server config. to check whether TLS is enable or not? waiting for his reply. also I've created network_security_config.xml as below

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
       <base-config>
           <trust-anchors>
               <certificates src="system"/>
           </trust-anchors>
       </base-config>
</network-security-config>

and declared in Menifest.xml

android:networkSecurityConfig="@xml/network_security_config"

I have also tried by putting certificate in PEM formate in raw folder but can not figure out this issue.

kindly guide me where I am doing mistake ?

MDroid
  • 550
  • 4
  • 22

0 Answers0