我正在使用Volley庫向Android後端發送一個來自Android Java的http請求。後端應用程序按照預期響應一個錯誤代碼和描述,以及一個StatusDescription。我可以通過wireshark查看響應狀態描述,但不知道如何獲取android端的描述字符串。如何使用Volley獲取錯誤消息描述
final JsonObjectRequest request = new JsonObjectRequest(JsonObjectRequest.Method.POST,
url,json,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
TextView mTextView = (TextView) findViewById(R.id.output);
print("Success");
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
TextView mTextView = (TextView) findViewById(R.id.output);
print("Failure (" + error.networkResponse.statusCode + ")");
//Trying to get the error description/response phrase here
}
}
);
這是C#代碼處理該請求:
[WebInvoke(方法= 「POST」,UriTemplate = 「用戶」,BodyStyle = WebMessageBodyStyle.Wrapped,RequestFormat = WebMessageFormat.Json,ResponseFormat = WebMessageFormat .json)] [OperationContract] void addUser(String username,String firstname,String lastname,String email,String hash) Console.WriteLine(DateTime.Now +「Packet receieved」);
//Stores the response object that will be sent back to the android client
OutgoingWebResponseContext response = WebOperationContext.Current.OutgoingResponse;
String description = "User added";
response.StatusCode = System.Net.HttpStatusCode.OK;
//Tries to add the new user
try
{
userTable.Insert(username,firstname,lastname,email,hash);
}
catch (SqlException e)
{
//Default response is a conflict
response.StatusCode = System.Net.HttpStatusCode.Conflict;
description = "Bad Request (" + e.Message + ")";
//Check what the conflict is
if (userTable.GetData().AsEnumerable().Any(row => username == row.Field<String>("username")))
{
description = "Username in use";
}
else if (userTable.GetData().AsEnumerable().Any(row => email == row.Field<String>("email")))
{
description = "Email address in use";
}
else
{
response.StatusCode = System.Net.HttpStatusCode.BadRequest;
}
}
//display and respond with the description
Console.WriteLine(description);
response.StatusDescription = description;
}
我已經看過其他人的問題,但似乎無法找到我正在尋找的答案。有人知道怎麼做嗎?我嘗試過的很多方法都會導致空的花括號,表明JSON的空體。我正在嘗試獲取狀態描述。
這工作,謝謝你的回覆! – GraphiteEdge