如何修复在Android Studio中的com.android.volley.ClientError

vtwuwzda  于 2023-02-19  发布在  Android
关注(0)|答案(2)|浏览(224)

当我点击登录按钮时,警报将显示,我使用com.android.volley:volley:1.1.1,对于API,我使用来自Python的restframework

这是我的mainactivity.java网址

@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);
        }
    });
}
}

如何修复?2你能指出我代码有什么问题吗?

6jygbczu

6jygbczu1#

尝试调试并查看public void onErrorResponse(VolleyError arg0)中的arg0是否包含任何有用的信息。或者您可以尝试记录错误响应主体,如下所示:https://stackoverflow.com/a/30722575/5148282

s8vozzvw

s8vozzvw2#

也许能帮到你

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
    }

相关问题