android - 意外的响应代码 400 截击安卓

标签 android mysql

我编写的代码是将我的android studio 连接到wamp 服务器mysql。我尝试从 mysql 检索登录信息到 android。但是它一直显示意外的响应代码 400。我应该如何更改我的代码?

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

最佳答案

这是使用 volley 库时的常见问题。您在发出请求时可能没有设置正确的 header ,这就是它给我们400 Bad Request 错误的原因。

您可能需要覆盖 getBodyContentType() 方法,以便正确更新 Content-Type header 。

public String getBodyContentType()
{
    return "application/xml";
}

我已经写了一篇博客来解释这个问题以及如何解决这个问题。请引用here寻求解决方案。

关于android - 意外的响应代码 400 截击安卓,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35524178/

相关文章:

android - 如何从 Android 应用生成类似事件的 Ctrl+v(粘贴)?

android - 使用 importet .jar 文件构建应用时出错

php - 如何在 Php mySql 中按字母顺序显示表数据?

mysql - 对于大型表连接和更新,我应该更改哪些 MySQL my.ini 参数?

MYSQL - 空白问题分组依据

python - 使用 Python 将 BibTex 文件转换为数据库条目

Android:过滤 SimpleCursorAdapter ListView

java - Android 类的 Mockito ClassNotFoundException

java - 使用警报对话框从内部类访问变量

php - 全文搜索非常慢