java.net.SocketException:套接字失败:Android Studio中的EACCES(权限被拒绝)



我正在尝试让Android应用程序与MySQL数据库进行通信。即使在清单中设置了正确的权限,我也会收到以下错误:

java.net.SocketException:套接字失败:Android Studio 中的EACCES(权限被拒绝(

这是AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.biddingapp">
<uses-configuration android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".SecondActivity"></activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

这是MainActivity.java

public class MainActivity extends AppCompatActivity {
private EditText username;
private EditText password;
private Button login;
Connection con;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
username = (EditText)findViewById(R.id.username);
password = (EditText)findViewById(R.id.password);
login = (Button)findViewById(R.id.login);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new MainActivity.checkLogin().execute("");
}
});
}
private class checkLogin extends AsyncTask<String, String, String>{
String str = null;
Boolean isSuccess = false;
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
}
@Override
protected String doInBackground(String... strings) {
con = connectionClass(ConnectionClass.user.toString(),ConnectionClass.password.toString(),ConnectionClass.database.toString(),ConnectionClass.server.toString());
if(con == null){
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this,"Check Internet Connection",Toast.LENGTH_LONG).show();
}
});
str = "On Internet Connection";
}
else {
try {
String sql = "SELECT * FROM users WHERE username = '" + username.getText() + "' AND password = '" + password.getText() + "' ";
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(sql);
if (rs.next()) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "Login Success", Toast.LENGTH_LONG).show();
}
});
str = "Success";
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
finish();
} else {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "Check email or password", Toast.LENGTH_LONG).show();
}
});
username.setText("");
password.setText("");
}
} catch (Exception e) {
isSuccess = false;
Log.e("SQL Error : ", e.getMessage());
}
}
return str;
}
}
@SuppressLint("NewApi")
public Connection connectionClass(String user, String password, String database, String server){
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
Connection connection = null;
String connectionURL = null;
try{
Class.forName("com.mysql.jdbc.Driver");
connectionURL = "jdbc:mysql://localhost/biddingapp://" + server+"/" + database + ";user=" + user + ";password=" + password + ";";
connection = DriverManager.getConnection(connectionURL);
}catch (Exception e){
Log.e("SQL Connection Error : ", e.getMessage());
}
return connection;
}
}

这是ConnectionClass.java

package com.example.biddingapp;
import android.annotation.SuppressLint;
import android.os.StrictMode;
import android.util.Log;
import java.sql.Connection;
import java.sql.DriverManager;
public class ConnectionClass {
public static String user = "androidstudio";
public static String password = "program";
public static String database = "users";
public static String server = "localhost";
}

我该如何解决这个问题?有更好的方法吗?

第一行应该是:<uses-permission android:name="android.permission.INTERNET" />

我认为您正在android模拟器中运行该应用程序,并希望连接到主机上运行的sql服务器。

从Android模拟器连接到模拟器运行的机器,您需要使用IP10.0.2.2

更多信息,你可以在这里找到

  • 1st权限<uses-permission android:name="android.permission.INTERNET" />假设是这样的,

  • 第二,如何访问数据库,如果使用本地存储,则需要<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /><uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

  • 您还需要具有运行时权限。

  • 第三,如果您使用的是Android 10或更高版本,您需要在<application/>选项卡下添加传统存储权限android:requestLegacyExternalStorage="true"

有关详细信息,请单击此处。

您还需要提到端口。

MySQL在本地机器上的3306端口上运行。

执行以下操作:

connectionURL = "jdbc:mysql://localhost:3306/biddingapp://" + server+"/" + database + ";user=" + user + ";password=" + password + ";";

此外,需要更正互联网权限

<uses-permission android:name="android.permission.INTERNET" />

相关内容

  • 没有找到相关文章