意外响应代码400截击android
我写的代码是将我的android studio连接到wamp服务器mysql。我尝试从mysql到android检索登录信息。然而,它不断向我显示意外的响应代码400。我应该如何更改代码意外响应代码400截击android,android,mysql,Android,Mysql,我写的代码是将我的android studio连接到wamp服务器mysql。我尝试从mysql到android检索登录信息。然而,它不断向我显示意外的响应代码400。我应该如何更改代码 @Override protected void onResume() { super.onResume(); //In onresume fetching value from sharedpreference SharedPreferences sharedPreferences =
@Override
protected void onResume() {
super.onResume();
//In onresume fetching value from sharedpreference
SharedPreferences sharedPreferences =
getSharedPreferences(Config.SHARED_PREF_NAME,Context.MODE_PRIVATE);
//Fetching the boolean value form sharedpreferences
loggedIn = sharedPreferences.getBoolean(Config.LOGGEDIN_SHARED_PREF,
false);
//If we will get true
if(loggedIn){
//We will start the Profile Activity
Intent intent = new Intent(LoginActivity.this,
PofileActivity.class);
startActivity(intent);
}
}
private void login(){
//Getting values from edit texts
final String email = editTextEmail.getText().toString().trim();
final String password = editTextPassword.getText().toString().trim();
//Creating a string request
StringRequest stringRequest = new StringRequest(Request.Method.POST,
Config.LOGIN_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
//If we are getting success from server
if(response.equalsIgnoreCase(Config.LOGIN_SUCCESS)){
//Creating a shared preference
SharedPreferences sharedPreferences =
LoginActivity.this.getSharedPreferences(Config.SHARED_PREF_NAME,
Context.MODE_PRIVATE);
//Creating editor to store values to shared
preferences
SharedPreferences.Editor editor =
sharedPreferences.edit();
//Adding values to editor
editor.putBoolean(Config.LOGGEDIN_SHARED_PREF,
true);
editor.putString(Config.EMAIL_SHARED_PREF, email);
//Saving values to editor
editor.commit();
//Starting profile activity
Intent intent = new Intent(LoginActivity.this,
PofileActivity.class);
startActivity(intent);
}else{
//If the server response is not success
//Displaying an error message on toast
Toast.makeText(LoginActivity.this, "Invalid username
or password", Toast.LENGTH_LONG).show();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d("Error: " + error.getMessage());
//You can handle error here if you want
}
}){
@Override
public Map<String, String> getParams() throws AuthFailureError {
Map<String,String> params = new HashMap<>();
//Adding parameters to request
params.put("Content-Type", "application/json");
params.put(Config.KEY_EMAIL, email);
params.put(Config.KEY_PASSWORD, password);
//returning parameter
return params;
}
};
//Adding the string request to the queue
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
@Override
public void onClick(View v) {
//Calling the login function
login();
}
@覆盖
受保护的void onResume(){
super.onResume();
//在onresume中从SharedReference获取值
SharedReferences SharedReferences=
GetSharedReferences(Config.SHARED\u PREF\u NAME,Context.MODE\u PRIVATE);
//从SharedReferences获取布尔值
loggedIn=SharedReferences.getBoolean(Config.loggedIn\u SHARED\u PREF,
假);
//如果我们能实现
if(loggedIn){
//我们将启动配置文件活动
意向意向=新意向(LoginActivity.this,
PofileActivity.class);
星触觉(意向);
}
}
私有void登录(){
//从编辑文本中获取值
最终字符串email=editTextEmail.getText().toString().trim();
最终字符串密码=editTextPassword.getText().toString().trim();
//创建字符串请求
StringRequest StringRequest=新StringRequest(Request.Method.POST,
Config.LOGIN\u URL,
新的Response.Listener(){
@凌驾
公共void onResponse(字符串响应){
//如果我们从服务器获得成功
if(response.equalsIgnoreCase(Config.LOGIN\u SUCCESS)){
//创建共享首选项
SharedReferences SharedReferences=
LoginActivity.this.getSharedReferences(Config.SHARED\u PREF\u NAME,
上下文。模式(私人);
//创建编辑器以将值存储到共享
偏好
SharedReferences.Editor=
SharedReferences.edit();
//向编辑器添加值
editor.putBoolean(Config.LOGGEDIN\u SHARED\u PREF,
正确的);
putString(Config.EMAIL\u SHARED\u PREF,EMAIL);
//将值保存到编辑器
commit();
//启动配置文件活动
意向意向=新意向(LoginActivity.this,
PofileActivity.class);
星触觉(意向);
}否则{
//如果服务器响应不成功
//在toast上显示错误消息
Toast.makeText(LoginActivity.this,“无效用户名
或密码“,Toast.LENGTH_LONG).show();
}
}
},
新的Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
d(“错误:+Error.getMessage());
//如果需要,可以在此处处理错误
}
}){
@凌驾
公共映射getParams()引发AuthFailureError{
Map params=新的HashMap();
//向请求添加参数
参数put(“内容类型”、“应用程序/json”);
参数put(Config.KEY_EMAIL,EMAIL);
参数put(Config.KEY_PASSWORD,PASSWORD);
//返回参数
返回参数;
}
};
//将字符串请求添加到队列
RequestQueue RequestQueue=Volley.newRequestQueue(this);
添加(stringRequest);
}
@凌驾
公共void onClick(视图v){
//调用登录函数
登录();
}
这是使用volley库时常见的问题。当您发出请求时,您可能没有设置正确的标题,这就是为什么它会给我们400个错误请求。
您可能需要重写getBodyContentType()方法,以便正确更新内容类型标题
public String getBodyContentType()
{
return "application/xml";
}
我写了一篇博客解释这个问题以及如何解决这个问题。请参阅解决方案。您的API是否按预期工作?400表示请求不正确。您是否传递了正确的参数?还要检查url或参数中是否有可能的输入错误扫描您发布的url?我的url是。我的电脑和手机共享相同的互联网连接,但是我的手机无法连接到网络,它说我没有访问服务器的权限。不过,通过使用emulator,我可以访问,但出现错误400