我正在尝试让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";
}
如何解决这个问题?有没有更好的办法?
2条答案
按热度按时间pzfprimi1#
第一行应该是:
<uses-permission android:name="android.permission.INTERNET" />
我认为您正在android模拟器中运行应用程序,并且希望连接到在您的主机上运行的sql服务器。从android仿真器连接到运行仿真器的机器时,需要使用ip
10.0.2.2
你可以在这里找到更多信息htzpubme2#
第一个许可
<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或更高版本,则需要添加旧版存储权限
android:requestLegacyExternalStorage="true"
在<application/>
选项卡。有关更多信息,请单击此处。