简体   繁体   English

未解决的 java.net.SocketException:套接字失败:EPERM(不允许操作)

[英]Un-resolved java.net.SocketException: socket failed: EPERM (Operation not permitted)

I am following a tutorial on connecting my android app to mySQL server.我正在关注将我的 android 应用程序连接到 mySQL 服务器的教程。 I am currently encountering an error that other post on StackOverflow cant solve, at least not for me.我目前遇到 StackOverflow 上的其他帖子无法解决的错误,至少对我来说不是。

The error is "java.net.SocketException: socket failed: EPERM (Operation not permitted)" which shows up both on the emulator and my physical device.错误是“java.net.SocketException: socket failed: EPERM (Operation not allowed)”,它同时出现在模拟器和我的物理设备上。 Other relating topics on stackoverflow has suggested that I add the lines stackoverflow 上的其他相关主题建议我添加这些行

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

to my AndroidManifest.xml then un-installing the application, to which I have but the error still persist.到我的 AndroidManifest.xml 然后卸载我有的应用程序,但错误仍然存在。

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>

SignUpActivity class注册活动 class

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;
        }
    }

}

Logcat日志猫

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

I would appreciate any assistance in solving this matter.我将不胜感激任何帮助解决这个问题。

As requested, the php signup used to connect the android app to the mySQL server根据要求,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";
?>

database config file数据库配置文件

<?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';

    }
}

?>

Looking at my own code, I spelt android wrong in the Manifest.xml file查看我自己的代码,我在 Manifest.xml 文件中拼写错误 android

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

correction更正

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.net.SocketException:套接字失败:EPERM(不允许操作) - java.net.SocketException: socket failed: EPERM (Operation not permitted) Android Java 抛出 java.net.SocketException:套接字失败:EPERM(不允许操作) - Android Java throws java.net.SocketException: socket failed: EPERM (Operation not permitted) java.net.SocketException:套接字失败:EPERM 操作不允许,尽管 AndroidManifest 中有许可 - java.net.SocketException: socket failed: EPERM Operation not permitted although AndroidManifest have permits in it Android java.net.SocketException:套接字失败 - Android java.net.SocketException: socket failed java.net.SocketException:套接字失败:EACCES(权限被拒绝) - java.net.SocketException: socket failed: EACCES (Permission denied) java.net.SocketException:sendto失败:EPIPE(断开的管道)发送字符以检查套接字连接时 - java.net.SocketException: sendto failed: EPIPE (Broken pipe) When sending character to check socket connection java.net.SocketException:套接字失败:EACCES(权限被拒绝)不能通过uses-permission解决 - java.net.SocketException: socket failed: EACCES (Permission denied) not solved by uses-permission 将信息发送到服务器java.net.SocketException:套接字失败:EACCES(权限被拒绝) - Sending information to server, java.net.SocketException: socket failed: EACCES (Permission denied) createServerSocket返回java.net.SocketException:套接字失败:EACCES(权限被拒绝) - createServerSocket returning java.net.SocketException: socket failed: EACCES (Permission denied) Android:套接字-java.net.SocketException:sendto失败:EPIPE(管道断开) - Android : Socket - java.net.SocketException: sendto failed: EPIPE (Broken pipe)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM