4

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.

4 Answers 4

11

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks but I have already try this way, but still not working...
I think then the problem is with your network The wifi you are connected in emulator is not allowing you to connect to the api you are calling.
Could you please show me more details about what you said? I have no idea what's wrong with my wifi setting in my emulator.
uninstall and install again app worked for me. Thanks @DipankarBaghel
Emulator was the problem for me
0

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.

Comments

0

Uninstalling the app from the emulator didn't do the trick. You have to kill the emulator first, uninstall the app, and build it again. At least, that's what's worked for me.

Comments

0

You can definitely solve this problem like I did. Follow the steps below:

  1. Create a duplicate of the emulator.
  2. Delete the first emulator.
  3. Restart Android Studio.

And your problem should be solved.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.