SSL 握手中止 - Android Azure 连接

问题描述

我正在开发一个从 Azure sql 数据库获取数据的 Android 应用。

应用在尝试获取数据时抛出异常 (sqlException)。显示错误消息是 Network error IOException: SSL handshake aborted: ssl=0x9a56a8e8: I/O error during system call,broken pipe。未获取任何数据。

但是,这仅在使用 Android 10 及更低版本时发生,并且在 Android 11 中工作并成功获取数据。

我在下面发布了我的代码

public class MainActivity extends AppCompatActivity {

    public TextView textView;
    public Button button;
    public Connection con;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = findViewById(R.id.textView);
        button = findViewById(R.id.button);

        button.setonClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CheckLogin checkLogin = new CheckLogin();
                checkLogin.execute("");
            }
        });
    }

    public class CheckLogin extends AsyncTask<String,String,String> {
        String z = "";
        Boolean isSuccess = false;
        String name1 = "";

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected void onPostExecute(String r) {
            Toast.makeText(MainActivity.this,r,Toast.LENGTH_LONG).show();
            if (isSuccess) {
                textView = findViewById(R.id.textView);
                textView.setText(name1);
            }
        }

        @Override
        protected String doInBackground(String... params) {
            try {
                con = connectionclass();
                if (con == null) {
                    z = "Check your internet access!";
                } else {
                    //String query = "select * from Users";
                    Statement stmt = con.createStatement();
                    //Check what databases are available (check if connecting to correct place
                    Log.e("Error",stmt.getConnection().getCatalog());
                    ResultSet rs = stmt.executeQuery("SELECT * FROM Users");
                    // try while???
                    if (rs.next()) {
                        name1 = rs.getString("Name"); // Name is a column in our Users table
                        z = "query successful";
                        isSuccess = true;
                        con.close();
                    } else {
                        z = "invalid query";
                        isSuccess = false;
                    }
                }
            } catch (Exception exception) {
                isSuccess = false;
                z = exception.getMessage();
                Log.d("sql error",z);
            }
            return z;
        }
    }

    @SuppressLint("NewApi")
    public Connection connectionclass() {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
        Connection connection = null;
        try {
            Class.forName("net.sourceforge.jtds.jdbc.Driver");

            String hostName = "azuretestappandroid";
            String dbname = "AppTestingDatabase";
            String user = "Tester@azuretestappandroid";
            String userPwd = "XXX";
            String connectionURL = String.format("jdbc:jtds:sqlserver://%s.database.windows.net:1433/%s;user=%s;encrypt=true;hostNameInCertificate=*.database.windows.net;loginTimeout=30;ssl=required",hostName,dbname,user,userPwd);

            connection = DriverManager.getConnection(connectionURL,userPwd);
        } catch (sqlException exception) {
            Log.e("error here 1: ",exception.getMessage());
        } catch (ClassNotFoundException exception) {
            Log.e("error here 2: ",exception.getMessage());
        } catch (Exception exception) {
            Log.e("error here 3: ",exception.getMessage());
        }
        return connection;
    }
}

我知道 jtds 库已经过时,所以如果有关于如何读取和写入数据到 Azure sql 数据库的任何建议,我们将不胜感激。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...