私はユーザー名とパスワードの検証を確認する安らかなAPIを呼び出すログインページを構築しています。ステータスコードがOKならば、apiはユーザーIDを返します。このIDを格納し私はアンドロイドからポストメソッドを呼び出すときにjsonの応答を返します
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();
}
}
});
これは私の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からの応答が、私はこのIDを保存することができますどのように "2013"(ユーザー名のID) です私の問題は、私はコード200としてユーザー名のIDとしての応答ではありません 何か助けていただければ幸いです
私の質問はそれを保存する方法です、私のレスポンス文字列は、ユーザ名 –
のIDではなく、あなたのサーバコードも投稿してください。 – Cristian
https://stackoverflow.com/users/7048554/cristian投稿 –