1

when i click login button, that alert will show up, i use com.android.volley:volley:1.1.1 and for api, i use restframework from python

when i click login, that alert will come

this is my mainactivity.java code

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    etUsername = (EditText) findViewById(R.id.etUsername);
    etPassword = (EditText) findViewById(R.id.etPassword);
    btnLogin = (Button) findViewById(R.id.btnLogin);
    btnLogin.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View arg0){
            username = etUsername.getText().toString();
            password = etPassword.getText().toString();
            RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
            String urlLogin = "http://10.0.3.2/api/accounts";
            StringRequest stringRequest = new StringRequest(Request.Method.POST, urlLogin, new Response.Listener<String>(){
                @Override
                public void onResponse(String response){
                    try {
                        JSONObject jsonObject = new JSONObject(response);
                        boolean validasiLogin = jsonObject.getBoolean("success");
                        Toast.makeText(MainActivity.this, validasiLogin+"", Toast.LENGTH_SHORT).show();
                        if (validasiLogin) {
                            String jsonUsername = jsonObject.getString("username");
                            Toast.makeText(MainActivity.this, "Username : "+jsonUsername, Toast.LENGTH_LONG).show();
                            Intent bukaIntent = new Intent(MainActivity.this, HomeActivity.class);
                            startActivity(bukaIntent);
                        }else {
                            Toast.makeText(MainActivity.this, "Username / Password Salah", Toast.LENGTH_SHORT).show();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError arg0){
                    Toast.makeText(MainActivity.this, arg0+"", Toast.LENGTH_SHORT).show();
                }
            }) {
                @Override
                protected Map<String, String> getParams() throws AuthFailureError {
                    Map<String, String> params = new HashMap<String, String>();
                    params.put("username", username);
                    params.put("password", password);
                    return params;
                }
            };
            requestQueue.add(stringRequest);
        }
    });
}
}

how to fix it? can u show what wrong with my code pliss?

Naufal
  • 89
  • 2
  • 8

2 Answers2

0

Try to debug and see if arg0 in public void onErrorResponse(VolleyError arg0) contains any useful information. Or you can try to log error response body like here: https://stackoverflow.com/a/30722575/5148282

otis_d
  • 71
  • 7
  • now it show `com.google.android.material.button.materialbutton(fe30b5d` – Naufal Mar 02 '21 at 11:11
  • @Naufal Add whole stack trace here it will be more helpful. Also check internet permission in manifest. – Gevaria Purva Mar 02 '21 at 11:15
  • @GevariaPurva i got this messege from run `E/Surface: getSlotFromBufferLocked: unknown buffer: 0xee8f2460 ` btw i use android 6.0 – Naufal Mar 02 '21 at 11:26
  • @Naufal try to print stack trace on onErrorResponse . add error.getMessage() and post it here. And debug first are you getting error toast or not. – Gevaria Purva Mar 02 '21 at 11:28
0

maybe it will help you

fun onErrorResponse(error: VolleyError?) {
        if (error == null || error.networkResponse == null) {
            return
        }
        val body: String
        //get status code here
        val statusCode: String = java.lang.String.valueOf(error.networkResponse.statusCode)
        //get response body and parse with appropriate encoding
        try {
            val UTF_8: Charset = Charset.forName("UTF-8")
            body = String(error.networkResponse.data, UTF_8)
        } catch (e: UnsupportedEncodingException) {
            // exception
        }

        //do stuff with the body
    }