我使用Volley庫和意外的響應代碼400.我試圖從用戶名和密碼登錄,但面臨com.android.volley.ServerError.How可以解決這個問題嗎?意外的響應代碼400?
登錄類
public class Login extends AppCompatActivity implements View.OnClickListener {
EditText userName, Password;
Button login;
public static final String LOGIN_URL = "http://192.168.0.106:84/Token";
public static final String KEY_USERNAME = "UserName";
public static final String KEY_PASSWORD = "Password";
String username, password;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
userName = (EditText) findViewById(R.id.login_name);
Password = (EditText) findViewById(R.id.login_password);
userName.setHint(Html.fromHtml("<font color='#008b8b' style='italic'>Username</font>"));
Password.setHint(Html.fromHtml("<font color='#008b8b'>Password</font>"));
login = (Button) findViewById(R.id.login);
login.setOnClickListener(this);
}
private void UserLogin() {
username = userName.getText().toString().trim();
password = Password.getText().toString().trim();
StringRequest stringRequest = new StringRequest(com.android.volley.Request.Method.POST, LOGIN_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
if (response.trim().equals("success")) {
openProfile();
} else {
Toast.makeText(Login.this, response, Toast.LENGTH_LONG).show();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(Login.this, error.toString(), Toast.LENGTH_LONG).show();
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> params = new HashMap<String, String>();
params.put("Content-Type", "application/x-www-form-urlencoded");
params.put("Charset", "UTF-8");
return params;
}
@Override
public Map<String, String> getParams() throws AuthFailureError {
HashMap<String, String> map = new HashMap<String, String>();
map.put(KEY_USERNAME, username);
map.put(KEY_PASSWORD, password);
return map;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
private void openProfile() {
Intent intent = new Intent(this, Home.class);
intent.putExtra(KEY_USERNAME, username);
startActivity(intent);
}
@Override
public void onClick(View v) {
UserLogin();
}
public String getBodyContentType() {
return "application/xml";
}
}
如何這個問題可以解決?
刪除標題後,我得到了這個。 [![在這裏輸入的形象描述] [1] [1]
無從得知沒有看到服務器的詳細信息錯誤。儘管如此,您仍將頭添加到參數中,這肯定是錯誤的。 –
錯誤代碼400意味着錯誤的請求。檢查你的服務器端需要什麼類型的數據。 – Piyush
@Robby getHeaders()方法也會給出相同的錯誤。 – seon