如何解决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 举报,一经查实,本站将立刻删除。