Android Volley StringRequest

来源:互联网 发布:linux怎么ping百度 编辑:程序博客网 时间:2024/04/29 02:08

Android Volley

private void VerifyUserCredential() {        String url= clsGlobal.ServiceUrl + "account/credentials";        RequestQueue queue = Volley.newRequestQueue(this);        StringRequest request = new StringRequest(                Request.Method.POST, url,                new Response.Listener<String>() {            @Override            public void onResponse(String response) {                if(response.equals(""))                {                    Toast.makeText(LoginActivity.this,"用户名或密码错误,请重新检查", Toast.LENGTH_LONG).show();                }else {                    Gson gson = new Gson();                    FormUser strUserInfo = gson.fromJson(response, FormUser.class);                    clsGlobal.StoreID = strUserInfo.getStore();                    clsGlobal.UserID = strUserInfo.getUserName();                    clsGlobal.UserName = strUserInfo.getFullName();                    Intent intent=new Intent(LoginActivity.this,MenuActivity.class);                    startActivity(intent);                }                Log.d("", response.toString());            }        }, new Response.ErrorListener() {            @Override            public void onErrorResponse(VolleyError error) {                Log.d("", error.getMessage());                if ((error instanceof TimeoutError) || (error instanceof NoConnectionError)) {                    Toast.makeText(LoginActivity.this,"网络请求超时!", Toast.LENGTH_LONG).show();                } else if (error instanceof AuthFailureError) {                    //TODO                } else if (error instanceof ServerError) {                    //TODO                    Toast.makeText(LoginActivity.this,"404页面!", Toast.LENGTH_LONG).show();                } else if (error instanceof NetworkError) {                    //TODO                } else if (error instanceof ParseError) {                    //TODO                }            }        }){            @Override            protected Map<String, String> getParams() throws AuthFailureError {                Map<String, String> params = new HashMap<String, String>();                params.put("username", "1");                params.put("password", "1");                return  params;            }            @Override            public Map<String, String> getHeaders() throws AuthFailureError {                Map<String, String> params = new HashMap<String, String>();                params.put("store", "1111");                //params.put("Content-Type", "application/json");                return  params;            }        };        queue.add(request);    }

Web Api

[HttpPost]        [Route("account/credentials")]        [APIExceptionFilter]        public IHttpActionResult VerifyCredentials(UserCredentialRequest request)        {            string StoreId = System.Web.HttpContext.Current.Request.Headers["store"];            if (StoreId == null)                return BadRequest();            var user = _UsersRepository.TableAsNoTracking                .Where(x => x.StoreId == StoreId && x.UserId == request.username)                .FirstOrDefault();            if (user == null || user.IsLocked)                return Content(HttpStatusCode.NoContent, "");            if (!Cryptography.VerifyPassword(request.password, user.Password))                return Content(HttpStatusCode.NoContent, "");            var response = new UserCredentialResponse();            response.Store = StoreId;            response.UserName = request.username;            response.FullName = user.UserName;            return Ok(response);        }
0 0
原创粉丝点击