Home > Back-end >  Volley RequestQueue is not working (Kotlin)
Volley RequestQueue is not working (Kotlin)

Time:07-15

I have a LoginActivity that has a very simple layout with 2 EditText's (username and password) and 1 Button (login button).

I want to perform a POST request to a server when the login button is pressed. I am using Volley.

Here's the code

class LoginActivity : AppCompatActivity() {
    private lateinit var loginButton: Button
    private lateinit var loginNameEditText: EditText
    private lateinit var passwordEditText: EditText
    private lateinit var queue: RequestQueue

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_login)

        loginButton = findViewById(R.id.loginButton)
        loginNameEditText = findViewById(R.id.loginName)
        passwordEditText = findViewById(R.id.loginPW)

        loginButton.setOnClickListener {
            val name: String = loginNameEditText.text.toString()
            val password: String = passwordEditText.text.toString()

            if (name.isEmpty() || password.isEmpty()) {
                Toast.makeText(this, "Check your input!", Toast.LENGTH_SHORT).show()
            }
            else {
                val url = "localhost/login/name/$name/password/$password"

                queue = Volley.newRequestQueue(this)

                val stringRequest = StringRequest(Request.Method.POST,
                    url,
                    { response ->
                        println(response)
                        if (response == "0") {
                            finish()
                        }
                        else {
                            Toast.makeText(this, "Incorrect name or password!", Toast.LENGTH_SHORT).show()
                        }
                    },
                    { error -> {}}
                )

                stringRequest.setShouldCache(false)
                queue.add(stringRequest)
            }
        }
    }
}

When login is successful, the server sends "0" as response.

But no request is being made by Volley. How can I fix this?

CodePudding user response:

After checking error as Ivan suggested, I found out that the error was due to

java.io.IOException: Cleartext HTTP traffic to * not permitted

I fixed the error by adding

<application
  ...
  android:usesCleartextTraffic="true">
</application>

to AndroidManifest.xml

CodePudding user response:

Try creating a error listener like this

Response.ErrorListener responseErrorListener = new Response.ErrorListener() {
    @Override
    public void one rrorResponse(VolleyError error) {
        Log.d(MODULE, TAG   " one rrorResponse "   error.getMessage());
        VolleyLog.e("Error: ", error.getMessage());
        error.printStackTrace();
        if (error instanceof NetworkError) {
            if (error.getMessage().contains("No route to host"))
                Str_Msg = mActivity.getResources().getString(R.string.server_error);
            else if (error.getMessage().contains("Unable to resolve host"))
                Str_Msg = mActivity.getResources().getString(R.string.host_error);
            else
                Str_Msg = mActivity.getResources().getString(R.string.msg_no_internet);
        } else if (error instanceof ServerError) {
            Str_Msg = mActivity.getResources().getString(R.string.server_error);
        } else if (error instanceof AuthFailureError) {
            Str_Msg = mActivity.getResources().getString(R.string.auth_error);
        } else if (error instanceof ParseError) {
            Str_Msg = mActivity.getResources().getString(R.string.parse_error);
        } else if (error instanceof NoConnectionError) {
            Str_Msg = mActivity.getResources().getString(R.string.msg_no_internet);
        } else if (error instanceof TimeoutError) {
            Str_Msg = mActivity.getResources().getString(R.string.timeout_error);
        }
    }
};

and check for any errors

  • Related