意外的响应代码400?

时间:2017-02-16 05:46:17

标签: android android-volley

我正在使用Volley库并发出意外的响应代码400.我正在尝试从用户名和密码登录,但遇到com.android.volley.ServerError.How可以解决这个问题吗?

登录类

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]

1 个答案:

答案 0 :(得分:0)

试试这段代码 这使用POST方法

final String URL = Vcruit.getInstance().GET_LOGIN;
    // Post params to be sent to the server
    HashMap<String, String> params = new HashMap<String, String>();
    params.put("apikey", xxx.getInstance().API_KEY);
    params.put("userName", Username);
    params.put("password", Password);


        JsonObjectRequest req = new JsonObjectRequest(URL, new JSONObject(params),
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    try {
                        VolleyLog.v("Response:%n %s", response.toString(4));
                        JSONObject jObj = new JSONObject(response.toString());
                        JSONArray results = jObj.getJSONArray("data");
                        for (int i = 0; i < results.length(); i++) {
                            JSONObject cat = results.getJSONObject(i);
                            String status = cat.getString("status");
                            String errormsg = cat.getString("ErrorMsg");

                            if (status.equals("Success")) {
                                                                } else {


                            }
                            }

                    } catch (JSONException e) {

                        e.printStackTrace();
                    }
                    loader.setVisibility(View.GONE);
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            VolleyLog.e("Error: ", error.getMessage());
        }
    });

    xxx.getInstance().addToRequestQueue(req);
}

https://www.simplifiedcoding.net/android-studio-volley-tutorial-to-create-a-login-application/