当我点击登录按钮时,警报将显示,我使用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你能指出我代码有什么问题吗?
2条答案
按热度按时间6jygbczu1#
尝试调试并查看
public void onErrorResponse(VolleyError arg0)
中的arg0是否包含任何有用的信息。或者您可以尝试记录错误响应主体,如下所示:https://stackoverflow.com/a/30722575/5148282s8vozzvw2#
也许能帮到你