0

I'm working on an Android app and currently stuck to link my database to my app for registration using volley but I get the problem: this is response:

com.android.volley.NoConnectionError:java.io.IOEXCEPTION: Cleartext HTTP traffic to 10.0.2.2 not permitted

Here is my MainActivity code:

public class MainActivity extends AppCompatActivity {

        Button loginBtn;
        EditText password, username;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        password = findViewById(R.id.pssword);
        username = findViewById(R.id.username);
        loginBtn = findViewById(R.id.check);

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

    void login() {
        StringRequest request = new StringRequest(Request.Method.POST, 
            "http://10.0.2.2:80/android/test.php", new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Toast.makeText(getApplicationContext(), "this is response: " + response, 
                Toast.LENGTH_SHORT).show();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(getApplicationContext(), "this is response: " + error, 
                Toast.LENGTH_SHORT).show();
            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> params = new HashMap<>();
                params.put("username", "hicham");
                params.put("password", "hello");

                return params;
            }
        };
        Volley.newRequestQueue(this).add(request);
    }
}
Dharman
  • 30,962
  • 25
  • 85
  • 135

1 Answers1

0

Please add the usesCleartextTraffic="true" tag in your AndroidManifest.xml accordingly to fix this issue.

 <application
    
    android:usesCleartextTraffic="true"

 </application>

This issue occurs because you are trying to access an http url instead of https and there are new restrictions regarding this lately. Hence, only after adding the tag I mentioned, your HTTP requests will work.

Happy coding! :)

Akshat Tiwari
  • 379
  • 2
  • 8