我正在学习如何将我的安卓应用程序连接到mySQL服务器。我目前遇到了一个StackOverflow上的其他帖子无法解决的错误,至少对我来说是这样。
错误为"java. net.套接字异常:套接字失败:EPERM(不允许操作)",它同时出现在仿真器和我的物理设备上。
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
到我的AndroidManifest.xml,然后卸载应用程序,我有,但错误仍然存在.
AndroidManifest.xml
<uses-permission android:name="andriod.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_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:usesCleartextTraffic="true"
android:theme="@style/Theme.Login2">
<activity
android:name=".SignUpActivity"
android:exported="false"
android:theme="@style/Theme.Design.Light.NoActionBar"/>
<activity
android:name=".ui.login.LoginActivity"
android:exported="false"
android:theme="@style/Theme.Design.Light.NoActionBar" />
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
注册活动类
package com.example.login2;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
public class SignUpActivity extends AppCompatActivity {
// EditText inputFullName, inputUserName, inputEmail, inputPassword;
//Button buttonSignIn, buttonLogIn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_up);
//Button signUpButton = findViewById(R.id.btn1Signup);
EditText inputFullName = findViewById(R.id.fullname);
EditText inputEmail = findViewById(R.id.email);
EditText inputPassword = findViewById(R.id.password2);
EditText inputUserName = findViewById(R.id.username2);
Button buttonLogIn = findViewById(R.id.button5);
Button buttonSignIn = (Button) findViewById(R.id.button8);
buttonSignIn.setOnClickListener(view -> {
// Intent intent = new Intent(this, MainActivity.class); //command go to another page
// startActivity(intent);//start activity
String fullname, username, email, password;
fullname = String.valueOf(inputFullName.getText());
username = String.valueOf(inputUserName.getText());
email = String.valueOf(inputEmail.getText());
password = String.valueOf(inputPassword.getText());
if(!fullname.equals("") && !username.equals("") && !email.equals("") && !password.equals("")) {
//Start ProgressBar first (Set visibility VISIBLE)
Handler handler = new Handler(Looper.getMainLooper());
handler.post(() -> {
//Starting Write and Read data with URL
//Creating array for parameters
String[] field = new String[4];
field[0] = "fullname";
field[1] = "username";
field[2] = "email";
field[3] = "password";
//Creating array for data
String[] data = new String[4];
field[0] = fullname;
field[1] = username;
field[2] = email;
field[3] = password;
PutData putData = new PutData("https://192.168.100.2/LoginRegister/signup.php", "POST", field, data);
if (putData.startPut()) {
if (putData.onComplete()) {
String result = putData.getResult();
if(result.equals("Sign Up Success")){
Toast.makeText(getApplicationContext(), result, Toast.LENGTH_SHORT).show();
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
finish();
}else{
Toast.makeText(getApplicationContext(), result, Toast.LENGTH_SHORT).show();
}
}
}
//End Write and Read data with URL
});
}
else {
Toast.makeText(this, "All fields required", Toast.LENGTH_SHORT).show();
}
});
}
public class PutData extends Thread {
private String url, method;
String result_data = "Empty";
String[] data, field;
public PutData(String url, String method, String[] field, String[] data) {
this.url = url;
this.method = method;
this.data = new String[data.length];
this.field = new String[field.length];
System.arraycopy(field, 0, this.field, 0, field.length);
System.arraycopy(data, 0, this.data, 0, data.length);
}
@Override
public void run() {
try {
String UTF8 = "UTF-8", iso = "iso-8859-1";
URL url = new URL(this.url);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod(this.method);
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, UTF8));
StringBuilder post_data = new StringBuilder();
for (int i = 0; i < this.field.length; i++) {
post_data.append(URLEncoder.encode(this.field[i], "UTF-8")).append("=").append(URLEncoder.encode(this.data[i], UTF8)).append("&");
}
bufferedWriter.write(post_data.toString());
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, iso));
StringBuilder result = new StringBuilder();
String result_line;
while ((result_line = bufferedReader.readLine()) != null) {
result.append(result_line);
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
setData(result.toString());
} catch (IOException e) {
setData(e.toString());
}
}
public boolean startPut() {
PutData.this.start();
return true;
}
public boolean onComplete() {
while (true) {
if (!this.isAlive()) {
return true;
}
}
}
public String getResult() {
return this.getData();
}
public void setData(String result_data) {
this.result_data = result_data;
}
public String getData() {
return result_data;
}
}
}
对数表
2022-01-07 11:30:24.055 11687-11687/? I/.example.login: Late-enabling -Xcheck:jni
2022-01-07 11:30:24.068 11687-11687/? I/.example.login: Unquickening 12 vdex files!
2022-01-07 11:30:24.069 11687-11687/? W/.example.login: Unexpected CPU variant for X86 using defaults: x86
2022-01-07 11:30:24.236 11687-11687/com.example.login2 D/NetworkSecurityConfig: No Network Security Config specified, using platform default
2022-01-07 11:30:24.237 11687-11687/com.example.login2 D/NetworkSecurityConfig: No Network Security Config specified, using platform default
2022-01-07 11:30:24.261 11687-11711/com.example.login2 D/libEGL: loaded /vendor/lib/egl/libEGL_emulation.so
2022-01-07 11:30:24.263 11687-11711/com.example.login2 D/libEGL: loaded /vendor/lib/egl/libGLESv1_CM_emulation.so
2022-01-07 11:30:24.267 11687-11711/com.example.login2 D/libEGL: loaded /vendor/lib/egl/libGLESv2_emulation.so
2022-01-07 11:30:24.389 11687-11687/com.example.login2 W/.example.login: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (greylist, reflection, allowed)
2022-01-07 11:30:24.389 11687-11687/com.example.login2 W/.example.login: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (greylist, reflection, allowed)
2022-01-07 11:30:24.500 11687-11709/com.example.login2 D/HostConnection: HostConnection::get() New Host Connection established 0xf75ec230, tid 11709
2022-01-07 11:30:24.507 11687-11709/com.example.login2 D/HostConnection: HostComposition ext ANDROID_EMU_CHECKSUM_HELPER_v1 ANDROID_EMU_native_sync_v2 ANDROID_EMU_native_sync_v3 ANDROID_EMU_native_sync_v4 ANDROID_EMU_dma_v1 ANDROID_EMU_direct_mem ANDROID_EMU_host_composition_v1 ANDROID_EMU_host_composition_v2 ANDROID_EMU_vulkan ANDROID_EMU_deferred_vulkan_commands ANDROID_EMU_vulkan_null_optional_strings ANDROID_EMU_vulkan_create_resources_with_requirements ANDROID_EMU_YUV_Cache ANDROID_EMU_vulkan_ignored_handles ANDROID_EMU_has_shared_slots_host_memory_allocator ANDROID_EMU_vulkan_free_memory_sync ANDROID_EMU_vulkan_shader_float16_int8 ANDROID_EMU_vulkan_async_queue_submit ANDROID_EMU_sync_buffer_data ANDROID_EMU_read_color_buffer_dma GL_OES_vertex_array_object GL_KHR_texture_compression_astc_ldr ANDROID_EMU_host_side_tracing ANDROID_EMU_gles_max_version_2
2022-01-07 11:30:24.508 11687-11709/com.example.login2 W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
2022-01-07 11:30:24.537 11687-11709/com.example.login2 D/EGL_emulation: eglCreateContext: 0xf75ec1c0: maj 2 min 0 rcv 2
2022-01-07 11:30:24.565 11687-11709/com.example.login2 D/EGL_emulation: eglMakeCurrent: 0xf75ec1c0: ver 2 0 (tinfo 0xf7931630) (first time)
2022-01-07 11:30:24.571 11687-11709/com.example.login2 I/Gralloc4: mapper 4.x is not supported
2022-01-07 11:30:24.572 11687-11709/com.example.login2 D/HostConnection: createUnique: call
2022-01-07 11:30:24.572 11687-11709/com.example.login2 D/HostConnection: HostConnection::get() New Host Connection established 0xf75eb580, tid 11709
2022-01-07 11:30:24.572 11687-11709/com.example.login2 D/goldfish-address-space: allocate: Ask for block of size 0x100
2022-01-07 11:30:24.572 11687-11709/com.example.login2 D/goldfish-address-space: allocate: ioctl allocate returned offset 0x3dfffe000 size 0x2000
2022-01-07 11:30:24.595 11687-11709/com.example.login2 D/HostConnection: HostComposition ext ANDROID_EMU_CHECKSUM_HELPER_v1 ANDROID_EMU_native_sync_v2 ANDROID_EMU_native_sync_v3 ANDROID_EMU_native_sync_v4 ANDROID_EMU_dma_v1 ANDROID_EMU_direct_mem ANDROID_EMU_host_composition_v1 ANDROID_EMU_host_composition_v2 ANDROID_EMU_vulkan ANDROID_EMU_deferred_vulkan_commands ANDROID_EMU_vulkan_null_optional_strings ANDROID_EMU_vulkan_create_resources_with_requirements ANDROID_EMU_YUV_Cache ANDROID_EMU_vulkan_ignored_handles ANDROID_EMU_has_shared_slots_host_memory_allocator ANDROID_EMU_vulkan_free_memory_sync ANDROID_EMU_vulkan_shader_float16_int8 ANDROID_EMU_vulkan_async_queue_submit ANDROID_EMU_sync_buffer_data ANDROID_EMU_read_color_buffer_dma GL_OES_vertex_array_object GL_KHR_texture_compression_astc_ldr ANDROID_EMU_host_side_tracing ANDROID_EMU_gles_max_version_2
2022-01-07 11:30:26.557 11687-11687/com.example.login2 I/AssistStructure: Flattened final assist data: 2276 bytes, containing 1 windows, 11 views
2022-01-07 11:30:33.555 11687-11687/com.example.login2 D/CompatibilityChangeReporter: Compat change id reported: 147798919; UID 10128; state: ENABLED
我希望能得到任何帮助来解决这个问题。
根据请求,php注册用于将android应用程序连接到mySQL服务器
<?php
require "DataBase.php";
$db = new DataBase();
if (isset($_POST['fullname']) && isset($_POST['email']) && isset($_POST['username']) && isset($_POST['password'])) {
if ($db->dbConnect()) {
if ($db->signUp("users", $_POST['fullname'], $_POST['email'], $_POST['username'], $_POST['password'])) {
echo "Sign Up Success";
} else echo "Sign up Failed";
} else echo "Error: Database connection";
} else echo "All fields are required";
?>
数据库配置文件
<?php
class DataBaseConfig
{
public $servername;
public $username;
public $password;
public $databasename;
public function __construct()
{
$this->servername = '%';
$this->username = 'test1';
$this->password = 'n8wOs3tx@3d!TGJl';
$this->databasename = 'test';
}
}
?>
2条答案
按热度按时间lo8azlld1#
查看我自己的代码,我在Manifest.xml文件中拼错了android
改正
t98cgbkg2#
在模拟器中执行“擦除数据”可以解决这个问题。