我正在构建一个日志在我调用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 } return Response.success(responseString,HttpHeaderParser.parseCacheHeaders(response)); } };
requestQueue.add(stringRequest);
} catch (JSONException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
});
这是我的网络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的响应是&#34; 2013&#34;(用户名的id) 我怎么能存储这个id,我的问题是我得到的响应是代码200而不是用户名的id 任何帮助将不胜感激
答案 0 :(得分:0)
好吧,如果您只想永久存储该值以供以后使用,您可以将其存储在共享首选项或数据库中,这取决于您。我个人宁愿使用数据库来获取这种用户数据。