【发布时间】:2017-12-13 18:47:22
【问题描述】:
我正在构建一个登录页面,我在其中调用一个 restful api 来检查用户名和密码验证,如果状态代码正常,api 返回用户名 ID,我希望能够存储这个 ID 并在以后使用它
signin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
RequestQueue requestQueue = Volley.newRequestQueue(SignIn.this);
String URL = "http://localhost/WebApplication7/api/login";
JSONObject jsonBody = new JSONObject();
// jsonBody.put("tblRegisteredUsers_nickName", username.getText().toString().trim());
jsonBody.put("tblRegisteredUsers_UserName", username.getText().toString().trim());
jsonBody.put("tblRegisteredUsers_Password", password.getText().toString().trim());
final String requestBody = jsonBody.toString();
StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
if (response.trim().equals("2013")) {
//login authenticated. Start the next activity of your app
Toast.makeText(SignIn.this, "Logged In", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
} else {
//login failed. prompt to re-enter the credentials
Toast.makeText(SignIn.this, "Failed to log In", Toast.LENGTH_SHORT).show();
Log.i("VOLLEY", response);
Toast.makeText(getApplicationContext(), response.toString(), Toast.LENGTH_SHORT).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("VOLLEY", error.toString());
}
}) {
@Override
public String getBodyContentType() {
return "application/json; charset=utf-8";
}
@Override
public byte[] getBody() throws AuthFailureError {
try {
return requestBody == null ? null : requestBody.getBytes("utf-8");
} catch (UnsupportedEncodingException uee) {
VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s", requestBody, "utf-8");
return null;
}
}
@Override
protected Response<String> parseNetworkResponse(NetworkResponse response) {
String responseString = "";
if (response != null) {
responseString = String.valueOf(response.statusCode);
// Toast.makeText(getApplicationContext(), responseString, Toast.LENGTH_SHORT).show(); // 可以获取response.headers等更多细节 } 返回 Response.success(responseString, HttpHeaderParser.parseCacheHeaders(response)); } };
requestQueue.add(stringRequest);
} catch (JSONException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
});
这是我的 web api 代码 (vb asp.net)
Public Function login(<FromBody> ByVal users As tblRegisteredUser) As IHttpActionResult
Try
Using entities As IAPD_DBEntities = New IAPD_DBEntities()
Dim username = entities.tblRegisteredUsers.FirstOrDefault(Function(e) e.tblRegisteredUsers_UserName.Equals(users.tblRegisteredUsers_UserName))
Dim password = entities.tblRegisteredUsers.FirstOrDefault(Function(e) e.tblRegisteredUsers_Password.Equals(users.tblRegisteredUsers_Password))
If username Is Nothing OrElse password Is Nothing Then
Dim message = Request.CreateResponse(HttpStatusCode.BadRequest, "username or password is incorrect")
Return message
Else
Dim response = Request.CreateResponse(HttpStatusCode.OK)
response.Content = New StringContent(username.tblRegisteredUsers_UserPKID.ToString.Trim.ToString, Encoding.UTF8, "application/json")
'Dim message = Request.CreateResponse(HttpStatusCode.Created, username.tblRegisteredUsers_UserPKID.ToString.Trim)
'message.Headers.Location = New Uri(Request.RequestUri, "Loged In successfully")
Return Ok(username.tblRegisteredUsers_UserPKID.ToString.Trim.ToString)
End If
End Using
Catch e As Exception
Return BadRequest(e.ToString)
End Try
End Function
并且来自 api 的响应是“2013”(用户名的 id) 我如何存储此 ID,我的问题是我得到响应为代码 200 而不是用户名的 ID 任何帮助将不胜感激
【问题讨论】:
标签: android rest android-volley