how to solve java.net.SocketException: socket failed: EPERM (Operation not permitted)

ε祈祈猫儿з 提交于 2020-07-10 03:38:05

问题


When I was using my Android Studio to create a simple registration to my local database I met the problem that shows on the title, and here is my code:

public class registerInterface extends AppCompatActivity {
    private EditText editTextUsername, editTextPassword, editTextEmail;
    private Button buttonRegister, backToLogin;
    private ProgressDialog progressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.registration_interface);
        editTextUsername = findViewById(R.id.username);
        editTextPassword = findViewById(R.id.password);
        editTextEmail = findViewById(R.id.email);

        buttonRegister = findViewById(R.id.register);
        backToLogin = findViewById(R.id.go_back_login);

        progressDialog = new ProgressDialog(this);

        buttonRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                registerUser();
            }
        });

    }

    public void registerUser(){
        final String email = editTextEmail.getText().toString().trim();
        final String password = editTextPassword.getText().toString().trim();
        final String username = editTextUsername.getText().toString().trim();

        progressDialog.setMessage("Registering...");
        progressDialog.show();

        StringRequest stringRequest = new StringRequest(Request.Method.POST, DataBaseConstants.URL_REGISTER, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                progressDialog.dismiss();
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    Toast.makeText(getApplicationContext(), jsonObject.getString("message"), Toast.LENGTH_LONG).show();
                } catch (JSONException e){
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                progressDialog.hide();
                Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();

            }
        }){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> params = new HashMap<>();
                params.put("username", username);
                params.put("email", email);
                params.put("password", password);
                return params;
            }
        };

        RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.add(stringRequest);
    }
}

I have already add these in my AndroidManifest.xml, but still does not work:

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

I don't if it is the problem about the IP address, but I just cannot figure it out.


回答1:


First

Just uninstall the app from the emulator then run again and it’ll work I had the same issue.

Sometime this issue occur when problem with emulator.

Secondly -

May be problem is with your network, the wifi you are connected with in emulator is not allowing you to connect to the api you are calling.




回答2:


Hi and I have found the solution. It's the matter of the virtual device, in my version API 29 the connection of network is forbidden. I recommend using the third party virtual device and now I'm using 'NetEase MUMU' and if you meet similar question you can check it out.



来源:https://stackoverflow.com/questions/60203194/how-to-solve-java-net-socketexception-socket-failed-eperm-operation-not-permi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!