【发布时间】:2016-03-07 10:19:25
【问题描述】:
我正在使用 Volley 库从 Android Java 向 c# 后端发送一个 http 请求。后端应用程序以预期的错误代码和描述以及 StatusDescription 进行响应。我可以通过wireshark看到响应状态描述,但不知道如何在android端获取描述字符串。
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)] [运营合同] void addUser(String username, String firstname, String lastname, String email, String hash) { Console.WriteLine(DateTime.Now + "收到的数据包");
//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 的主体为空。我正在尝试专门获取状态描述。
【问题讨论】:
标签: java android android-volley